From 77bf9d33ee42d90e86d9cc5d3a67356b19ca2f6b Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Wed, 16 Apr 2025 20:00:56 +0300 Subject: [PATCH] Add 'treesit-language-at-point-default' (bug#77256). * lisp/treesit.el (treesit-language-at-point-function): Change the default value from nil to 'treesit-language-at-point-default'. (treesit-language-at): Funcall 'treesit-language-at-point-function' unconditionally. (treesit-language-at-point-default): New function with body from 'treesit-language-at'. (treesit-node-at): Simplify by replacing duplicate code with the call to 'treesit-parsers-at'. --- lisp/treesit.el | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/lisp/treesit.el b/lisp/treesit.el index 5a408b09507..3cf3be5122c 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -165,7 +165,8 @@ of max unsigned 32-bit value for byte offsets into buffer text." The primary parser should be a parser that parses the entire buffer, as opposed to embedded parsers which parses only part of the buffer.") -(defvar-local treesit-language-at-point-function nil +(defvar-local treesit-language-at-point-function + #'treesit-language-at-point-default "A function that returns the language at point. This is used by `treesit-language-at', which is used by various functions to determine which parser to use at point. @@ -183,10 +184,15 @@ When there are multiple parsers that covers POSITION, determine the most relevant parser (hence language) by their embed level. If `treesit-language-at-point-function' is non-nil, return the return value of that function instead." - (if treesit-language-at-point-function - (funcall treesit-language-at-point-function position) - (treesit-parser-language - (car (treesit-parsers-at position))))) + (funcall (or treesit-language-at-point-function + #'treesit-language-at-point-default) + position)) + +(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)))) ;;; Node API supplement @@ -238,12 +244,8 @@ language and doesn't match the language of the local parser." ;; 2. Given a language, try local parser, then global ;; parser. (parser-or-lang - (let* ((local-parser (car (treesit-local-parsers-at - pos parser-or-lang))) - (global-parser (car (treesit-parsers-at - pos parser-or-lang nil - '(primary global)))) - (parser (or local-parser global-parser))) + (let ((parser (car (treesit-parsers-at + pos parser-or-lang)))) (when parser (treesit-parser-root-node parser)))) ;; 3. No given language, try to get a language at point. @@ -252,20 +254,8 @@ 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. 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 (treesit-local-parsers-at pos lang))) - (global-parser (car (treesit-parsers-at - pos lang nil - '(primary global)))) - (parser (or local-parser global-parser))) + ;; LANG can be nil. Use the parser deepest by embed level. + (let ((parser (car (treesit-parsers-at pos)))) (when parser (treesit-parser-root-node parser)))))) (node root)