2
2
3
3
import weakref
4
4
from asyncio import CancelledError , Event , Task , create_task , sleep
5
- from functools import partial
6
5
from typing import Callable , NamedTuple
7
6
8
7
try :
@@ -61,7 +60,7 @@ def __init__(
61
60
"""The tree-sitter Parser or None if tree-sitter is unavailable."""
62
61
63
62
self ._syntax_tree : Tree = self ._parser .parse (
64
- partial ( self ._read_callable , lines = self . lines )
63
+ self .text . encode ( "utf-8" )
65
64
) # type: ignore
66
65
"""The tree-sitter Tree (syntax tree) built from the document."""
67
66
@@ -165,7 +164,7 @@ def replace_range(self, start: Location, end: Location, text: str) -> EditResult
165
164
)
166
165
return replace_result
167
166
168
- def reparse (self , timeout_us : int , lines : list [ str ], syntax_tree = None ) -> bool :
167
+ def reparse (self , timeout_us : int , text : bytes ) -> bool :
169
168
"""Reparse the document.
170
169
171
170
Args:
@@ -176,13 +175,12 @@ def reparse(self, timeout_us: int, lines: list[str], syntax_tree=None) -> bool:
176
175
True if parsing succeeded and False if a timeout occurred.
177
176
"""
178
177
assert timeout_us > 0
179
- read_source = partial (self ._read_callable , lines = lines )
180
178
tree = self ._syntax_tree
181
179
saved_timeout = self ._parser .timeout_micros
182
180
try :
183
181
self ._parser .timeout_micros = timeout_us
184
182
try :
185
- tree = self ._parser .parse (read_source , tree ) # type: ignore[arg-type]
183
+ tree = self ._parser .parse (text , tree ) # type: ignore[arg-type]
186
184
except ValueError :
187
185
# The only known cause is a timeout.
188
186
return False
@@ -194,7 +192,7 @@ def set_new_tree():
194
192
self ._syntax_tree = tree
195
193
196
194
changed_ranges = self ._syntax_tree .changed_ranges (tree )
197
- self ._syntax_tree_update_callback (self ._syntax_tree , len ( lines ) )
195
+ self ._syntax_tree_update_callback (self ._syntax_tree )
198
196
else :
199
197
self ._syntax_tree = tree
200
198
return True
@@ -256,50 +254,6 @@ def _location_to_point(self, location: Location) -> tuple[int, int]:
256
254
bytes_on_left = 0
257
255
return row , bytes_on_left
258
256
259
- def _read_callable (
260
- self ,
261
- byte_offset : int ,
262
- point : tuple [int , int ],
263
- lines : list [str ],
264
- ) -> bytes :
265
- """A callable which informs tree-sitter about the document content.
266
-
267
- This is passed to tree-sitter which will call it frequently to retrieve
268
- the bytes from the document.
269
-
270
- Args:
271
- byte_offset: The number of (utf-8) bytes from the start of the document.
272
- point: A tuple (row index, column *byte* offset). Note that this differs
273
- from our Location tuple which is (row_index, column codepoint offset).
274
- lines: The lines of the document being parsed.
275
-
276
- Returns:
277
- All the utf-8 bytes between the byte_offset/point and the end of the current
278
- line _including_ the line separator character(s). Returns None if the
279
- offset/point requested by tree-sitter doesn't correspond to a byte.
280
- """
281
- row , column = point
282
- newline = self .newline
283
-
284
- row_out_of_bounds = row >= len (lines )
285
- if row_out_of_bounds :
286
- return b""
287
- else :
288
- row_text = lines [row ]
289
-
290
- encoded_row = _utf8_encode (row_text )
291
- encoded_row_length = len (encoded_row )
292
-
293
- if column < encoded_row_length :
294
- return encoded_row [column :] + _utf8_encode (newline )
295
- elif column == encoded_row_length :
296
- return _utf8_encode (newline [0 ])
297
- elif column == encoded_row_length + 1 :
298
- if newline == "\r \n " :
299
- return b"\n "
300
-
301
- return b""
302
-
303
257
304
258
class BackgroundSyntaxParser :
305
259
"""A provider of incremental background parsing for syntax highlighting.
@@ -357,15 +311,15 @@ async def _perform_a_single_reparse(self, force_update: bool) -> None:
357
311
358
312
# In order to allow the user to continue editing without interruption, we reparse
359
313
# a snapshot of the TextArea's document.
360
- copy_of_text_for_parsing = document .copy_of_lines ( )
314
+ copy_of_text_for_parsing = document .text . encode ( "utf-8" )
361
315
362
- # Use tree-sitter's parser timeout mechanism, when necessary, break the
363
- # full reparse into multiple steps. Most of the time, tree-sitter is so
364
- # fast that no looping occurs.
316
+ # Use tree-sitter's parser timeout mechanism to break the full reparse
317
+ # into multiple steps. Most of the time, tree-sitter is so fast that no
318
+ # looping occurs.
365
319
parsed_ok = False
366
320
while not parsed_ok :
367
321
parsed_ok = document .reparse (
368
- self .PARSE_TIMEOUT_MICROSECONDS , lines = copy_of_text_for_parsing
322
+ self .PARSE_TIMEOUT_MICROSECONDS , text = copy_of_text_for_parsing
369
323
)
370
324
if not parsed_ok :
371
325
# Sleeping for zero seconds allows other tasks, I/O, *etc.* to execute,
0 commit comments