Skip to content

Commit

Permalink
Use PositionCodec from pygls (microsoft#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig authored Jan 17, 2024
1 parent 1546a62 commit 49b49dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
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

0 comments on commit 49b49dc

Please sign in to comment.