aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/treesit.el6
-rw-r--r--test/src/treesit-tests.el235
2 files changed, 124 insertions, 117 deletions
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 156e0b87709..8be6276decb 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -873,8 +873,10 @@ If ONLY contains the symbol `primary', include the primary parser."
873 (and (memq 'global only) 873 (and (memq 'global only)
874 (not (overlay-get ov 'treesit-parser-local-p)))))) 874 (not (overlay-get ov 'treesit-parser-local-p))))))
875 (push (if with-host (cons parser host-parser) parser) res))) 875 (push (if with-host (cons parser host-parser) parser) res)))
876 (when (or (null only) (memq 'primary only)) 876 (when (and treesit-primary-parser (or (null only) (memq 'primary only)))
877 (setq res (cons treesit-primary-parser res))) 877 (push treesit-primary-parser res))
878 (unless res
879 (push (car (treesit-parser-list)) res))
878 (seq-sort-by (lambda (p) 880 (seq-sort-by (lambda (p)
879 (treesit-parser-embed-level 881 (treesit-parser-embed-level
880 (or (car-safe p) p))) 882 (or (car-safe p) p)))
diff --git a/test/src/treesit-tests.el b/test/src/treesit-tests.el
index caacb74315d..770849c4566 100644
--- a/test/src/treesit-tests.el
+++ b/test/src/treesit-tests.el
@@ -835,104 +835,107 @@ visible_end.)"
835(ert-deftest treesit-node-supplemental () 835(ert-deftest treesit-node-supplemental ()
836 "Supplemental node functions." 836 "Supplemental node functions."
837 (skip-unless (treesit-language-available-p 'json)) 837 (skip-unless (treesit-language-available-p 'json))
838 (let (parser root-node doc-node) 838 (with-temp-buffer
839 (progn 839 (let (parser root-node doc-node)
840 (insert "[1,2,{\"name\": \"Bob\"},3]") 840 (progn
841 (setq parser (treesit-parser-create 'json)) 841 (insert "[1,2,{\"name\": \"Bob\"},3]")
842 (setq root-node (treesit-parser-root-node 842 (setq parser (treesit-parser-create 'json))
843 parser)) 843 (setq root-node (treesit-parser-root-node
844 (setq doc-node (treesit-node-child root-node 0))) 844 parser))
845 ;; `treesit-node-buffer'. 845 (setq doc-node (treesit-node-child root-node 0)))
846 (should (equal (treesit-node-buffer root-node) 846 ;; `treesit-node-buffer'.
847 (current-buffer))) 847 (should (equal (treesit-node-buffer root-node)
848 ;; `treesit-node-language'. 848 (current-buffer)))
849 (should (eq (treesit-node-language root-node) 849 ;; `treesit-node-language'.
850 'json)) 850 (should (eq (treesit-node-language root-node)
851 ;; `treesit-node-at'. 851 'json))
852 (should (equal (treesit-node-string 852 ;; `treesit-node-at'.
853 (treesit-node-at 1 'json)) 853 (should (equal (treesit-node-string
854 "(\"[\")")) 854 (treesit-node-at 1 'json))
855 ;; `treesit-node-on' 855 "(\"[\")"))
856 (should (equal (treesit-node-string 856 ;; `treesit-node-on'
857 (treesit-node-on 1 2 'json)) 857 (should (equal (treesit-node-string
858 "(\"[\")")) 858 (treesit-node-on 1 2 'json))
859 ;; `treesit-buffer-root-node'. 859 "(\"[\")"))
860 (should (treesit-node-eq 860 ;; `treesit-buffer-root-node'.
861 (treesit-buffer-root-node 'json) 861 (should (treesit-node-eq
862 root-node)) 862 (treesit-buffer-root-node 'json)
863 ;; `treesit-filter-child'. 863 root-node))
864 (should (equal (mapcar 864 ;; `treesit-filter-child'.
865 (lambda (node) 865 (should (equal (mapcar
866 (treesit-node-type node)) 866 (lambda (node)
867 (treesit-filter-child 867 (treesit-node-type node))
868 doc-node (lambda (node) 868 (treesit-filter-child
869 (treesit-node-check node 'named)))) 869 doc-node (lambda (node)
870 '("number" "number" "object" "number"))) 870 (treesit-node-check node 'named))))
871 ;; `treesit-node-text'. 871 '("number" "number" "object" "number")))
872 (should (equal (treesit-node-text doc-node) 872 ;; `treesit-node-text'.
873 "[1,2,{\"name\": \"Bob\"},3]")) 873 (should (equal (treesit-node-text doc-node)
874 ;; `treesit-node-index'. 874 "[1,2,{\"name\": \"Bob\"},3]"))
875 (should (eq (treesit-node-index doc-node) 875 ;; `treesit-node-index'.
876 0)) 876 (should (eq (treesit-node-index doc-node)
877 ;; TODO: 877 0))
878 ;; `treesit-parent-until' 878 ;; TODO:
879 ;; `treesit-parent-while' 879 ;; `treesit-parent-until'
880 ;; `treesit-node-children' 880 ;; `treesit-parent-while'
881 ;; `treesit-node-field-name' 881 ;; `treesit-node-children'
882 ;; `treesit-search-forward-goto' 882 ;; `treesit-node-field-name'
883 )) 883 ;; `treesit-search-forward-goto'
884 )))
884 885
885(ert-deftest treesit-node-at () 886(ert-deftest treesit-node-at ()
886 "Test `treesit-node-at'." 887 "Test `treesit-node-at'."
887 (skip-unless (treesit-language-available-p 'json)) 888 (skip-unless (treesit-language-available-p 'json))
888 (let (parser) 889 (with-temp-buffer
889 (progn 890 (let (parser)
890 (insert "[1, 2, 3,4] ") 891 (progn
891 (setq parser (treesit-parser-create 'json)) 892 (insert "[1, 2, 3,4] ")
892 (treesit-parser-root-node parser)) 893 (setq parser (treesit-parser-create 'json))
893 ;; Point at ",", should return ",". 894 (treesit-parser-root-node parser))
894 (goto-char (point-min)) 895 ;; Point at ",", should return ",".
895 (search-forward "1") 896 (goto-char (point-min))
896 (should (equal (treesit-node-text 897 (search-forward "1")
897 (treesit-node-at (point))) 898 (should (equal (treesit-node-text
898 ",")) 899 (treesit-node-at (point)))
899 ;; Point behind ",", should still return the ",". 900 ","))
900 (search-forward ",") 901 ;; Point behind ",", should still return the ",".
901 (should (equal (treesit-node-text 902 (search-forward ",")
902 (treesit-node-at (point))) 903 (should (equal (treesit-node-text
903 ",")) 904 (treesit-node-at (point)))
904 ;; Point between "," and "2", should return 2. 905 ","))
905 (forward-char) 906 ;; Point between "," and "2", should return 2.
906 (should (equal (treesit-node-text 907 (forward-char)
907 (treesit-node-at (point))) 908 (should (equal (treesit-node-text
908 "2")) 909 (treesit-node-at (point)))
909 ;; EOF, should return the last leaf node "]". 910 "2"))
910 (goto-char (point-max)) 911 ;; EOF, should return the last leaf node "]".
911 (should (equal (treesit-node-text 912 (goto-char (point-max))
912 (treesit-node-at (point))) 913 (should (equal (treesit-node-text
913 "]")))) 914 (treesit-node-at (point)))
915 "]")))))
914 916
915(ert-deftest treesit-node-check () 917(ert-deftest treesit-node-check ()
916 "Test `treesit-node-check'." 918 "Test `treesit-node-check'."
917 (skip-unless (treesit-language-available-p 'json)) 919 (skip-unless (treesit-language-available-p 'json))
918 (let (parser root-node array-node comment-node) 920 (with-temp-buffer
919 (progn 921 (let (parser root-node array-node comment-node)
920 (insert "/* comment */ [1, 2, 3,4 ") 922 (progn
921 (setq parser (treesit-parser-create 'json)) 923 (insert "/* comment */ [1, 2, 3,4 ")
922 (setq root-node (treesit-parser-root-node 924 (setq parser (treesit-parser-create 'json))
923 parser)) 925 (setq root-node (treesit-parser-root-node
924 (setq comment-node (treesit-node-child root-node 0)) 926 parser))
925 (setq array-node (treesit-node-child root-node 1))) 927 (setq comment-node (treesit-node-child root-node 0))
926 928 (setq array-node (treesit-node-child root-node 1)))
927 (should (treesit-node-check comment-node 'extra)) 929
928 (should (treesit-node-check array-node 'has-error)) 930 (should (treesit-node-check comment-node 'extra))
929 (should-error (treesit-node-check array-node 'xxx)) 931 (should (treesit-node-check array-node 'has-error))
930 (should (treesit-node-check (treesit-node-child array-node -1) 932 (should-error (treesit-node-check array-node 'xxx))
931 'missing)) 933 (should (treesit-node-check (treesit-node-child array-node -1)
932 (goto-char (point-max)) 934 'missing))
933 (insert "]") 935 (goto-char (point-max))
934 (treesit-parser-root-node parser) 936 (insert "]")
935 (should (treesit-node-check array-node 'outdated)))) 937 (treesit-parser-root-node parser)
938 (should (treesit-node-check array-node 'outdated)))))
936 939
937;;; Defun navigation 940;;; Defun navigation
938;; 941;;
@@ -1262,36 +1265,38 @@ This tests bug#60355."
1262 "Test search subtree forward." 1265 "Test search subtree forward."
1263 (skip-unless (treesit-language-available-p 'python)) 1266 (skip-unless (treesit-language-available-p 'python))
1264 (require 'python) 1267 (require 'python)
1265 (python-ts-mode) 1268 (with-temp-buffer
1266 (insert "Temp(1, 2)") 1269 (python-ts-mode)
1267 (goto-char (point-min)) 1270 (insert "Temp(1, 2)")
1268 (pcase-let* ((`((,_ . ,call-node)) 1271 (goto-char (point-min))
1269 (treesit-query-capture (treesit-buffer-root-node) 1272 (pcase-let* ((`((,_ . ,call-node))
1270 '((call) @c))) 1273 (treesit-query-capture (treesit-buffer-root-node)
1271 (node (treesit-search-subtree 1274 '((call) @c)))
1272 call-node 1275 (node (treesit-search-subtree
1273 (lambda (n) (equal (treesit-node-type n) "integer"))))) 1276 call-node
1274 1277 (lambda (n) (equal (treesit-node-type n) "integer")))))
1275 (should node) 1278
1276 (should (equal (treesit-node-text node) "1")))) 1279 (should node)
1280 (should (equal (treesit-node-text node) "1")))))
1277 1281
1278(ert-deftest treesit-search-subtree-backward-1 () 1282(ert-deftest treesit-search-subtree-backward-1 ()
1279 "Test search subtree with backward=t." 1283 "Test search subtree with backward=t."
1280 (skip-unless (treesit-language-available-p 'python)) 1284 (skip-unless (treesit-language-available-p 'python))
1281 (require 'python) 1285 (require 'python)
1282 (python-ts-mode) 1286 (with-temp-buffer
1283 (insert "Temp(1, 2)") 1287 (python-ts-mode)
1284 (goto-char (point-min)) 1288 (insert "Temp(1, 2)")
1285 (pcase-let* ((`((,_ . ,call-node)) 1289 (goto-char (point-min))
1286 (treesit-query-capture (treesit-buffer-root-node) 1290 (pcase-let* ((`((,_ . ,call-node))
1287 '((call) @c))) 1291 (treesit-query-capture (treesit-buffer-root-node)
1288 (node (treesit-search-subtree 1292 '((call) @c)))
1289 call-node 1293 (node (treesit-search-subtree
1290 (lambda (n) (equal (treesit-node-type n) "integer")) 1294 call-node
1291 t))) 1295 (lambda (n) (equal (treesit-node-type n) "integer"))
1292 1296 t)))
1293 (should node) 1297
1294 (should (equal (treesit-node-text node) "2")))) 1298 (should node)
1299 (should (equal (treesit-node-text node) "2")))))
1295 1300
1296;;; Imenu 1301;;; Imenu
1297 1302