aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2024-09-15 00:24:03 -0700
committerYuan Fu2024-09-15 00:31:40 -0700
commit460b9d705ab482003fabe75b0fd1df223abe467c (patch)
treefa25d65ea42a6e5f48f7a5a32d5b95fb6e265fae
parent81347c1aaf25b27e78e8beee4bc818ad2c4e1b71 (diff)
downloademacs-460b9d705ab482003fabe75b0fd1df223abe467c.tar.gz
emacs-460b9d705ab482003fabe75b0fd1df223abe467c.zip
Fix treesit_sync_visible_region's range fixup code (bug#73264)
new_ranges_head | v ( )->( )->( )->( )->( ) ^ ^ | | | lisp_ranges (loop head) | prev_cons -> set cdr to nil to cut of the rest result: ( )->( ) * src/treesit.c (treesit_sync_visible_region): Cut off this cons and the rest, not set the current range's end to nil. * test/src/treesit-tests.el: (treesit-range-fixup-after-edit): Add tests for all cases.
-rw-r--r--src/treesit.c14
-rw-r--r--test/src/treesit-tests.el32
2 files changed, 43 insertions, 3 deletions
diff --git a/src/treesit.c b/src/treesit.c
index 9958d8a4c2a..628a4dff9cc 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -1064,6 +1064,7 @@ treesit_sync_visible_region (Lisp_Object parser)
1064 if (NILP (lisp_ranges)) return; 1064 if (NILP (lisp_ranges)) return;
1065 1065
1066 Lisp_Object new_ranges_head = lisp_ranges; 1066 Lisp_Object new_ranges_head = lisp_ranges;
1067 Lisp_Object prev_cons = Qnil;
1067 1068
1068 FOR_EACH_TAIL_SAFE (lisp_ranges) 1069 FOR_EACH_TAIL_SAFE (lisp_ranges)
1069 { 1070 {
@@ -1076,9 +1077,12 @@ treesit_sync_visible_region (Lisp_Object parser)
1076 new_ranges_head = XCDR (new_ranges_head); 1077 new_ranges_head = XCDR (new_ranges_head);
1077 else if (beg >= visible_end) 1078 else if (beg >= visible_end)
1078 { 1079 {
1079 /* Even the beg is after visible_end, dicard this range and all 1080 /* Even the beg is after visible_end, discard this range and all
1080 the ranges after it. */ 1081 the ranges after it. */
1081 XSETCDR (range, Qnil); 1082 if (NILP (prev_cons))
1083 new_ranges_head = Qnil;
1084 else
1085 XSETCDR (prev_cons, Qnil);
1082 break; 1086 break;
1083 } 1087 }
1084 else 1088 else
@@ -1091,12 +1095,18 @@ treesit_sync_visible_region (Lisp_Object parser)
1091 if (end > visible_end) 1095 if (end > visible_end)
1092 XSETCDR (range, make_fixnum (visible_end)); 1096 XSETCDR (range, make_fixnum (visible_end));
1093 } 1097 }
1098 prev_cons = lisp_ranges;
1094 } 1099 }
1095 1100
1096 XTS_PARSER (parser)->last_set_ranges = new_ranges_head; 1101 XTS_PARSER (parser)->last_set_ranges = new_ranges_head;
1097 1102
1098 if (NILP (new_ranges_head)) 1103 if (NILP (new_ranges_head))
1099 { 1104 {
1105 /* We are in a weird situation here: none of the previous ranges
1106 overlaps with the new visible region. We don't have any good
1107 options, so just throw the towel: just remove ranges and hope
1108 lisp world will soon update with reasonable ranges or just
1109 delete this parser. */
1100 bool success; 1110 bool success;
1101 success = ts_parser_set_included_ranges (XTS_PARSER (parser)->parser, 1111 success = ts_parser_set_included_ranges (XTS_PARSER (parser)->parser,
1102 NULL, 0); 1112 NULL, 0);
diff --git a/test/src/treesit-tests.el b/test/src/treesit-tests.el
index 98aaeb62781..68ed6ca751f 100644
--- a/test/src/treesit-tests.el
+++ b/test/src/treesit-tests.el
@@ -709,7 +709,37 @@ visible_end.)"
709 '((1 . 7) (10 . 15)))) 709 '((1 . 7) (10 . 15))))
710 (narrow-to-region 5 13) 710 (narrow-to-region 5 13)
711 (should (equal (treesit-parser-included-ranges parser) 711 (should (equal (treesit-parser-included-ranges parser)
712 '((5 . 7) (10 . 13))))))) 712 '((5 . 7) (10 . 13))))
713
714 ;; Narrow in front.
715 (widen)
716 (treesit-parser-set-included-ranges parser '((4 . 17)))
717 ;; 11111111111111111111
718 ;; [ ]
719 ;; { } narrow
720 (narrow-to-region 1 8)
721 (should (equal (treesit-parser-included-ranges parser)
722 '((4 . 8))))
723
724 ;; Narrow in back.
725 (widen)
726 (treesit-parser-set-included-ranges parser '((4 . 17)))
727 ;; 11111111111111111111
728 ;; [ ]
729 ;; { } narrow
730 (narrow-to-region 15 20)
731 (should (equal (treesit-parser-included-ranges parser)
732 '((15 . 17))))
733
734 ;; No overlap
735 (widen)
736 (treesit-parser-set-included-ranges parser '((15 . 20)))
737 ;; 11111111111111111111
738 ;; [ ]
739 ;; { } narrow
740 (narrow-to-region 1 10)
741 (should (equal (treesit-parser-included-ranges parser)
742 nil)))))
713 743
714;;; Multiple language 744;;; Multiple language
715 745