Skip to content

Commit 2093116

Browse files
committed
feat: make TextDocument.lines be a Sequence[str]
The `PositionCodec` does not need a mutable sequence as it only does read-only operations. Additionally, `TextDocument.lines` can now return an immutable list (using tuple here for simplicity), which will enable `pygls` to cache the `lines` attribute later internally. The caching is not done in this commit as it would require us to figure out the cache invalidation rules, which will be after this has been accepted.
1 parent be50d2c commit 2093116

3 files changed

Lines changed: 18 additions & 19 deletions

File tree

pygls/workspace/position_codec.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
# limitations under the License. #
1818
############################################################################
1919
import logging
20-
from typing import List, Optional, Union
20+
from typing import Optional, Union, Sequence
2121

2222
from lsprotocol import types
2323

24-
2524
log = logging.getLogger(__name__)
2625

2726

@@ -64,7 +63,7 @@ def client_num_units(self, chars: str):
6463
return utf32_units + self.utf16_unit_offset(chars)
6564

6665
def position_from_client_units(
67-
self, lines: List[str], position: types.Position
66+
self, lines: Sequence[str], position: types.Position
6867
) -> types.Position:
6968
"""
7069
Convert the position.character from UTF-[32|16|8] code units to UTF-32.
@@ -84,7 +83,7 @@ def position_from_client_units(
8483
see: https://github.com/microsoft/language-server-protocol/issues/376
8584
8685
Arguments:
87-
lines (list):
86+
lines (sequence):
8887
The content of the document which the position refers to.
8988
position (Position):
9089
The line and character offset in UTF-[32|16|8] code units.
@@ -138,14 +137,14 @@ def position_from_client_units(
138137
return position
139138

140139
def position_to_client_units(
141-
self, lines: List[str], position: types.Position
140+
self, lines: Sequence[str], position: types.Position
142141
) -> types.Position:
143142
"""
144143
Convert the position.character from its internal UTF-32 representation
145144
to client-supported UTF-[32|16|8] code units.
146145
147146
Arguments:
148-
lines (list):
147+
lines (sequence):
149148
The content of the document which the position refers to.
150149
position (Position):
151150
The line and character offset in UTF-32 code units.
@@ -165,13 +164,13 @@ def position_to_client_units(
165164
return types.Position(line=len(lines), character=0)
166165

167166
def range_from_client_units(
168-
self, lines: List[str], range: types.Range
167+
self, lines: Sequence[str], range: types.Range
169168
) -> types.Range:
170169
"""
171170
Convert range.[start|end].character from UTF-[32|16|8] code units to UTF-32.
172171
173172
Arguments:
174-
lines (list):
173+
lines (sequence):
175174
The content of the document which the range refers to.
176175
range (Range):
177176
The line and character offset in UTF-[32|16|8] code units.
@@ -186,13 +185,13 @@ def range_from_client_units(
186185
return range_new
187186

188187
def range_to_client_units(
189-
self, lines: List[str], range: types.Range
188+
self, lines: Sequence[str], range: types.Range
190189
) -> types.Range:
191190
"""
192191
Convert range.[start|end].character from UTF-32 to UTF-[32|16|8] code units.
193192
194193
Arguments:
195-
lines (list):
194+
lines (sequence):
196195
The content of the document which the range refers to.
197196
range (Range):
198197
The line and character offset in code units.

pygls/workspace/text_document.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import os
2222
import pathlib
2323
import re
24-
from typing import List, Optional, Pattern
24+
from typing import Optional, Pattern, Sequence
2525

2626
from lsprotocol import types
2727

@@ -163,8 +163,8 @@ def apply_change(self, change: types.TextDocumentContentChangeEvent) -> None:
163163
self._apply_full_change(change)
164164

165165
@property
166-
def lines(self) -> List[str]:
167-
return self.source.splitlines(True)
166+
def lines(self) -> Sequence[str]:
167+
return tuple(self.source.splitlines(True))
168168

169169
def offset_at_position(self, client_position: types.Position) -> int:
170170
"""Return the character offset pointed at by the given client_position."""

tests/test_document.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_document_end_of_file_edit():
5151
)
5252
doc.apply_change(change)
5353

54-
assert doc.lines == [
54+
assert list(doc.lines) == [
5555
"print 'a'\n",
5656
"print 'b'\n",
5757
"o",
@@ -73,7 +73,7 @@ def test_document_full_edit():
7373
)
7474
doc.apply_change(change)
7575

76-
assert doc.lines == ["print a, b"]
76+
assert list(doc.lines) == ["print a, b"]
7777

7878
doc = TextDocument(
7979
"file:///uri", "".join(old), sync_kind=types.TextDocumentSyncKind.Full
@@ -87,7 +87,7 @@ def test_document_full_edit():
8787
)
8888
doc.apply_change(change)
8989

90-
assert doc.lines == ["print a, b"]
90+
assert list(doc.lines) == ["print a, b"]
9191

9292

9393
def test_document_line_edit():
@@ -125,7 +125,7 @@ def test_document_multiline_edit():
125125
)
126126
doc.apply_change(change)
127127

128-
assert doc.lines == ["def hello(a, b):\n", " print a, b\n"]
128+
assert list(doc.lines) == ["def hello(a, b):\n", " print a, b\n"]
129129

130130
doc = TextDocument(
131131
"file:///uri", "".join(old), sync_kind=types.TextDocumentSyncKind.Incremental
@@ -139,7 +139,7 @@ def test_document_multiline_edit():
139139
)
140140
doc.apply_change(change)
141141

142-
assert doc.lines == ["def hello(a, b):\n", " print a, b\n"]
142+
assert list(doc.lines) == ["def hello(a, b):\n", " print a, b\n"]
143143

144144

145145
def test_document_no_edit():
@@ -157,7 +157,7 @@ def test_document_no_edit():
157157
)
158158
doc.apply_change(change)
159159

160-
assert doc.lines == old
160+
assert list(doc.lines) == old
161161

162162

163163
def test_document_props():

0 commit comments

Comments
 (0)