[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
emacs-29 6b2720778dc 1/2: Improve tree-sitter's prev-sibling indent anch
From: |
Yuan Fu |
Subject: |
emacs-29 6b2720778dc 1/2: Improve tree-sitter's prev-sibling indent anchor |
Date: |
Wed, 1 Mar 2023 17:08:04 -0500 (EST) |
branch: emacs-29
commit 6b2720778dc9531c0157bc7e773d2011bdf905e3
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Improve tree-sitter's prev-sibling indent anchor
Now it handles the case where NODE is nil when indenting an empty
line: it tries to get the previous sibling nonetheless.
* lisp/progmodes/c-ts-mode.el (c-ts-mode--anchor-prev-sibling):
* lisp/treesit.el (treesit-simple-indent-presets): Add an or form to
handle more cases.
---
lisp/progmodes/c-ts-mode.el | 12 +++++++++---
lisp/treesit.el | 12 ++++++++++--
2 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 53f7839e4af..cc99b8e213e 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -257,7 +257,7 @@ is actually the parent of point at the moment of
indentation."
0
c-ts-mode-indent-offset)))
-(defun c-ts-mode--anchor-prev-sibling (node &rest _)
+(defun c-ts-mode--anchor-prev-sibling (node parent bol &rest _)
"Return the start of the previous named sibling of NODE.
This anchor handles the special case where the previous sibling
@@ -273,8 +273,14 @@ The anchor of \"int y = 2;\" should be \"int x = 1;\"
rather than
the labeled_statement.
Return nil if a) there is no prev-sibling, or 2) prev-sibling
-doesn't have a child."
- (when-let ((prev-sibling (treesit-node-prev-sibling node t)))
+doesn't have a child.
+
+PARENT and BOL are like other anchor functions."
+ (when-let ((prev-sibling
+ (or (treesit-node-prev-sibling node t)
+ (treesit-node-prev-sibling
+ (treesit-node-first-child-for-pos parent bol) t)
+ (treesit-node-child parent -1 t))))
(while (and prev-sibling
(equal "labeled_statement"
(treesit-node-type prev-sibling)))
diff --git a/lisp/treesit.el b/lisp/treesit.el
index fe9ed399773..dbd102d00b3 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -1237,9 +1237,17 @@ See `treesit-simple-indent-presets'.")
(line-beginning-position))
(throw 'term (point)))
(setq parent (treesit-node-parent parent)))))))
- (cons 'prev-sibling (lambda (node &rest _)
+ (cons 'prev-sibling (lambda (node parent bol &rest _)
(treesit-node-start
- (treesit-node-prev-sibling node))))
+ (or (treesit-node-prev-sibling node t)
+ ;; If node is nil (indenting empty
+ ;; line), we still try to guess the
+ ;; previous sibling.
+ (treesit-node-prev-sibling
+ (treesit-node-first-child-for-pos
+ parent bol)
+ t)
+ (treesit-node-child parent -1 t)))))
(cons 'no-indent (lambda (_n _p bol &rest _) bol))
(cons 'prev-line (lambda (_n _p bol &rest _)
(save-excursion