mirror of
git://git.sv.gnu.org/emacs.git
synced 2026-02-16 17:24:23 +00:00
Improve c-ts-mode compound indents (bug#74507)
Properly indent the body of compound expressions, even when then
compound expression is not at the beginning of line and the
parent is not an if/for/while/etc., and matches the behavior of
c-mode.
This fixes a problem that is common with macros and in testing
frameworks. For example, you expect this to indent:
TEST_CASE(1) {
assert (...);
}
If the compound statement is the function body itself, don't
apply this new rule and instead guide by the parent and first
sibling.
I'm sure there are subtle interactions that aren't handled
properly by checking for "function_definition" rather than
something more general, but it does fix the test case and the
check can be improved as more cases are found.
* lisp/progmodes/c-ts-mode.el:
(c-ts-mode--parent-is-not-top-compound): New function.
(c-ts-mode--indent-styles): Use it.
* test/lisp/progmodes/c-ts-mode-resources/indent.erts: New
compound statement test.
This commit is contained in:
parent
4afd1eca36
commit
c65c5d0222
2 changed files with 39 additions and 0 deletions
|
|
@ -403,6 +403,12 @@ PARENT is its parent."
|
|||
(treesit-node-start parent)
|
||||
(line-end-position))))))
|
||||
|
||||
(defun c-ts-mode--parent-is-not-top-compound (_n parent &rest _)
|
||||
"Matches when PARENT is not the top level compound statement.
|
||||
The top-level compound is the {} that immediately follows the function
|
||||
signature."
|
||||
(not (equal "function_definition" (treesit-node-type (treesit-node-parent parent)))))
|
||||
|
||||
(defun c-ts-mode--indent-styles (mode)
|
||||
"Indent rules supported by `c-ts-mode'.
|
||||
MODE is either `c' or `cpp'."
|
||||
|
|
@ -479,6 +485,7 @@ MODE is either `c' or `cpp'."
|
|||
;; Closing bracket. This should be before initializer_list
|
||||
;; (and probably others) rule because that rule (and other
|
||||
;; similar rules) will match the closing bracket. (Bug#61398)
|
||||
((and (node-is "}") c-ts-mode--parent-is-not-top-compound) parent-bol 0)
|
||||
((node-is "}") standalone-parent 0)
|
||||
,@(when (eq mode 'cpp)
|
||||
'(((node-is "access_specifier") parent-bol 0)
|
||||
|
|
@ -498,6 +505,8 @@ MODE is either `c' or `cpp'."
|
|||
((parent-is "field_declaration_list") c-ts-mode--anchor-prev-sibling 0)
|
||||
|
||||
;; Statement in {} blocks.
|
||||
((and (parent-is "compound_statement") c-ts-mode--parent-is-not-top-compound)
|
||||
parent-bol c-ts-mode-indent-offset)
|
||||
((or (and (parent-is "compound_statement")
|
||||
;; If the previous sibling(s) are not on their
|
||||
;; own line, indent as if this node is the first
|
||||
|
|
|
|||
|
|
@ -223,6 +223,36 @@ make_pair(int long_identifier_a[], int long_identifier_b[],
|
|||
|
||||
=-=-=
|
||||
|
||||
Name: Compound Statement after code
|
||||
|
||||
=-=
|
||||
#define IOTA(var, n) for (int var = 0; var != (n); ++var)
|
||||
int main()
|
||||
{
|
||||
IOTA (v, 10) {
|
||||
printf("%d ", v);
|
||||
}
|
||||
|
||||
const char *msg = "Hello, world!"; {
|
||||
puts("Hello, world!");
|
||||
}
|
||||
}
|
||||
|
||||
=-=
|
||||
#define IOTA(var, n) for (int var = 0; var != (n); ++var)
|
||||
int main()
|
||||
{
|
||||
IOTA (v, 10) {
|
||||
printf("%d ", v);
|
||||
}
|
||||
|
||||
const char *msg = "Hello, world!"; {
|
||||
puts("Hello, world!");
|
||||
}
|
||||
}
|
||||
|
||||
=-=-=
|
||||
|
||||
Name: Switch-Case statement
|
||||
|
||||
=-=
|
||||
|
|
|
|||
Loading…
Reference in a new issue