add cache, try parsing using pygls
This commit is contained in:
+11
-42
@@ -6,52 +6,21 @@ from lsprotocol.types import Position, Range
|
||||
from parsimonious.nodes import Node
|
||||
|
||||
|
||||
# @total_ordering
|
||||
# class Position(NamedTuple):
|
||||
# line: int
|
||||
# char: int
|
||||
#
|
||||
# def __lt__(self, other: Self) -> bool:
|
||||
# return (self.line < other.line) or (
|
||||
# (self.line == other.line) and (self.char < other.char)
|
||||
# )
|
||||
#
|
||||
# def __eq__(self, other: Self) -> bool:
|
||||
# return (self.line == other.line) and (self.char == other.char)
|
||||
#
|
||||
#
|
||||
# class Range(NamedTuple):
|
||||
# start: Position
|
||||
# end: Position
|
||||
#
|
||||
# def __add__(self, other: Self) -> Self:
|
||||
# start = min(self.start, other.start)
|
||||
# end = max(self.end, other.end)
|
||||
# return Range(start, end)
|
||||
#
|
||||
# def contained_by(self, possibly_contained_by: Self) -> bool:
|
||||
# return (self.start >= possibly_contained_by.start) and (
|
||||
# self.end <= possibly_contained_by.end
|
||||
# )
|
||||
#
|
||||
# def contains(self, possibly_contains: Self) -> bool:
|
||||
# return (self.start <= possibly_contains.start) and (
|
||||
# self.end >= possibly_contains.end
|
||||
# )
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Locator:
|
||||
raw: str
|
||||
|
||||
@cached_property
|
||||
def newlines(self) -> tuple[int, ...]:
|
||||
t = tuple(i for i, char in enumerate(self.raw) if char == "\n")
|
||||
return t
|
||||
raw: list[str]
|
||||
|
||||
def _locate_pos(self, index: int) -> Position:
|
||||
line = next(i for i, char in enumerate(self.newlines) if char >= index)
|
||||
return Position(line - 1, index - (self.newlines[line - 1] if line > 0 else 0))
|
||||
counter = 0
|
||||
line = 0
|
||||
for ix, raw_line in enumerate(self.raw):
|
||||
if counter + len(raw_line) > index:
|
||||
line = ix
|
||||
break
|
||||
else:
|
||||
counter += len(raw_line)
|
||||
|
||||
return Position(line, index - counter)
|
||||
|
||||
@overload
|
||||
def locate(self, index: int) -> Position:
|
||||
|
||||
Reference in New Issue
Block a user