22
33import  weakref 
44from  asyncio  import  CancelledError , Event , Task , create_task , sleep 
5- from  functools  import  partial 
65from  typing  import  Callable , NamedTuple 
76
87try :
@@ -61,7 +60,7 @@ def __init__(
6160        """The tree-sitter Parser or None if tree-sitter is unavailable.""" 
6261
6362        self ._syntax_tree : Tree  =  self ._parser .parse (
64-             partial ( self ._read_callable ,  lines = self . lines )
63+             self .text . encode ( "utf-8" )
6564        )  # type: ignore 
6665        """The tree-sitter Tree (syntax tree) built from the document.""" 
6766
@@ -165,7 +164,7 @@ def replace_range(self, start: Location, end: Location, text: str) -> EditResult
165164        )
166165        return  replace_result 
167166
168-     def  reparse (self , timeout_us : int , lines :  list [ str ],  syntax_tree = None ) ->  bool :
167+     def  reparse (self , timeout_us : int , text :  bytes ) ->  bool :
169168        """Reparse the document. 
170169
171170        Args: 
@@ -176,13 +175,12 @@ def reparse(self, timeout_us: int, lines: list[str], syntax_tree=None) -> bool:
176175            True if parsing succeeded and False if a timeout occurred. 
177176        """ 
178177        assert  timeout_us  >  0 
179-         read_source  =  partial (self ._read_callable , lines = lines )
180178        tree  =  self ._syntax_tree 
181179        saved_timeout  =  self ._parser .timeout_micros 
182180        try :
183181            self ._parser .timeout_micros  =  timeout_us 
184182            try :
185-                 tree  =  self ._parser .parse (read_source , tree )  # type: ignore[arg-type] 
183+                 tree  =  self ._parser .parse (text , tree )  # type: ignore[arg-type] 
186184            except  ValueError :
187185                # The only known cause is a timeout. 
188186                return  False 
@@ -194,7 +192,7 @@ def set_new_tree():
194192                        self ._syntax_tree  =  tree 
195193
196194                    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 )
198196                else :
199197                    self ._syntax_tree  =  tree 
200198                return  True 
@@ -256,50 +254,6 @@ def _location_to_point(self, location: Location) -> tuple[int, int]:
256254            bytes_on_left  =  0 
257255        return  row , bytes_on_left 
258256
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- 
303257
304258class  BackgroundSyntaxParser :
305259    """A provider of incremental background parsing for syntax highlighting. 
@@ -357,15 +311,15 @@ async def _perform_a_single_reparse(self, force_update: bool) -> None:
357311
358312        # In order to allow the user to continue editing without interruption, we reparse 
359313        # 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" )
361315
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. 
365319        parsed_ok  =  False 
366320        while  not  parsed_ok :
367321            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 
369323            )
370324            if  not  parsed_ok :
371325                # Sleeping for zero seconds allows other tasks, I/O, *etc.* to execute, 
0 commit comments