forked from Instagram/LibCST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_position.py
69 lines (54 loc) · 2.06 KB
/
_position.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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.
"""
Data structures used for storing position information.
These are publicly exported by metadata, but their implementation lives outside of
metadata, because they're used internally by the codegen logic, which computes position
locations.
"""
from dataclasses import dataclass
from typing import cast, overload, Tuple, Union
from libcst._add_slots import add_slots
_CodePositionT = Union[Tuple[int, int], "CodePosition"]
@add_slots
@dataclass(frozen=True)
class CodePosition:
#: Line numbers are 1-indexed.
line: int
#: Column numbers are 0-indexed.
column: int
@add_slots
@dataclass(frozen=True)
# pyre-fixme[13]: Attribute `end` is never initialized.
# pyre-fixme[13]: Attribute `start` is never initialized.
class CodeRange:
#: Starting position of a node (inclusive).
start: CodePosition
#: Ending position of a node (exclusive).
end: CodePosition
@overload
def __init__(self, start: CodePosition, end: CodePosition) -> None:
...
@overload
def __init__(self, start: Tuple[int, int], end: Tuple[int, int]) -> None:
...
def __init__(self, start: _CodePositionT, end: _CodePositionT) -> None:
if isinstance(start, tuple) and isinstance(end, tuple):
object.__setattr__(self, "start", CodePosition(start[0], start[1]))
object.__setattr__(self, "end", CodePosition(end[0], end[1]))
else:
start = cast(CodePosition, start)
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
)