Conservative heuristic for tree-sitter parser ranges (bug#73324)

* src/treesit.c (treesit_sync_visible_region): If the parser's original
ranges don't overlap with visible region, give it a zero range, rather
than don't set any range.
* test/src/treesit-tests.el (treesit-range-fixup-after-edit): Test new
behavior.
This commit is contained in:
Yuan Fu 2024-09-17 21:17:13 -07:00
parent 035024b4e5
commit f0daa2f215
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 19 additions and 24 deletions

View file

@ -1098,24 +1098,20 @@ treesit_sync_visible_region (Lisp_Object parser)
prev_cons = lisp_ranges; prev_cons = lisp_ranges;
} }
XTS_PARSER (parser)->last_set_ranges = new_ranges_head;
if (NILP (new_ranges_head))
{
/* We are in a weird situation here: none of the previous ranges /* We are in a weird situation here: none of the previous ranges
overlaps with the new visible region. We don't have any good overlaps with the new visible region. We don't have any good
options, so just throw the towel: just remove ranges and hope options, so just throw the towel: just give the parser a zero
lisp world will soon update with reasonable ranges or just range. (Perfect filling!!) */
delete this parser. */ if (NILP (new_ranges_head))
bool success; new_ranges_head = Fcons (Fcons (make_fixnum (visible_beg),
success = ts_parser_set_included_ranges (XTS_PARSER (parser)->parser, make_fixnum (visible_beg)),
NULL, 0); Qnil);
eassert (success);
} XTS_PARSER (parser)->last_set_ranges = new_ranges_head;
else
{
uint32_t len = 0; uint32_t len = 0;
TSRange *ts_ranges = treesit_make_ts_ranges (new_ranges_head, parser, TSRange *ts_ranges = NULL;
ts_ranges = treesit_make_ts_ranges (new_ranges_head, parser,
&len); &len);
bool success; bool success;
success = ts_parser_set_included_ranges (XTS_PARSER (parser)->parser, success = ts_parser_set_included_ranges (XTS_PARSER (parser)->parser,
@ -1123,7 +1119,6 @@ treesit_sync_visible_region (Lisp_Object parser)
xfree (ts_ranges); xfree (ts_ranges);
eassert (success); eassert (success);
} }
}
/* (ref:bytepos-range-pitfall) Suppose we have the following buffer /* (ref:bytepos-range-pitfall) Suppose we have the following buffer
content ([ ] is a unibyte char, [ ] is a multibyte char): content ([ ] is a unibyte char, [ ] is a multibyte char):

View file

@ -741,7 +741,7 @@ visible_end.)"
;; { } narrow ;; { } narrow
(narrow-to-region 1 10) (narrow-to-region 1 10)
(should (equal (treesit-parser-included-ranges parser) (should (equal (treesit-parser-included-ranges parser)
nil))))) '((1 . 1)))))))
;;; Multiple language ;;; Multiple language