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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
from typing import Tuple, Union, overload, Optional
from pylsp_rope import typing
from pylsp_rope.typing import LineNumber, CharNumber, Literal
START_OF_LINE: Literal["^"] = "^"
END_OF_LINE: Literal["$"] = "$"
AutoLineNumber = Union[LineNumber, int]
AutoCharNumber = Union[CharNumber, int]
_CharNumberOrMarker = Union[AutoCharNumber, Literal["^", "$"]]
_PrimitiveLineCharNumber = Union[
AutoLineNumber, Tuple[AutoLineNumber, Optional[_CharNumberOrMarker]]
]
@overload
def Position(
line: Tuple[AutoLineNumber, Optional[_CharNumberOrMarker]],
*,
_default_character: _CharNumberOrMarker = CharNumber(0),
) -> typing.Position: ...
@overload
def Position(
line: AutoLineNumber,
*,
_default_character: _CharNumberOrMarker = CharNumber(0),
) -> typing.Position: ...
@overload
def Position(
line: AutoLineNumber,
character: AutoCharNumber,
) -> typing.Position: ...
@overload
def Position(
line: AutoLineNumber,
character: Literal["^", "$"],
) -> typing.Position: ...
def Position(
line: _PrimitiveLineCharNumber,
character: Optional[_CharNumberOrMarker] = None,
*,
_default_character: _CharNumberOrMarker = CharNumber(0),
) -> typing.Position:
"""
Returns a [Position](https://microsoft.github.io/language-server-protocol/specification#position)
object for a document.
`pos` can be:
- Tuple[LineNumber, CharNumber] are passed directly to the object
- int selects the start of the line
- "^" the first non-blank character of the line
- "$" the end of the line, which is the start of the next line
Selects the start of line 4
>>> a = Position(4)
>>> b = Position(4, 0)
>>> c = Position((4, 0))
>>> assert a == b == c
Selects the end of line 4:
>>> c = Position(4, "$")
>>> d = Position(5, 0)
>>> assert c == d
"""
if isinstance(line, tuple):
# assert (
# character is None
# ), "If `line` is a tuple, then `character` must not be supplied"
lineno, character = line
else:
lineno = line
if character is None:
character = _default_character
if character == "$":
lineno = LineNumber(lineno + 1)
character = CharNumber(0)
assert character != "^", "not implemented yet"
return {
"line": lineno,
"character": character,
}
def Range(
start: _PrimitiveLineCharNumber,
end: Optional[_PrimitiveLineCharNumber] = None,
) -> typing.Range:
"""
Returns a [Range](https://microsoft.github.io/language-server-protocol/specification#range)
object for a document.
`start` and `end` accepts the same arguments as Position object.
If `start` or `end` is an int, then the whole line is selected.
Selects the whole line 4, including the line ending
>>> a = Range(4)
>>> b = Range(4, 4)
>>> c = Range((4, 0), (5, 0))
>>> assert a == b == c
Selects line 4-6
>>> d = Range(4, 6)
>>> e = Range((4, 0), (7, 0))
>>> assert d == e
"""
if end is None:
end = start
return {
"start": Position(start, _default_character=CharNumber(0)),
"end": Position(end, _default_character=END_OF_LINE),
}
|