summaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorYuan Fu <casouri@gmail.com>2023-03-01 13:55:53 -0800
committerYuan Fu <casouri@gmail.com>2023-03-01 14:07:01 -0800
commit6b2720778dc9531c0157bc7e773d2011bdf905e3 (patch)
tree795ff38380df607c1a6c06b7699098d1e6f189f2 /lisp
parentdb50545e3e9cd51dd8e6ad93034dc48d45b705e2 (diff)
downloademacs-6b2720778dc9531c0157bc7e773d2011bdf905e3.tar.gz
emacs-6b2720778dc9531c0157bc7e773d2011bdf905e3.tar.bz2
emacs-6b2720778dc9531c0157bc7e773d2011bdf905e3.zip
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.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/progmodes/c-ts-mode.el12
-rw-r--r--lisp/treesit.el12
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