aboutsummaryrefslogtreecommitdiffstats
path: root/queries/c/indents.scm
blob: db7b3a1c92d62d214e30b4a5c1e191b0e61c6f25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
[
  (compound_statement) 
  (field_declaration_list)
  (case_statement)
  (enumerator_list)
  (compound_literal_expression)
  (initializer_list)
  (init_declarator)
  (expression_statement)
] @indent

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

(
  (if_statement)
  (ERROR "else") @indent
)

(
 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: (compound_statement)
    "else" @branch
    alternative: 
      [
        [ "{" "}" ] @branch
        (compound_statement ["{" "}"] @branch)
        (_)
      ] 
  ) @indent
)
(
  (if_statement
    consequence: (_ ";" @indent_end) @_consequence
  ) @indent
  (#not-has-type? @_consequence compound_statement)
)
(
  (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
(
  (if_statement
    consequence: (_)
    alternative: 
      [
        (if_statement consequence: (compound_statement) @dedent)
        (_)
      ] @dedent
  )
)

(compound_statement "}" @indent_end)

[
  ")"
  "}"
  (statement_identifier)
] @branch

[
  "#define"
  "#ifdef"
  "#ifndef"
  "#elif"
  "#if"
  "#else"
  "#endif"
] @zero_indent

[
  (preproc_arg)
  (string_literal)
] @ignore

((ERROR (parameter_declaration)) @aligned_indent
 (#set! "delimiter" "()"))
([(argument_list) (parameter_list)] @aligned_indent
  (#set! "delimiter" "()"))

(comment) @auto