From f9791700fa61b4170bf1d0430f08aa0db95b95ee Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Fri, 29 Nov 2024 13:45:24 -0800 Subject: [PATCH] Refactor treesit--indent-1 * lisp/treesit.el (treesit--indent-largest-node-at): New function. (treesit--indent-1): Extract code out into the new function. (cherry picked from commit 1d425125694b61e2b07a7b429a6475dfe59afe37) --- lisp/treesit.el | 57 ++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/lisp/treesit.el b/lisp/treesit.el index 55b6349139d..d3413d1590f 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1939,41 +1939,44 @@ PARENT is its parent; ANCHOR is a point (not a node), and OFFSET is a number. Emacs finds the column of ANCHOR and adds OFFSET to it as the final indentation of the current line.") -(defun treesit--indent-1 () - "Indent the current line. -Return (ANCHOR . OFFSET). This function is used by -`treesit-indent' and `treesit-indent-region'." - ;; Basically holds the common part between the two indent function. - (let* ((bol (save-excursion - (forward-line 0) - (skip-chars-forward " \t") - (point))) - (local-parsers (treesit-local-parsers-at bol nil t)) +(defun treesit--indent-largest-node-at (pos) + "Get largest node that still starts at POS." + (let* ((local-parsers (treesit-local-parsers-at pos nil t)) (smallest-node (cond ((car local-parsers) (let ((local-parser (caar local-parsers)) (host-parser (cdar local-parsers))) (if (eq (treesit-node-start (treesit-parser-root-node local-parser)) - bol) - (treesit-node-at bol host-parser) - (treesit-node-at bol local-parser)))) + pos) + (treesit-node-at pos host-parser) + (treesit-node-at pos local-parser)))) ((null (treesit-parser-list)) nil) ((eq 1 (length (treesit-parser-list nil nil t))) - (treesit-node-at bol)) - ((treesit-language-at bol) - (treesit-node-at bol (treesit-language-at bol))) - (t (treesit-node-at bol)))) + (treesit-node-at pos)) + ((treesit-language-at pos) + (treesit-node-at pos (treesit-language-at pos))) + (t (treesit-node-at pos)))) (root (treesit-parser-root-node - (treesit-node-parser smallest-node))) - (node (treesit-parent-while - smallest-node - (lambda (node) - (and (eq bol (treesit-node-start node)) - (not (treesit-node-eq node root))))))) - (let* - ((parser (if smallest-node - (treesit-node-parser smallest-node) + (treesit-node-parser smallest-node)))) + (treesit-parent-while + smallest-node + (lambda (node) + (and (eq pos (treesit-node-start node)) + (not (treesit-node-eq node root))))))) + +(defun treesit--indent-1 () + "Indent the current line. +Return (ANCHOR . OFFSET). This function is used by +`treesit-indent' and `treesit-indent-region'." + ;; Basically holds the common part between the two indent function. + (let* ((bol (save-excursion + (forward-line 0) + (skip-chars-forward " \t") + (point))) + (node (treesit--indent-largest-node-at bol)) + (parser (if node + (treesit-node-parser node) nil)) ;; NODE would be nil if BOL is on a whitespace. In that case ;; we set PARENT to the "node at point", which would @@ -1981,7 +1984,7 @@ Return (ANCHOR . OFFSET). This function is used by (parent (cond ((and node parser) (treesit-node-parent node)) (t (treesit-node-on bol bol))))) - (funcall treesit-indent-function node parent bol)))) + (funcall treesit-indent-function node parent bol))) (defun treesit-indent () "Indent according to the result of `treesit-indent-function'." -- 2.39.2