aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPham Huy Hoang <hoangtun0810@gmail.com>2023-01-12 17:16:48 +0900
committerStephan Seitz <stephan.seitz@fau.de>2023-01-14 04:52:21 -0800
commitf669924df2d19b73146fcb8034fbb436b38391cd (patch)
treea03ebd1dd85259b4136ac1c2e8ebaa6d8797169c
parentindents(c): Refactor indent queries (diff)
downloadnvim-treesitter-f669924df2d19b73146fcb8034fbb436b38391cd.tar
nvim-treesitter-f669924df2d19b73146fcb8034fbb436b38391cd.tar.gz
nvim-treesitter-f669924df2d19b73146fcb8034fbb436b38391cd.tar.bz2
nvim-treesitter-f669924df2d19b73146fcb8034fbb436b38391cd.tar.lz
nvim-treesitter-f669924df2d19b73146fcb8034fbb436b38391cd.tar.xz
nvim-treesitter-f669924df2d19b73146fcb8034fbb436b38391cd.tar.zst
nvim-treesitter-f669924df2d19b73146fcb8034fbb436b38391cd.zip
indents(c): update if cases
-rw-r--r--queries/c/indents.scm105
-rw-r--r--tests/indent/c_spec.lua5
2 files changed, 60 insertions, 50 deletions
diff --git a/queries/c/indents.scm b/queries/c/indents.scm
index 185ace10c..db7b3a1c9 100644
--- a/queries/c/indents.scm
+++ b/queries/c/indents.scm
@@ -10,80 +10,85 @@
] @indent
(
- if_statement
- condition: (_) @indent
-)
+ ERROR
+ "for" "(" @indent ";" ";" ")" @indent_end)
(
- (if_statement
- consequence: (_ ";" @indent_end) @_consequence
+ (for_statement
+ body: (_) @_body
) @indent
- (#not-has-type? @_consequence compound_statement)
-)
+ (#not-has-type? @_body compound_statement)
+)
-;; 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)
+ while_statement
+ condition: (_) @indent
+)
+(
+ (while_statement
+ body: (_) @_body
+ ) @indent
+ (#not-has-type? @_body compound_statement)
)
-;; if-elseif-else case
(
- if_statement
- consequence: (_) @_consequence
- alternative:
- (if_statement "else" @branch)
- (#not-has-type? @_consequence compound_statement)
+ (if_statement)
+ (ERROR "else") @indent
)
-;; 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
+ condition: (_) @indent
+)
+;; Make sure all cases of if-else are tagged with @indent
+;; So we will offset the indents for the else case
(
(if_statement
- consequence: (_ ";" @indent_end) @_consequence
+ consequence: (compound_statement)
"else" @branch
alternative:
[
- (compound_statement) @dedent
- (if_statement
- consequence: (_) @_consequence (#not-has-type? @_consequence compound_statement)
- ) @dedent
- (if_statement consequence: (compound_statement) @dedent)
+ [ "{" "}" ] @branch
+ (compound_statement ["{" "}"] @branch)
+ (_)
]
- )@indent
- (#not-has-type? @_consequence compound_statement)
+ ) @indent
)
-
-(
- ERROR
- "for" "(" @indent ";" ";" ")" @indent_end)
(
- (for_statement
- body: (_) @_body
+ (if_statement
+ consequence: (_ ";" @indent_end) @_consequence
) @indent
- (#not-has-type? @_body compound_statement)
+ (#not-has-type? @_consequence compound_statement)
)
-
(
- while_statement
- condition: (_) @indent
+ (if_statement
+ consequence: (_) @_consequence
+ "else" @branch
+ alternative:
+ [
+ [ "{" "}" ] @branch
+ (compound_statement ["{" "}"] @branch)
+ (_)
+ ]
+ )
+ (#not-has-type? @_consequence compound_statement)
)
+
+;; Dedent for chaining if-else statements
+;; this will go recursively through each if-elseif
+;; if-elseif -> second `if` is dedented once, indented twice
+;; if-elseif-elseif -> third `if` is dedented twice, indented 3 times
+;; -> all are indented once
(
- (while_statement
- body: (_) @_body
- ) @indent
- (#not-has-type? @_body compound_statement)
+ (if_statement
+ consequence: (_)
+ alternative:
+ [
+ (if_statement consequence: (compound_statement) @dedent)
+ (_)
+ ] @dedent
+ )
)
-
(compound_statement "}" @indent_end)
[
diff --git a/tests/indent/c_spec.lua b/tests/indent/c_spec.lua
index 6d5db4aab..4fcdb3ebf 100644
--- a/tests/indent/c_spec.lua
+++ b/tests/indent/c_spec.lua
@@ -41,6 +41,7 @@ describe("indent C:", function()
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 })
+ runner:new_line("issue-4117.c", { on_line = 3, text = "else", indent = 4 })
-- the line after inserted one will be left with wrong indent but we only care about the inserted one
for _, line in ipairs { 2, 4, 7, 10 } do
runner:new_line("no_braces.c", { on_line = line, text = "x++;", indent = 8 })
@@ -68,5 +69,9 @@ describe("indent C:", function()
} do
runner:new_line("if_else.c", { on_line = info[1], text = "x++;", indent = info[2] })
end
+ -- dedent braces on new line
+ for _, line in ipairs { 10, 12, 14 } do
+ runner:new_line("if_else.c", { on_line = line, text = "{}", indent = 4 })
+ end
end)
end)