Skip to content

Commit 3012809

Browse files
committed
Add highlight rules
1 parent 18f6c6c commit 3012809

File tree

4 files changed

+220
-14
lines changed

4 files changed

+220
-14
lines changed

grammar.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,10 +812,6 @@ function sep1(rule, separator) {
812812
return seq(rule, repeat(seq(separator, rule)));
813813
}
814814

815-
function sep(rule, separator) {
816-
return optional(sep1(rule, separator));
817-
}
818-
819815
function unaryOp($, assoc, precedence, operator, right = null) {
820816
// Expression such as `x + y` falls under the "expression vs local call"
821817
// conflict that we already have. By using dynamic precedence we penalize

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
"version": "0.19.0",
44
"description": "Elixir grammar for tree-sitter",
55
"main": "bindings/node",
6-
"keywords": ["parser", "lexer", "elixir", "tree-sitter"],
6+
"keywords": [
7+
"parser",
8+
"lexer",
9+
"elixir",
10+
"tree-sitter"
11+
],
712
"scripts": {
813
"test": "tree-sitter test",
914
"format": "prettier --trailing-comma es5 --write grammar.js",
@@ -16,6 +21,16 @@
1621
},
1722
"devDependencies": {
1823
"prettier": "^2.3.2",
19-
"tree-sitter-cli": "^0.20.0"
20-
}
24+
"tree-sitter-cli": "^0.19.5"
25+
},
26+
"tree-sitter": [
27+
{
28+
"scope": "source.elixir",
29+
"file-types": [
30+
"ex",
31+
"exs"
32+
],
33+
"injection-regex": "^(ex|elixir)$"
34+
}
35+
]
2136
}

queries/highlights.scm

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
(char) @constant
68+
69+
; Quoted content
70+
71+
(interpolation "#{" @punctuation.special "}" @punctuation.special) @embedded
72+
73+
(escape_sequence) @string.escape
74+
75+
[
76+
(atom)
77+
(quoted_atom)
78+
(keyword)
79+
(quoted_keyword)
80+
] @string.special.symbol
81+
82+
[
83+
(string)
84+
(charlist)
85+
] @string
86+
87+
; Note that we explicitly target sigil quoted start/end, so they are not overridden by delimiters
88+
89+
(sigil
90+
(sigil_name) @__name__
91+
quoted_start: _ @string
92+
quoted_end: _ @string
93+
(#match? @__name__ "^[sS]$")) @string
94+
95+
(sigil
96+
(sigil_name) @__name__
97+
quoted_start: _ @string.regex
98+
quoted_end: _ @string.regex
99+
(#match? @__name__ "^[rR]$")) @string.regex
100+
101+
(sigil
102+
(sigil_name) @__name__
103+
quoted_start: _ @string.special
104+
quoted_end: _ @string.special) @string.special
105+
106+
; Calls
107+
108+
; * definition keyword
109+
(call
110+
target: (identifier) @keyword
111+
(#match? @keyword "^(def|defdelegate|defexception|defguard|defguardp|defimpl|defmacro|defmacrop|defmodule|defn|defnp|defoverridable|defp|defprotocol|defstruct)$"))
112+
113+
; * kernel or special forms keyword
114+
(call
115+
target: (identifier) @keyword
116+
(#match? @keyword "^(alias|case|cond|else|for|if|import|quote|raise|receive|require|reraise|super|throw|try|unless|unquote|unquote_splicing|use|with)$"))
117+
118+
; * function call
119+
(call
120+
target: [
121+
; local
122+
(identifier) @function
123+
; remote
124+
(dot
125+
right: (identifier) @function)
126+
])
127+
128+
; * just identifier in function definition
129+
(call
130+
target: (identifier) @keyword
131+
(arguments
132+
[
133+
(identifier) @function
134+
(binary_operator
135+
left: (identifier) @function
136+
operator: "when")
137+
])
138+
(#match? @keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp)$"))
139+
140+
; * pipe into identifier (definition)
141+
(call
142+
target: (identifier) @keyword
143+
(arguments
144+
(binary_operator
145+
operator: "|>"
146+
right: (identifier) @variable))
147+
(#match? @keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp)$"))
148+
149+
; * pipe into identifier (function call)
150+
(binary_operator
151+
operator: "|>"
152+
right: (identifier) @function)
153+
154+
; Identifiers
155+
156+
; * special
157+
(
158+
(identifier) @constant.builtin
159+
(#match? @constant.builtin "^(__MODULE__|__DIR__|__ENV__|__CALLER__|__STACKTRACE__)$")
160+
)
161+
162+
; * unused
163+
(
164+
(identifier) @comment.unused
165+
(#match? @comment.unused "^_")
166+
)
167+
168+
; * regular
169+
(identifier) @variable
170+
171+
; Comment
172+
173+
(comment) @comment
174+
175+
; Punctuation
176+
177+
[
178+
"%"
179+
] @punctuation
180+
181+
[
182+
","
183+
";"
184+
] @punctuation.delimiter
185+
186+
[
187+
"("
188+
")"
189+
"["
190+
"]"
191+
"{"
192+
"}"
193+
"<<"
194+
">>"
195+
] @punctuation.bracket

0 commit comments

Comments
 (0)