[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 1d425125694 02/11: Refactor treesit--indent-1
From: |
Yuan Fu |
Subject: |
master 1d425125694 02/11: Refactor treesit--indent-1 |
Date: |
Sun, 1 Dec 2024 21:10:22 -0500 (EST) |
branch: master
commit 1d425125694b61e2b07a7b429a6475dfe59afe37
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Refactor treesit--indent-1
* lisp/treesit.el (treesit--indent-largest-node-at): New
function.
(treesit--indent-1): Extract code out into the new function.
---
lisp/treesit.el | 57 ++++++++++++++++++++++++++++++---------------------------
1 file changed, 30 insertions(+), 27 deletions(-)
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 6f5d065cc24..5d57b9e8e3e 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1943,41 +1943,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
@@ -1985,7 +1988,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'."
- master updated (4afd1eca366 -> de98b5a24f2), Yuan Fu, 2024/12/01
- master c65c5d02224 01/11: Improve c-ts-mode compound indents (bug#74507), Yuan Fu, 2024/12/01
- master d0b918d8f3c 04/11: Add treesit-explore command, Yuan Fu, 2024/12/01
- master 4afdb7e80fe 05/11: ; Minor simplification for tree-sitter indent preset column-0, Yuan Fu, 2024/12/01
- master 63d69bd1549 07/11: Use new baseline indent rule in c-ts-mode, Yuan Fu, 2024/12/01
- master 9acf6eff01a 10/11: Standardize and promote c-ts-mode's custom matcher and anchor, Yuan Fu, 2024/12/01
- master de98b5a24f2 11/11: ; Indent by 8 in BSD indent tests for c-ts-mode, Yuan Fu, 2024/12/01
- master 1e44c63fcad 03/11: Allow treesit-simple-indent's rule to be a single function, Yuan Fu, 2024/12/01
- master 1d425125694 02/11: Refactor treesit--indent-1,
Yuan Fu <=
- master e37cd4fa597 06/11: Add baseline tree-sitter indent rule for C-like languages, Yuan Fu, 2024/12/01
- master 44fcd37a486 08/11: Add more c-ts-mode indent tests, Yuan Fu, 2024/12/01
- master 994258f5567 09/11: Remove unused functions in c-ts-mode, Yuan Fu, 2024/12/01