Fix comment in treesit_record_change (bug#61369)

Turns out the previous commit message and comment is not entirely
correct: the old behavior is in fact wrong, not just "correct but has
problems".

Here is why the old code is wrong:

|visible range|     -> markup for visible range

updated range       -> markup for updated range
-------------

First we have some text

|aaaaaa|

Now we insert something at the beginning, because we clip
new_end_offset to visible_end, out of eight b's inserted, only the
first six are known to tree-sitter.

|bbbbbbbbaaaa|aa  start: 0, old_end: 0, new_end: 6
 ------

In treesit_sync_visible_region, we sync up visible region, but the two
missing b's are not in the updated range.

|bbbbbbbbaaaaaa|  start: 12, old_end: 12, new_end: 14
             --

The old behavior not only is wrong, but also doesn't make much sense.

* src/treesit.c (treesit_record_change): Update comment.
This commit is contained in:
Yuan Fu 2023-02-18 02:20:12 -08:00
parent 1e5cebc88b
commit e985466556
No known key found for this signature in database
GPG key ID: 56E19BC57664A442

View file

@ -797,12 +797,10 @@ treesit_record_change (ptrdiff_t start_byte, ptrdiff_t old_end_byte,
max (visible_beg, old_end_byte))
- visible_beg);
/* We don't clip new_end_offset under visible_end, because
inserting in narrowed region always extends the visible
region. If we clip new_end_offset here, and re-add the
clipped "tail" in treesit_sync_visible_region later,
while it is technically equivalent, tree-sitter's
incremental parsing algorithm doesn't seem to like it
(bug#61369). */
otherwise we would miss updating the clipped part. Plus,
when inserting in narrowed region, the narrowed region
will grow to accommodate the new text, so this is the
correct behavior. (Bug#61369). */
ptrdiff_t new_end_offset = (max (visible_beg, new_end_byte)
- visible_beg);
eassert (start_offset <= old_end_offset);