nvim-treesitter/queries/c/indents.scm

130 lines
2.6 KiB
Scheme
Raw Normal View History

2021-01-05 19:26:08 +07:00
[
2023-01-10 14:04:27 +09:00
(compound_statement)
2021-01-05 19:26:08 +07:00
(field_declaration_list)
(case_statement)
(enumerator_list)
2021-07-31 12:59:19 -04:00
(compound_literal_expression)
2022-01-18 21:17:26 +06:00
(initializer_list)
2023-01-10 14:04:27 +09:00
(init_declarator)
] @indent.begin
2023-01-10 14:04:27 +09:00
; With current indent logic, if we capture expression_statement with @indent.begin
; It will be affected by _parent_ node with error subnodes deep down the tree
; So narrow indent capture to check for error inside expression statement only,
(expression_statement
(_) @indent.begin
";" @indent.end)
2023-01-10 14:04:27 +09:00
(
2023-01-12 17:16:48 +09:00
ERROR
"for" "(" @indent.begin ";" ";" ")" @indent.end)
2023-01-10 14:04:27 +09:00
(
2023-01-12 17:16:48 +09:00
(for_statement
body: (_) @_body
) @indent.begin
2023-01-12 17:16:48 +09:00
(#not-has-type? @_body compound_statement)
)
2023-01-10 14:04:27 +09:00
(
2023-01-12 17:16:48 +09:00
while_statement
condition: (_) @indent.begin
2023-01-12 17:16:48 +09:00
)
(
(while_statement
body: (_) @_body
) @indent.begin
2023-01-12 17:16:48 +09:00
(#not-has-type? @_body compound_statement)
2023-01-10 14:04:27 +09:00
)
(
2023-01-12 17:16:48 +09:00
(if_statement)
(ERROR "else") @indent.begin
2023-01-10 14:04:27 +09:00
)
2023-01-12 17:16:48 +09:00
(
if_statement
condition: (_) @indent.begin
2023-01-12 17:16:48 +09:00
)
;; Make sure all cases of if-else are tagged with @indent.begin
2023-01-12 17:16:48 +09:00
;; So we will offset the indents for the else case
2023-01-10 14:04:27 +09:00
(
(if_statement
2023-01-12 17:16:48 +09:00
consequence: (compound_statement)
"else" @indent.branch
2023-01-10 14:04:27 +09:00
alternative:
[
[ "{" "}" ] @indent.branch
(compound_statement ["{" "}"] @indent.branch)
2023-01-12 17:16:48 +09:00
(_)
2023-01-10 14:04:27 +09:00
]
) @indent.begin
2023-01-10 14:04:27 +09:00
)
(
2023-01-12 17:16:48 +09:00
(if_statement
consequence: (_ ";" @indent.end) @_consequence
) @indent.begin
2023-01-12 17:16:48 +09:00
(#not-has-type? @_consequence compound_statement)
2023-01-10 14:04:27 +09:00
)
(
2023-01-12 17:16:48 +09:00
(if_statement
consequence: (_) @_consequence
"else" @indent.branch
2023-01-12 17:16:48 +09:00
alternative:
[
[ "{" "}" ] @indent.branch
(compound_statement ["{" "}"] @indent.branch)
2023-01-12 17:16:48 +09:00
(_)
]
)
(#not-has-type? @_consequence compound_statement)
2023-01-10 14:04:27 +09:00
)
2023-01-12 17:16:48 +09:00
;; 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
2023-01-10 14:04:27 +09:00
(
2023-01-12 17:16:48 +09:00
(if_statement
consequence: (_)
alternative:
[
(if_statement consequence: (compound_statement) @indent.dedent)
2023-01-12 17:16:48 +09:00
(_)
] @indent.dedent
2023-01-12 17:16:48 +09:00
)
2023-01-10 14:04:27 +09:00
)
(compound_statement "}" @indent.end)
2022-01-22 18:24:38 +01:00
[
")"
"}"
(statement_identifier)
] @indent.branch
2022-01-22 18:24:38 +01:00
2021-01-05 19:26:08 +07:00
[
"#define"
"#ifdef"
"#ifndef"
"#elif"
2022-01-18 21:17:26 +06:00
"#if"
"#else"
2021-01-05 19:26:08 +07:00
"#endif"
] @indent.zero
[
2022-01-18 21:17:26 +06:00
(preproc_arg)
(string_literal)
] @indent.ignore
2022-01-18 21:17:26 +06:00
((ERROR (parameter_declaration)) @indent.align
(#set! indent.open_delimiter "(")
(#set! indent.close_delimiter ")"))
([(argument_list) (parameter_list)] @indent.align
(#set! indent.open_delimiter "(")
(#set! indent.close_delimiter ")"))
(comment) @indent.auto