1; Reserved keywords
2
3["when" "and" "or" "not" "in" "fn" "do" "end" "catch" "rescue" "after" "else"] @keyword
4
5; Operators
6
7; * doc string
8(unary_operator
9  operator: "@" @comment.doc
10  operand: (call
11    target: (identifier) @comment.doc.__attribute__
12    (arguments
13      [
14        (string) @comment.doc
15        (charlist) @comment.doc
16        (sigil
17          quoted_start: _ @comment.doc
18          quoted_end: _ @comment.doc) @comment.doc
19        (boolean) @comment.doc
20      ]))
21  (#match? @comment.doc.__attribute__ "^(moduledoc|typedoc|doc)$"))
22
23; * module attribute
24(unary_operator
25  operator: "@" @attribute
26  operand: [
27    (identifier) @attribute
28    (call
29      target: (identifier) @attribute)
30    (boolean) @attribute
31    (nil) @attribute
32  ])
33
34; * capture operand
35(unary_operator
36  operator: "&"
37  operand: (integer) @operator)
38
39(operator_identifier) @operator
40
41(unary_operator
42  operator: _ @operator)
43
44(binary_operator
45  operator: _ @operator)
46
47(dot
48  operator: _ @operator)
49
50(stab_clause
51  operator: _ @operator)
52
53; Literals
54
55[
56  (boolean)
57  (nil)
58] @constant
59
60[
61  (integer)
62  (float)
63] @number
64
65(alias) @type
66
67(call
68  target: (dot
69    left: (atom) @type))
70
71(char) @constant
72
73; Quoted content
74
75(interpolation "#{" @punctuation.special "}" @punctuation.special) @embedded
76
77(escape_sequence) @string.escape
78
79[
80  (atom)
81  (quoted_atom)
82  (keyword)
83  (quoted_keyword)
84] @string.special.symbol
85
86[
87  (string)
88  (charlist)
89] @string
90
91; Note that we explicitly target sigil quoted start/end, so they are not overridden by delimiters
92
93(sigil
94  (sigil_name) @__name__
95  quoted_start: _ @string
96  quoted_end: _ @string
97  (#match? @__name__ "^[sS]$")) @string
98
99(sigil
100  (sigil_name) @__name__
101  quoted_start: _ @string.regex
102  quoted_end: _ @string.regex
103  (#match? @__name__ "^[rR]$")) @string.regex
104
105(sigil
106  (sigil_name) @__name__
107  quoted_start: _ @string.special
108  quoted_end: _ @string.special) @string.special
109
110; Calls
111
112; * definition keyword
113(call
114  target: (identifier) @keyword
115  (#match? @keyword "^(def|defdelegate|defexception|defguard|defguardp|defimpl|defmacro|defmacrop|defmodule|defn|defnp|defoverridable|defp|defprotocol|defstruct)$"))
116
117; * kernel or special forms keyword
118(call
119  target: (identifier) @keyword
120  (#match? @keyword "^(alias|case|cond|else|for|if|import|quote|raise|receive|require|reraise|super|throw|try|unless|unquote|unquote_splicing|use|with)$"))
121
122; * function call
123(call
124  target: [
125    ; local
126    (identifier) @function
127    ; remote
128    (dot
129      right: (identifier) @function)
130  ])
131
132; * just identifier in function definition
133(call
134  target: (identifier) @keyword
135  (arguments
136    [
137      (identifier) @function
138      (binary_operator
139        left: (identifier) @function
140        operator: "when")
141    ])
142  (#match? @keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp)$"))
143
144; * pipe into identifier (definition)
145(call
146  target: (identifier) @keyword
147  (arguments
148    (binary_operator
149      operator: "|>"
150      right: (identifier) @variable))
151  (#match? @keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp)$"))
152
153; * pipe into identifier (function call)
154(binary_operator
155  operator: "|>"
156  right: (identifier) @function)
157
158; Identifiers
159
160; * special
161(
162  (identifier) @constant.builtin
163  (#match? @constant.builtin "^(__MODULE__|__DIR__|__ENV__|__CALLER__|__STACKTRACE__)$")
164)
165
166; * unused
167(
168  (identifier) @comment.unused
169  (#match? @comment.unused "^_")
170)
171
172; * regular
173(identifier) @variable
174
175; Comment
176
177(comment) @comment
178
179; Punctuation
180
181[
182 "%"
183] @punctuation
184
185[
186 ","
187 ";"
188] @punctuation.delimiter
189
190[
191  "("
192  ")"
193  "["
194  "]"
195  "{"
196  "}"
197  "<<"
198  ">>"
199] @punctuation.bracket
200