From b01a9db62ade248dcf160dca0162dce9ec2c4587 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Thu, 17 Apr 2025 16:56:07 -0700 Subject: [PATCH] Add guard in treesit-language-at-point-default (bug#77870) * 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 | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lisp/treesit.el b/lisp/treesit.el index 5311cbffef5..841dd82f1ac 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -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. -- 2.39.5