Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PositionCodec from pygls #414

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions bundled/tool/lsp_edit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import List, Optional

from lsprotocol import types as lsp
from pygls.workspace.position_codec import PositionCodec

DIFF_TIMEOUT = 1 # 1 second

Expand All @@ -29,28 +30,16 @@ def get_text_edits(
) -> List[lsp.TextEdit]:
"""Return a list of text edits to transform old_text into new_text."""

def code_units(c: str) -> int:
if position_encoding == lsp.PositionEncodingKind.Utf16:
return len(c.encode("utf-16-le")) // 2
elif position_encoding == lsp.PositionEncodingKind.Utf8:
return len(c.encode("utf-8"))
return len(c.encode("utf-32-le")) // 4
lines = old_text.splitlines(True)
codec = PositionCodec(position_encoding)

line_offsets = [0]
code_unit_offsets = []

for line in old_text.splitlines(True):
col_offset = [0]
for c in line:
col_offset.append(col_offset[-1] + code_units(c))
code_unit_offsets.append(col_offset)
for line in lines:
line_offsets.append(line_offsets[-1] + len(line))
code_unit_offsets.append([0])

def from_offset(offset: int) -> lsp.Position:
line = bisect.bisect_right(line_offsets, offset) - 1
col = offset - line_offsets[line]
character = code_unit_offsets[line][col]
character = offset - line_offsets[line]
return lsp.Position(line=line, character=character)

sequences = []
Expand All @@ -64,7 +53,13 @@ def from_offset(offset: int) -> lsp.Position:
if sequences:
edits = [
lsp.TextEdit(
range=lsp.Range(start=from_offset(old_start), end=from_offset(old_end)),
range=codec.range_to_client_units(
lines=lines,
range=lsp.Range(
start=from_offset(old_start),
end=from_offset(old_end),
),
),
new_text=new_text[new_start:new_end],
)
for opcode, old_start, old_end, new_start, new_end in sequences
Expand Down
6 changes: 3 additions & 3 deletions src/test/python_tests/test_edit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
new_text='"',
),
lsp.TextEdit(
range=lsp.Range(lsp.Position(0, 9), lsp.Position(0, 10)),
range=lsp.Range(lsp.Position(0, 8), lsp.Position(0, 9)),
new_text='"',
),
],
Expand Down Expand Up @@ -85,11 +85,11 @@ def test_with_emojis(encoding: lsp.PositionEncodingKind, expected: List[lsp.Text
lsp.PositionEncodingKind.Utf8,
[
lsp.TextEdit(
range=lsp.Range(lsp.Position(1, 179), lsp.Position(1, 179)),
range=lsp.Range(lsp.Position(1, 136), lsp.Position(1, 136)),
new_text="\n ",
),
lsp.TextEdit(
range=lsp.Range(lsp.Position(1, 354), lsp.Position(1, 354)),
range=lsp.Range(lsp.Position(1, 268), lsp.Position(1, 268)),
new_text=",",
),
],
Expand Down
Loading