Use the first parser from 'treesit-parser-list' to fix tests.

* lisp/treesit.el (treesit-parsers-at): Add treesit-primary-parser
only when it's non-nil.  When the result list is still empty,
add the first parser from 'treesit-parser-list'.
https://lists.gnu.org/archive/html/emacs-devel/2025-04/msg00627.html

* test/src/treesit-tests.el (treesit-node-supplemental)
(treesit-node-at, treesit-node-check)
(treesit-search-subtree-forward-1)
(treesit-search-subtree-backward-1): Wrap test body in 'with-temp-buffer'.
This commit is contained in:
Juri Linkov 2025-04-17 09:49:04 +03:00
parent 2925ff6c53
commit ee46b6c4e6
2 changed files with 121 additions and 114 deletions

View file

@ -873,8 +873,10 @@ If ONLY contains the symbol `primary', include the primary parser."
(and (memq 'global only)
(not (overlay-get ov 'treesit-parser-local-p))))))
(push (if with-host (cons parser host-parser) parser) res)))
(when (or (null only) (memq 'primary only))
(setq res (cons treesit-primary-parser res)))
(when (and treesit-primary-parser (or (null only) (memq 'primary only)))
(push treesit-primary-parser res))
(unless res
(push (car (treesit-parser-list)) res))
(seq-sort-by (lambda (p)
(treesit-parser-embed-level
(or (car-safe p) p)))

View file

@ -835,104 +835,107 @@ visible_end.)"
(ert-deftest treesit-node-supplemental ()
"Supplemental node functions."
(skip-unless (treesit-language-available-p 'json))
(let (parser root-node doc-node)
(progn
(insert "[1,2,{\"name\": \"Bob\"},3]")
(setq parser (treesit-parser-create 'json))
(setq root-node (treesit-parser-root-node
parser))
(setq doc-node (treesit-node-child root-node 0)))
;; `treesit-node-buffer'.
(should (equal (treesit-node-buffer root-node)
(current-buffer)))
;; `treesit-node-language'.
(should (eq (treesit-node-language root-node)
'json))
;; `treesit-node-at'.
(should (equal (treesit-node-string
(treesit-node-at 1 'json))
"(\"[\")"))
;; `treesit-node-on'
(should (equal (treesit-node-string
(treesit-node-on 1 2 'json))
"(\"[\")"))
;; `treesit-buffer-root-node'.
(should (treesit-node-eq
(treesit-buffer-root-node 'json)
root-node))
;; `treesit-filter-child'.
(should (equal (mapcar
(lambda (node)
(treesit-node-type node))
(treesit-filter-child
doc-node (lambda (node)
(treesit-node-check node 'named))))
'("number" "number" "object" "number")))
;; `treesit-node-text'.
(should (equal (treesit-node-text doc-node)
"[1,2,{\"name\": \"Bob\"},3]"))
;; `treesit-node-index'.
(should (eq (treesit-node-index doc-node)
0))
;; TODO:
;; `treesit-parent-until'
;; `treesit-parent-while'
;; `treesit-node-children'
;; `treesit-node-field-name'
;; `treesit-search-forward-goto'
))
(with-temp-buffer
(let (parser root-node doc-node)
(progn
(insert "[1,2,{\"name\": \"Bob\"},3]")
(setq parser (treesit-parser-create 'json))
(setq root-node (treesit-parser-root-node
parser))
(setq doc-node (treesit-node-child root-node 0)))
;; `treesit-node-buffer'.
(should (equal (treesit-node-buffer root-node)
(current-buffer)))
;; `treesit-node-language'.
(should (eq (treesit-node-language root-node)
'json))
;; `treesit-node-at'.
(should (equal (treesit-node-string
(treesit-node-at 1 'json))
"(\"[\")"))
;; `treesit-node-on'
(should (equal (treesit-node-string
(treesit-node-on 1 2 'json))
"(\"[\")"))
;; `treesit-buffer-root-node'.
(should (treesit-node-eq
(treesit-buffer-root-node 'json)
root-node))
;; `treesit-filter-child'.
(should (equal (mapcar
(lambda (node)
(treesit-node-type node))
(treesit-filter-child
doc-node (lambda (node)
(treesit-node-check node 'named))))
'("number" "number" "object" "number")))
;; `treesit-node-text'.
(should (equal (treesit-node-text doc-node)
"[1,2,{\"name\": \"Bob\"},3]"))
;; `treesit-node-index'.
(should (eq (treesit-node-index doc-node)
0))
;; TODO:
;; `treesit-parent-until'
;; `treesit-parent-while'
;; `treesit-node-children'
;; `treesit-node-field-name'
;; `treesit-search-forward-goto'
)))
(ert-deftest treesit-node-at ()
"Test `treesit-node-at'."
(skip-unless (treesit-language-available-p 'json))
(let (parser)
(progn
(insert "[1, 2, 3,4] ")
(setq parser (treesit-parser-create 'json))
(treesit-parser-root-node parser))
;; Point at ",", should return ",".
(goto-char (point-min))
(search-forward "1")
(should (equal (treesit-node-text
(treesit-node-at (point)))
","))
;; Point behind ",", should still return the ",".
(search-forward ",")
(should (equal (treesit-node-text
(treesit-node-at (point)))
","))
;; Point between "," and "2", should return 2.
(forward-char)
(should (equal (treesit-node-text
(treesit-node-at (point)))
"2"))
;; EOF, should return the last leaf node "]".
(goto-char (point-max))
(should (equal (treesit-node-text
(treesit-node-at (point)))
"]"))))
(with-temp-buffer
(let (parser)
(progn
(insert "[1, 2, 3,4] ")
(setq parser (treesit-parser-create 'json))
(treesit-parser-root-node parser))
;; Point at ",", should return ",".
(goto-char (point-min))
(search-forward "1")
(should (equal (treesit-node-text
(treesit-node-at (point)))
","))
;; Point behind ",", should still return the ",".
(search-forward ",")
(should (equal (treesit-node-text
(treesit-node-at (point)))
","))
;; Point between "," and "2", should return 2.
(forward-char)
(should (equal (treesit-node-text
(treesit-node-at (point)))
"2"))
;; EOF, should return the last leaf node "]".
(goto-char (point-max))
(should (equal (treesit-node-text
(treesit-node-at (point)))
"]")))))
(ert-deftest treesit-node-check ()
"Test `treesit-node-check'."
(skip-unless (treesit-language-available-p 'json))
(let (parser root-node array-node comment-node)
(progn
(insert "/* comment */ [1, 2, 3,4 ")
(setq parser (treesit-parser-create 'json))
(setq root-node (treesit-parser-root-node
parser))
(setq comment-node (treesit-node-child root-node 0))
(setq array-node (treesit-node-child root-node 1)))
(with-temp-buffer
(let (parser root-node array-node comment-node)
(progn
(insert "/* comment */ [1, 2, 3,4 ")
(setq parser (treesit-parser-create 'json))
(setq root-node (treesit-parser-root-node
parser))
(setq comment-node (treesit-node-child root-node 0))
(setq array-node (treesit-node-child root-node 1)))
(should (treesit-node-check comment-node 'extra))
(should (treesit-node-check array-node 'has-error))
(should-error (treesit-node-check array-node 'xxx))
(should (treesit-node-check (treesit-node-child array-node -1)
'missing))
(goto-char (point-max))
(insert "]")
(treesit-parser-root-node parser)
(should (treesit-node-check array-node 'outdated))))
(should (treesit-node-check comment-node 'extra))
(should (treesit-node-check array-node 'has-error))
(should-error (treesit-node-check array-node 'xxx))
(should (treesit-node-check (treesit-node-child array-node -1)
'missing))
(goto-char (point-max))
(insert "]")
(treesit-parser-root-node parser)
(should (treesit-node-check array-node 'outdated)))))
;;; Defun navigation
;;
@ -1262,36 +1265,38 @@ This tests bug#60355."
"Test search subtree forward."
(skip-unless (treesit-language-available-p 'python))
(require 'python)
(python-ts-mode)
(insert "Temp(1, 2)")
(goto-char (point-min))
(pcase-let* ((`((,_ . ,call-node))
(treesit-query-capture (treesit-buffer-root-node)
'((call) @c)))
(node (treesit-search-subtree
call-node
(lambda (n) (equal (treesit-node-type n) "integer")))))
(with-temp-buffer
(python-ts-mode)
(insert "Temp(1, 2)")
(goto-char (point-min))
(pcase-let* ((`((,_ . ,call-node))
(treesit-query-capture (treesit-buffer-root-node)
'((call) @c)))
(node (treesit-search-subtree
call-node
(lambda (n) (equal (treesit-node-type n) "integer")))))
(should node)
(should (equal (treesit-node-text node) "1"))))
(should node)
(should (equal (treesit-node-text node) "1")))))
(ert-deftest treesit-search-subtree-backward-1 ()
"Test search subtree with backward=t."
(skip-unless (treesit-language-available-p 'python))
(require 'python)
(python-ts-mode)
(insert "Temp(1, 2)")
(goto-char (point-min))
(pcase-let* ((`((,_ . ,call-node))
(treesit-query-capture (treesit-buffer-root-node)
'((call) @c)))
(node (treesit-search-subtree
call-node
(lambda (n) (equal (treesit-node-type n) "integer"))
t)))
(with-temp-buffer
(python-ts-mode)
(insert "Temp(1, 2)")
(goto-char (point-min))
(pcase-let* ((`((,_ . ,call-node))
(treesit-query-capture (treesit-buffer-root-node)
'((call) @c)))
(node (treesit-search-subtree
call-node
(lambda (n) (equal (treesit-node-type n) "integer"))
t)))
(should node)
(should (equal (treesit-node-text node) "2"))))
(should node)
(should (equal (treesit-node-text node) "2")))))
;;; Imenu