From 284e2cee653f5a6a97beedecf4f23ee91db51ea4 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Thu, 6 Jun 2024 15:46:53 +0200 Subject: [PATCH] Add `CodeRange.__contains__` --- libcst/_position.py | 11 +++++++++++ libcst/tests/test_position.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 libcst/tests/test_position.py diff --git a/libcst/_position.py b/libcst/_position.py index d7ba0d072..67923dc52 100644 --- a/libcst/_position.py +++ b/libcst/_position.py @@ -56,3 +56,14 @@ def __init__(self, start: _CodePositionT, end: _CodePositionT) -> None: end = cast(CodePosition, end) object.__setattr__(self, "start", start) object.__setattr__(self, "end", end) + + def __contains__(self, pos: _CodePositionT, /) -> bool: + if isinstance(pos, tuple): + line, column = pos + else: + line, column = pos.line, pos.column + + return ( + self.start.line <= line <= self.end.line + and self.start.column <= column <= self.end.column + ) diff --git a/libcst/tests/test_position.py b/libcst/tests/test_position.py new file mode 100644 index 000000000..af4621594 --- /dev/null +++ b/libcst/tests/test_position.py @@ -0,0 +1,29 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +from libcst._position import _CodePositionT, CodePosition, CodeRange +from libcst.testing.utils import data_provider, UnitTest + + +code_range = CodeRange((1, 2), (4, 5)) + + +class PositionTest(UnitTest): + @data_provider( + [ + (CodePosition(1, 2), True), + ((1, 2), True), + (CodePosition(3, 3), True), + ((3, 3), True), + (CodePosition(2, 1), False), + ((2, 1), True), + (CodePosition(5, 4), False), + ((5, 4), False), + ] + ) + def test_code_range_contains( + self, position: _CodePositionT, expected: bool + ) -> None: + self.assertEqual(position in code_range, expected)