]> git.eshelyaron.com Git - emacs.git/commitdiff
Use the first parser from 'treesit-parser-list' to fix tests.
authorJuri Linkov <juri@linkov.net>
Thu, 17 Apr 2025 06:49:04 +0000 (09:49 +0300)
committerEshel Yaron <me@eshelyaron.com>
Thu, 17 Apr 2025 07:20:21 +0000 (09:20 +0200)
* 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'.

(cherry picked from commit ee46b6c4e67aa52b4ef683c5c16f64638e369cd3)

lisp/treesit.el
test/src/treesit-tests.el

index d09ecd8c618597ccad6486908f4ee5f93b7b4264..009c219dd9f120035a55a541dc4526c8b76de485 100644 (file)
@@ -872,8 +872,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)))
index 9bf07677ab2ce0292ca546e0f69cc07feaeb6e6f..6095eb0c593a1a8097be552954e4d9457747a412 100644 (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)))
-
-    (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))))
+  (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)))))
 
 ;;; 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")))))
-
-    (should node)
-    (should (equal (treesit-node-text node) "1"))))
+  (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")))))
 
 (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)))
-
-    (should node)
-    (should (equal (treesit-node-text node) "2"))))
+  (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")))))
 
 ;;; Imenu