diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index abe929bd777..70415f69fbe 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))) @@ -350,17 +356,17 @@ MODE is either `c' or `cpp'." ;; int[5] a = { 0, 0, 0, 0 }; ((match nil "initializer_list" nil 1 1) parent-bol c-ts-mode-indent-offset) - ((match nil "initializer_list" nil 2) c-ts-mode--anchor-prev-sibling 0) + ((parent-is "initializer_list") c-ts-mode--anchor-prev-sibling 0) ;; Statement in enum. ((match nil "enumerator_list" nil 1 1) standalone-parent c-ts-mode-indent-offset) - ((match nil "enumerator_list" nil 2) c-ts-mode--anchor-prev-sibling 0) + ((parent-is "enumerator_list") c-ts-mode--anchor-prev-sibling 0) ;; Statement in struct and union. ((match nil "field_declaration_list" nil 1 1) standalone-parent c-ts-mode-indent-offset) - ((match nil "field_declaration_list" nil 2) c-ts-mode--anchor-prev-sibling 0) + ((parent-is "field_declaration_list") c-ts-mode--anchor-prev-sibling 0) ;; Statement in {} blocks. ((match nil "compound_statement" nil 1 1) standalone-parent c-ts-mode-indent-offset) - ((match nil "compound_statement" nil 2) c-ts-mode--anchor-prev-sibling 0) + ((parent-is "compound_statement") c-ts-mode--anchor-prev-sibling 0) ;; Opening bracket. ((node-is "compound_statement") standalone-parent c-ts-mode-indent-offset) ;; Bug#61291. diff --git a/lisp/progmodes/go-ts-mode.el b/lisp/progmodes/go-ts-mode.el index ce77cc3973d..9a02c15aa00 100644 --- a/lisp/progmodes/go-ts-mode.el +++ b/lisp/progmodes/go-ts-mode.el @@ -427,7 +427,7 @@ what the parent of the node would be if it were a node." (treesit-major-mode-setup))) -(if (treesit-ready-p 'gomod) +(if (treesit-language-available-p 'gomod) (add-to-list 'auto-mode-alist '("/go\\.mod\\'" . go-mod-ts-mode))) (provide 'go-ts-mode) diff --git a/lisp/treesit.el b/lisp/treesit.el index 9491658de2c..e7a8ad6104f 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 diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent.erts b/test/lisp/progmodes/c-ts-mode-resources/indent.erts index 36d7af4faf1..904c6498cb5 100644 --- a/test/lisp/progmodes/c-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/c-ts-mode-resources/indent.erts @@ -84,14 +84,6 @@ int main() } =-=-= -Name: Empty Line -=-= -int main() -{ - | -} -=-=-= - Name: Concecutive blocks (GNU Style) (bug#60873) =-= @@ -385,3 +377,28 @@ namespace test { }; } =-=-= + +Code: + (lambda () + (c-ts-mode) + (setq-local indent-tabs-mode nil) + (setq-local c-ts-mode-indent-offset 2) + (c-ts-mode-set-style 'gnu) + (indent-for-tab-command)) + +Name: Empty Line +=-= +int main() +{ + | +} +=-=-= + +Name: Empty Line Previous Sibling +=-= +int main() +{ + int a = 1; + | +} +=-=-=