mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
indents(c): Refactor indent queries
This commit is contained in:
parent
5ac2c92060
commit
2f5deed456
6 changed files with 179 additions and 15 deletions
|
|
@ -1,28 +1,92 @@
|
|||
[
|
||||
(compound_statement)
|
||||
(preproc_arg)
|
||||
(compound_statement)
|
||||
(field_declaration_list)
|
||||
(case_statement)
|
||||
(enumerator_list)
|
||||
(struct_specifier)
|
||||
(compound_literal_expression)
|
||||
(initializer_list)
|
||||
(while_statement)
|
||||
(for_statement)
|
||||
(switch_statement)
|
||||
(init_declarator)
|
||||
(expression_statement)
|
||||
] @indent
|
||||
(if_statement condition: (_) @indent)
|
||||
((if_statement
|
||||
consequence: (_) @_consequence
|
||||
|
||||
(
|
||||
if_statement
|
||||
condition: (_) @indent
|
||||
)
|
||||
(
|
||||
(if_statement
|
||||
consequence: (_ ";" @indent_end) @_consequence
|
||||
) @indent
|
||||
(#not-has-type? @_consequence compound_statement)
|
||||
) @indent)
|
||||
(init_declarator) @indent
|
||||
)
|
||||
|
||||
;; if-else case,
|
||||
;; with else that is not if/compound statement
|
||||
(
|
||||
(if_statement
|
||||
consequence: (_ ";" @indent_end) @_consequence
|
||||
"else" @branch
|
||||
alternative: (_) @_alternative
|
||||
)@indent
|
||||
(#not-has-type? @_consequence compound_statement)
|
||||
(#not-has-type? @_alternative compound_statement)
|
||||
)
|
||||
|
||||
;; if-elseif-else case
|
||||
(
|
||||
if_statement
|
||||
consequence: (_) @_consequence
|
||||
alternative:
|
||||
(if_statement "else" @branch)
|
||||
(#not-has-type? @_consequence compound_statement)
|
||||
)
|
||||
|
||||
;; if- else case
|
||||
;; The alternative should be one of the cases below
|
||||
;; because the alternative is a nested tree inside the original if
|
||||
;; dedent to offset the indent it should have gotten
|
||||
;; Does not support multi chained bare ifs
|
||||
(
|
||||
(if_statement
|
||||
consequence: (_ ";" @indent_end) @_consequence
|
||||
"else" @branch
|
||||
alternative:
|
||||
[
|
||||
(compound_statement) @dedent
|
||||
(if_statement
|
||||
consequence: (_) @_consequence (#not-has-type? @_consequence compound_statement)
|
||||
) @dedent
|
||||
(if_statement consequence: (compound_statement) @dedent)
|
||||
]
|
||||
)@indent
|
||||
(#not-has-type? @_consequence compound_statement)
|
||||
)
|
||||
|
||||
(
|
||||
ERROR
|
||||
"for" "(" @indent ";" ";" ")" @indent_end)
|
||||
(
|
||||
(for_statement
|
||||
body: (_) @_body
|
||||
) @indent
|
||||
(#not-has-type? @_body compound_statement)
|
||||
)
|
||||
|
||||
(
|
||||
while_statement
|
||||
condition: (_) @indent
|
||||
)
|
||||
(
|
||||
(while_statement
|
||||
body: (_) @_body
|
||||
) @indent
|
||||
(#not-has-type? @_body compound_statement)
|
||||
)
|
||||
|
||||
|
||||
(compound_statement "}" @indent_end)
|
||||
|
||||
[
|
||||
"else"
|
||||
")"
|
||||
"}"
|
||||
(statement_identifier)
|
||||
|
|
|
|||
60
tests/indent/c/if_else.c
Normal file
60
tests/indent/c/if_else.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
int foo(int x){
|
||||
if (x > 10)
|
||||
return 10;
|
||||
|
||||
if (x > 10)
|
||||
return 10;
|
||||
else
|
||||
return 10;
|
||||
|
||||
if (x > 20)
|
||||
return 20;
|
||||
else if (x > 15)
|
||||
return 15;
|
||||
else
|
||||
return 10;
|
||||
}
|
||||
|
||||
int bar(int x){
|
||||
if (x > 20)
|
||||
return 10;
|
||||
else {
|
||||
return 10;
|
||||
}
|
||||
|
||||
if (x > 20)
|
||||
return 10;
|
||||
else if (x > 10) {
|
||||
return 10;
|
||||
}
|
||||
}
|
||||
|
||||
int baz(int x){
|
||||
if (x > 20)
|
||||
return x;
|
||||
else if(x > 10) {
|
||||
if(x > 10) {
|
||||
if(x > 10)
|
||||
return 10;
|
||||
if(x > 5) {
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (x > 20)
|
||||
if (x > 19)
|
||||
if(x > 18)
|
||||
return x;
|
||||
|
||||
if (x > 20)
|
||||
return x;
|
||||
else if (x > 19) {
|
||||
if (x > 18)
|
||||
return x;
|
||||
else
|
||||
x++;
|
||||
}
|
||||
else
|
||||
return x;
|
||||
}
|
||||
7
tests/indent/c/issue-4079.c
Normal file
7
tests/indent/c/issue-4079.c
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
int main(){
|
||||
for(;;)
|
||||
}
|
||||
|
||||
int foo(){
|
||||
while(1)
|
||||
}
|
||||
6
tests/indent/c/issue-4117.c
Normal file
6
tests/indent/c/issue-4117.c
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
int main(){
|
||||
if(1){
|
||||
}
|
||||
else {
|
||||
}
|
||||
}
|
||||
|
|
@ -10,3 +10,4 @@ int foo(int x) {
|
|||
for (int i = 0; i < 3; ++i)
|
||||
x--;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -38,9 +38,35 @@ describe("indent C:", function()
|
|||
runner:new_line("ternary.c", { on_line = 4, text = ": (x == 0) : 0", indent = 8 })
|
||||
runner:new_line("issue-1568.c", { on_line = 4, text = "x++;", indent = 8 })
|
||||
runner:new_line("issue-2086.c", { on_line = 3, text = "}", indent = 0 })
|
||||
runner:new_line("issue-4079.c", { on_line = 2, text = "return;", indent = 8 })
|
||||
runner:new_line("issue-4079.c", { on_line = 2, text = "{", indent = 4 })
|
||||
runner:new_line("issue-4079.c", { on_line = 6, text = "{", indent = 4 })
|
||||
-- the line after inserted one will be left with wrong indent but we only care about the inserted one
|
||||
runner:new_line("no_braces.c", { on_line = 4, text = "x++;", indent = 8 })
|
||||
runner:new_line("no_braces.c", { on_line = 7, text = "x++;", indent = 8 })
|
||||
runner:new_line("no_braces.c", { on_line = 10, text = "x++;", indent = 8 })
|
||||
for _, line in ipairs { 2, 4, 7, 10 } do
|
||||
runner:new_line("no_braces.c", { on_line = line, text = "x++;", indent = 8 })
|
||||
end
|
||||
for _, line in ipairs { 2, 5, 7, 10, 12, 14, 20, 22, 25, 27, 28, 33 } do
|
||||
runner:new_line("if_else.c", { on_line = line, text = "x++;", indent = 8 })
|
||||
end
|
||||
for _, line in ipairs { 3, 6, 8, 13, 15, 20, 23 } do
|
||||
runner:new_line("if_else.c", { on_line = line, text = "else", indent = 4 })
|
||||
end
|
||||
for _, info in ipairs {
|
||||
{ 36, 12 },
|
||||
{ 37, 16 },
|
||||
{ 38, 12 },
|
||||
{ 39, 16 },
|
||||
{ 41, 12 },
|
||||
{ 42, 8 },
|
||||
{ 45, 8 },
|
||||
{ 46, 12 },
|
||||
{ 47, 16 },
|
||||
{ 48, 4 },
|
||||
{ 52, 8 },
|
||||
{ 53, 12 },
|
||||
{ 54, 12 },
|
||||
} do
|
||||
runner:new_line("if_else.c", { on_line = info[1], text = "x++;", indent = info[2] })
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue