]> git.eshelyaron.com Git - emacs.git/commitdiff
Add guard in treesit-language-at-point-default (bug#77870)
authorYuan Fu <casouri@gmail.com>
Thu, 17 Apr 2025 23:56:07 +0000 (16:56 -0700)
committerEshel Yaron <me@eshelyaron.com>
Fri, 18 Apr 2025 06:47:22 +0000 (08:47 +0200)
* lisp/treesit.el (treesit-language-at-point-default): Return
nil if there's no parser in the buffer.
(treesit-parsers-at): Add docstring for parser order.

(cherry picked from commit f728aa72c047ff8485bce08a6effb1c7790bce9f)

lisp/treesit.el

index 5311cbffef59d21d87bc3fc3706688bc87be3870..841dd82f1acb0e9f4d711e7b5922920799f9ed39 100644 (file)
@@ -190,9 +190,14 @@ the return value of that function instead."
 
 (defun treesit-language-at-point-default (position)
   "Default function for `treesit-language-at-point-function'.
-Return the deepest parser by embed level."
-  (treesit-parser-language
-   (car (treesit-parsers-at position))))
+
+Returns the language at POSITION, or nil if there's no parser in the
+buffer.  When there are multiple parsers that cover POSITION, use the
+parser with the deepest embed level as it's the \"most relevant\" parser
+at POSITION."
+  (let ((parser (car (treesit-parsers-at position))))
+    (when parser
+      (treesit-parser-language parser))))
 
 ;;; Node API supplement
 
@@ -854,7 +859,10 @@ by an overlay with non-nil `treesit-parser-local-p' property.
 If ONLY contains the symbol `global', include non-local parsers
 excluding the primary parser.
 
-If ONLY contains the symbol `primary', include the primary parser."
+If ONLY contains the symbol `primary', include the primary parser.
+
+The returned parsers are sorted in descending order of embed level.
+That is, the deepest embedded parser comes first."
   (let ((res nil))
     ;; Refer to (ref:local-parser-overlay) for more explanation of local
     ;; parser overlays.