summaryrefslogtreecommitdiffstats
path: root/queries/c/indents.scm
blob: cf4c8fa62be6160a891d2a8af4c6e176b2bf4088 (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
[
  (compound_statement) 
  (field_declaration_list)
  (case_statement)
  (enumerator_list)
  (compound_literal_expression)
  (initializer_list)
  (init_declarator)
] @indent.begin

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

(ERROR
  "for" "(" @indent.begin ";" ";" ")" @indent.end)

((for_statement
    body: (_) @_body) @indent.begin
  (#not-has-type? @_body compound_statement))

(while_statement
  condition: (_) @indent.begin)

((while_statement
    body: (_) @_body) @indent.begin
  (#not-has-type? @_body compound_statement))

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

(if_statement
  condition: (_) @indent.begin)

;; Supports if without braces (but not both if-else without braces)
((if_statement
  consequence: 
    (_ ";" @indent.end) @_consequence
    (#not-has-type? @_consequence compound_statement)
  alternative:
    (else_clause 
      "else" @indent.branch
      [ 
        (if_statement (compound_statement) @indent.dedent)? @indent.dedent
        (compound_statement)? @indent.dedent
        (_)? @indent.dedent
      ]
      )?
  ) @indent.begin)

(else_clause (_ . "{" @indent.branch))


(compound_statement "}" @indent.end)

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

[
  "#define"
  "#ifdef"
  "#ifndef"
  "#elif"
  "#if"
  "#else"
  "#endif"
] @indent.zero

[
  (preproc_arg)
  (string_literal)
] @indent.ignore

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