]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix treesit-node-at wrt local parsers
authorYuan Fu <casouri@gmail.com>
Sat, 9 Sep 2023 19:35:13 +0000 (12:35 -0700)
committerYuan Fu <casouri@gmail.com>
Sat, 9 Sep 2023 19:36:49 +0000 (12:36 -0700)
* lisp/treesit.el (treesit-node-at): Don't compute language at point
first.  Because treesit-language-at uses treesit-node-at with a
language argument. Only call treesit-language-at when PARSER-OR-LANG
is nil.

lisp/treesit.el

index 1711446b40b1bac42dfb5cc1ed08ed22ea74ec03..8371d0dda08d6e454e444ebb62a24bd3a1a7cf21 100644 (file)
@@ -189,16 +189,18 @@ is a language, find the first parser for that language in the
 current buffer, or create one if none exists; If PARSER-OR-LANG
 is nil, try to guess the language at POS using `treesit-language-at'.
 
-If there's a local parser at POS, try to use that parser first."
-  (let* ((lang-at-point (treesit-language-at pos))
-         (root (if (treesit-parser-p parser-or-lang)
+If there's a local parser at POS, the local parser takes priority
+unless PARSER-OR-LANG is a parser, or PARSER-OR-LANG is a
+language and doesn't match the language of the local parser."
+  (let* ((root (if (treesit-parser-p parser-or-lang)
                    (treesit-parser-root-node parser-or-lang)
-                 (or (when-let ((parser (car (treesit-local-parsers-at
-                                              pos (or parser-or-lang
-                                                      lang-at-point)))))
+                 (or (when-let ((parser
+                                 (car (treesit-local-parsers-at
+                                       pos parser-or-lang))))
                        (treesit-parser-root-node parser))
                      (treesit-buffer-root-node
-                      (or parser-or-lang lang-at-point)))))
+                      (or parser-or-lang
+                          (treesit-language-at pos))))))
          (node root)
          (node-before root)
          (pos-1 (max (1- pos) (point-min)))