]> git.eshelyaron.com Git - emacs.git/commitdiff
Make treesit-node-at take advantage of the embed-level property
authorYuan Fu <casouri@gmail.com>
Thu, 27 Feb 2025 12:06:36 +0000 (04:06 -0800)
committerEshel Yaron <me@eshelyaron.com>
Fri, 28 Feb 2025 11:20:13 +0000 (12:20 +0100)
* lisp/treesit.el (treesit-node-at): Select the local parser
with the highest embed-level.

(cherry picked from commit 8a45c2da226e188420956fd6269f72db3f437e38)

lisp/treesit.el

index 020c16338550b74edba4c0f777a02757e76d8ae0..6b42b0bf320ee942fca96fce2b091701d3465523 100644 (file)
@@ -258,10 +258,20 @@ language and doesn't match the language of the local parser."
                 ;; finding parser, try local parser first, then global
                 ;; parser.
                 (t
-                 ;; LANG can be nil.
-                 (let* ((lang (treesit-language-at pos))
-                        (local-parser (car (treesit-local-parsers-at
-                                            pos lang)))
+                 ;; LANG can be nil.  We don't want to use the fallback
+                 ;; in `treesit-language-at', so here we call
+                 ;; `treesit-language-at-point-function' directly.
+                 (let* ((lang (and treesit-language-at-point-function
+                                   (funcall treesit-language-at-point-function
+                                            pos)))
+                        (local-parser
+                         ;; Find the local parser with highest
+                         ;; embed-level at point.
+                         (car (seq-sort-by #'treesit-parser-embed-level
+                                           (lambda (a b)
+                                             (> (or a 0) (or b 0)))
+                                           (treesit-local-parsers-at
+                                            pos lang))))
                         (global-parser (car (treesit-parser-list
                                              nil lang)))
                         (parser (or local-parser global-parser)))