Fix treesit.el tests

* lisp/treesit.el (treesit--imenu-merge-entries):
(Ftreesit_parser_set_included_ranges): Warn in the docstring
that the function is destructive/owns the argument.
* test/src/treesit-tests.el (treesit-range-fixup-after-edit):
(treesit-imenu): Fix tests.
This commit is contained in:
Yuan Fu 2025-01-17 18:24:45 -08:00
parent f7e41ba3d0
commit fae424aba9
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
3 changed files with 26 additions and 19 deletions

View file

@ -3333,7 +3333,9 @@ ENTRY. MARKER marks the start of each tree-sitter node."
ENTRIES is a list of (CATEGORY . SUB-ENTRIES...). Merge them so there's
no duplicate CATEGORY. CATEGORY's are strings. The merge is stable,
meaning the order of elements are kept."
meaning the order of elements are kept.
This function is destructive, meaning ENTRIES will be modified."
(let ((return-entries nil))
(dolist (entry entries)
(let* ((category (car entry))

View file

@ -1946,7 +1946,10 @@ which the parser should operate. Regions must not overlap, and the
regions should come in order in the list. Signal
`treesit-set-range-error' if the argument is invalid, or something
else went wrong. If RANGES is nil, the PARSER is to parse the whole
buffer. */)
buffer.
DO NOT modify RANGES after passing it to this function, as RANGES is
saved to PARSER internally. */)
(Lisp_Object parser, Lisp_Object ranges)
{
treesit_check_parser (parser);

View file

@ -697,58 +697,60 @@ visible_end.)"
(with-temp-buffer
(let ((parser (treesit-parser-create 'json)))
(insert "11111111111111111111")
(treesit-parser-set-included-ranges parser '((1 . 20)))
(treesit-parser-set-included-ranges parser (copy-tree '((1 . 20))))
(treesit-parser-root-node parser)
(should (equal (treesit-parser-included-ranges parser)
'((1 . 20))))
(copy-tree '((1 . 20)))))
(narrow-to-region 5 15)
(should (equal (treesit-parser-included-ranges parser)
'((5 . 15))))
(copy-tree '((5 . 15)))))
(widen)
;; Trickier ranges
;; 11111111111111111111
;; [ ] [ ]
;; { narrow }
(treesit-parser-set-included-ranges parser '((1 . 7) (10 . 15)))
(treesit-parser-set-included-ranges
parser (copy-tree '((1 . 7) (10 . 15))))
(should (equal (treesit-parser-included-ranges parser)
'((1 . 7) (10 . 15))))
(copy-tree '((1 . 7) (10 . 15)))))
(narrow-to-region 5 13)
(should (equal (treesit-parser-included-ranges parser)
'((5 . 7) (10 . 13))))
(copy-tree '((5 . 7) (10 . 13)))))
;; Narrow in front, and discard the last one.
(widen)
(treesit-parser-set-included-ranges
parser '((4 . 10) (12 . 14) (16 . 20)))
parser (copy-tree '((4 . 10) (12 . 14) (16 . 20))))
;; 11111111111111111111
;; [ ] [ ] [ ]
;; { } narrow
(narrow-to-region 1 8)
(should (equal (treesit-parser-included-ranges parser)
'((4 . 8))))
(copy-tree '((4 . 8)))))
;; Narrow in back, and discard the first one.
(widen)
(treesit-parser-set-included-ranges
parser '((1 . 5) (7 . 9) (11 . 17)))
parser (copy-tree '((1 . 5) (7 . 9) (11 . 17))))
;; 11111111111111111111
;; [ ] [ ] [ ]
;; { } narrow
(narrow-to-region 15 20)
(should (equal (treesit-parser-included-ranges parser)
'((15 . 17))))
(copy-tree '((15 . 17)))))
;; No overlap
(widen)
(treesit-parser-set-included-ranges parser '((15 . 20)))
(treesit-parser-set-included-ranges
parser (copy-tree '((15 . 20))))
;; 11111111111111111111
;; [ ]
;; { } narrow
(narrow-to-region 1 10)
(should (equal (treesit-parser-included-ranges parser)
'((1 . 1)))))))
(copy-tree '((1 . 1))))))))
;;; Multiple language
@ -1275,11 +1277,11 @@ This tests bug#60355."
(ert-deftest treesit-imenu ()
"Test imenu functions."
(should (equal (treesit--imenu-merge-entries
'(("Function" . (f1 f2))
("Function" . (f3 f4 f5))
("Class" . (c1 c2 c3))
("Variables" . (v1 v2))
("Class" . (c4))))
(copy-tree '(("Function" . (f1 f2))
("Function" . (f3 f4 f5))
("Class" . (c1 c2 c3))
("Variables" . (v1 v2))
("Class" . (c4)))))
'(("Function" . (f1 f2 f3 f4 f5))
("Class" . (c1 c2 c3 c4))
("Variables" . (v1 v2))))))