Merge from origin/emacs-29

59365f9285 * lisp/progmodes/go-ts-mode.el: Use treesit-language-avai...
56cd810b9d Don’t signal warning when loading go-ts-mode.el without g...
b06d551966 Fix c-ts-mode empty line indentation (bug#61893)
6b2720778d Improve tree-sitter's prev-sibling indent anchor
This commit is contained in:
Stefan Kangas 2023-03-02 06:30:15 +01:00
commit a798a29f75
4 changed files with 49 additions and 18 deletions

View file

@ -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.

View file

@ -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)

View file

@ -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

View file

@ -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;
|
}
=-=-=