on master help sign in
Rewritten Rust `highlights.scm` (#425)

* rewrote Rust highlights.scm * wip * wip * wip * wip * fixed type highlighting * wip * rewrite again * moved operators * missing newline * missing newline * update book * fix constructor highlighting * fix constructor highlighting * fix const highlighting * better constructor highlighting * remove dup, bug was my locals.scm file * fixed docs * merge * fixed for highlighting * add yield * remove yield * added yield back * fixed yield highlighting * unecessary

commit084a8a952292521f1f7b7f4144e6823c29b85507 authorKirawi <67773714+kirawi@users.noreply.github.com> dateThu 8 Jul 2021, 12:11 (5 years ago) parentc7aa7bf
3 files changed+306−183
book/src/themes.md+1 helix-core/src/syntax.rs+2−2 runtime/queries/rust/highlights.scm+303−181
book/src/themes.md+1
@@ -51,6 +51,7 @@ Possible keys:
5151| `attribute` | |
5252| `keyword` | |
5353| `keyword.directive` | Preprocessor directives (\#if in C) |
54+| `keyword.control` | Control flow |
5455| `namespace` | |
5556| `punctuation` | |
5657| `punctuation.delimiter` | |
helix-core/src/syntax.rs+2−2
@@ -161,7 +161,7 @@ impl LanguageConfiguration {
161161
162162 let injections_query = read_query(&language, "injections.scm");
163163
164− let locals_query = "";
164+ let locals_query = read_query(&language, "locals.scm");
165165
166166 if highlights_query.is_empty() {
167167 None
@@ -171,7 +171,7 @@ impl LanguageConfiguration {
171171 language,
172172 &highlights_query,
173173 &injections_query,
174− locals_query,
174+ &locals_query,
175175 )
176176 .unwrap(); // TODO: no unwrap
177177 config.configure(scopes);
runtime/queries/rust/highlights.scm+303−181
@@ -1,214 +1,336 @@
1−; Identifier conventions
1+; -------
2+; Tree-Sitter doesn't allow overrides in regards to captures,
3+; though it is possible to affect the child node of a captured
4+; node. Thus, the approach here is to flip the order so that
5+; overrides are unnecessary.
6+; -------
27
38
4−; Assume all-caps names are constants
5−((identifier) @constant
6− (#match? @constant "^[A-Z][A-Z\\d_]+$"))
79
8−; Assume other uppercase names are enum constructors
9−(enum_variant) @type.enum.variant
10−((identifier) @constructor
11− (#match? @constructor "^[A-Z]"))
10+; -------
11+; Types
12+; -------
1213
13−; Assume that uppercase names in paths are types
14−(mod_item
15− name: (identifier) @namespace)
16−(scoped_identifier
17− path: (identifier) @namespace)
18−(scoped_identifier
19− (scoped_identifier
20− name: (identifier) @namespace))
21−(scoped_type_identifier
22− path: (identifier) @namespace)
23−(scoped_type_identifier
24− (scoped_identifier
25− name: (identifier) @namespace))
14+; ---
15+; Primitives
16+; ---
2617
27−((scoped_identifier
28− path: (identifier) @type)
29− (#match? @type "^[A-Z]"))
30−((scoped_identifier
31− path: (scoped_identifier
32− name: (identifier) @type))
33− (#match? @type "^[A-Z]"))
18+(escape_sequence) @escape
19+(primitive_type) @type.builtin
20+(boolean_literal) @constant.builtin
21+[
22+ (integer_literal)
23+ (float_literal)
24+] @number
25+[
26+ (char_literal)
27+ (string_literal)
28+ (raw_string_literal)
29+] @string
30+[
31+ (line_comment)
32+ (block_comment)
33+] @comment
3434
35−; Namespaces
35+; ---
36+; Extraneous
37+; ---
3638
37−(crate) @namespace
38−(extern_crate_declaration
39− (crate)
40− name: (identifier) @namespace)
41−(scoped_use_list
42− path: (identifier) @namespace)
43−(scoped_use_list
44− path: (scoped_identifier
45− (identifier) @namespace))
46−(use_list (scoped_identifier (identifier) @namespace . (_)))
39+(self) @variable.builtin
40+(enum_variant (identifier) @type.enum.variant)
4741
48−; Function calls
42+(field_initializer
43+ (field_identifier) @property)
44+(shorthand_field_initializer) @variable
45+(shorthand_field_identifier) @variable
4946
50−(call_expression
51− function: (identifier) @function)
52−(call_expression
53− function: (field_expression
54− field: (field_identifier) @function.method))
55−(call_expression
56− function: (scoped_identifier
57− "::"
58− name: (identifier) @function))
47+(lifetime
48+ "'" @label
49+ (identifier) @label)
50+(loop_label
51+ (identifier) @type)
5952
60−(generic_function
61− function: (identifier) @function)
62−(generic_function
63− function: (scoped_identifier
64− name: (identifier) @function))
65−(generic_function
66− function: (field_expression
67− field: (field_identifier) @function.method))
53+; ---
54+; Punctuation
55+; ---
6856
69−(macro_invocation
70− macro: (identifier) @function.macro
71− "!" @function.macro)
72−(macro_invocation
73− macro: (scoped_identifier
74− (identifier) @function.macro .))
57+[
58+ "::"
59+ "."
60+ ";"
61+] @punctuation.delimiter
7562
76−; (metavariable) @variable
77−(metavariable) @function.macro
63+[
64+ "("
65+ ")"
66+ "["
67+ "]"
68+] @punctuation.bracket
69+(type_arguments
70+ [
71+ "<"
72+ ">"
73+ ] @punctuation.bracket)
74+(type_parameters
75+ [
76+ "<"
77+ ">"
78+ ] @punctuation.bracket)
7879
79−"$" @function.macro
80+; ---
81+; Parameters
82+; ---
8083
81−; Function definitions
84+(parameter
85+ pattern: (identifier) @variable.parameter)
86+(closure_parameters
87+ (identifier) @variable.parameter)
8288
83−(function_item (identifier) @function)
84−(function_signature_item (identifier) @function)
8589
86−; Other identifiers
8790
88−(type_identifier) @type
89−(primitive_type) @type.builtin
90−(field_identifier) @property
91+; -------
92+; Keywords
93+; -------
9194
92−(line_comment) @comment
93−(block_comment) @comment
95+(for_expression
96+ "for" @keyword.control)
97+((identifier) @keyword.control
98+ (#match? @keyword.control "^yield$"))
99+[
100+ "while"
101+ "loop"
102+ "in"
103+ "break"
104+ "continue"
94105
95−"(" @punctuation.bracket
96−")" @punctuation.bracket
97−"[" @punctuation.bracket
98−"]" @punctuation.bracket
106+ "match"
107+ "if"
108+ "else"
109+ "return"
110+
111+ "await"
112+] @keyword.control
113+
114+[
115+ (crate)
116+ (super)
117+ "as"
118+ "use"
119+ "pub"
120+ "mod"
121+ "extern"
122+
123+ "fn"
124+ "struct"
125+ "enum"
126+ "impl"
127+ "where"
128+ "trait"
129+ "for"
130+
131+ "type"
132+ "union"
133+ "unsafe"
134+ "default"
135+ "macro_rules!"
136+
137+ "let"
138+ "ref"
139+ "move"
140+
141+ "dyn"
142+ "static"
143+ "const"
144+ "async"
145+] @keyword
99146
100−(type_arguments
101− "<" @punctuation.bracket
102− ">" @punctuation.bracket)
103−(type_parameters
104− "<" @punctuation.bracket
105− ">" @punctuation.bracket)
106−
107−"::" @punctuation.delimiter
108−"." @punctuation.delimiter
109−";" @punctuation.delimiter
110−
111−(parameter (identifier) @variable.parameter)
112−(closure_parameters (_) @variable.parameter)
113−
114−(lifetime (identifier) @label)
115−
116−"async" @keyword
117−"break" @keyword
118−"const" @keyword
119−"continue" @keyword
120−(crate) @keyword
121−"default" @keyword
122−"dyn" @keyword
123−"else" @keyword
124−"enum" @keyword
125−"extern" @keyword
126−"fn" @keyword
127−"for" @keyword
128−"if" @keyword
129−"impl" @keyword
130−"in" @keyword
131−"let" @keyword
132−"let" @keyword
133−"loop" @keyword
134−"macro_rules!" @keyword
135−"match" @keyword
136−"mod" @keyword
137−"move" @keyword
138−"pub" @keyword
139−"ref" @keyword
140−"return" @keyword
141−"static" @keyword
142−"struct" @keyword
143−"trait" @keyword
144−"type" @keyword
145−"union" @keyword
146−"unsafe" @keyword
147−"use" @keyword
148−"where" @keyword
149−"while" @keyword
150147(mutable_specifier) @keyword.mut
151−(use_list (self) @keyword)
152−(scoped_use_list (self) @keyword)
153−(scoped_identifier (self) @keyword)
154−(super) @keyword
155−"as" @keyword
156148
157−(self) @variable.builtin
158149
159−[
160−(char_literal)
161−(string_literal)
162−(raw_string_literal)
163−] @string
164150
165−(boolean_literal) @constant.builtin
166−(integer_literal) @number
167−(float_literal) @number
151+; -------
152+; Guess Other Types
153+; -------
168154
169−(escape_sequence) @escape
155+((identifier) @constant
156+ (#match? @constant "^[A-Z][A-Z\\d_]+$"))
157+
158+; ---
159+; PascalCase identifiers in call_expressions (e.g. `Ok()`)
160+; are assumed to be enum constructors.
161+; ---
162+
163+(call_expression
164+ function: [
165+ ((identifier) @type.variant
166+ (#match? @type.variant "^[A-Z]"))
167+ (scoped_identifier
168+ name: ((identifier) @type.variant
169+ (#match? @type.variant "^[A-Z]")))
170+ ])
171+
172+; ---
173+; Assume that types in match arms are enums and not
174+; tuple structs. Same for `if let` expressions.
175+; ---
176+
177+(match_pattern
178+ (scoped_identifier
179+ name: (identifier) @constructor))
180+(tuple_struct_pattern
181+ type: [
182+ ((identifier) @constructor)
183+ (scoped_identifier
184+ name: (identifier) @constructor)
185+ ])
186+(struct_pattern
187+ type: [
188+ ((type_identifier) @constructor)
189+ (scoped_type_identifier
190+ name: (type_identifier) @constructor)
191+ ])
170192
193+; ---
194+; Other PascalCase identifiers are assumed to be structs.
195+; ---
196+
197+((identifier) @type
198+ (#match? @type "^[A-Z]"))
199+
200+
201+
202+; -------
203+; Functions
204+; -------
205+
206+(call_expression
207+ function: [
208+ ((identifier) @function)
209+ (scoped_identifier
210+ name: (identifier) @function)
211+ (field_expression
212+ field: (field_identifier) @function)
213+ ])
214+(generic_function
215+ function: [
216+ ((identifier) @function)
217+ (scoped_identifier
218+ name: (identifier) @function)
219+ (field_expression
220+ field: (field_identifier) @function.method)
221+ ])
222+
223+(function_item
224+ name: (identifier) @function)
225+
226+; ---
227+; Macros
228+; ---
229+
230+(meta_item
231+ (identifier) @attribute)
171232(attribute_item) @attribute
172233(inner_attribute_item) @attribute
173234
235+(macro_definition
236+ name: (identifier) @function.macro)
237+(macro_invocation
238+ macro: [
239+ ((identifier) @function.macro)
240+ (scoped_identifier
241+ name: (identifier) @function.macro)
242+ ]
243+ "!" @function.macro)
244+
245+(metavariable) @variable.parameter
246+(fragment_specifier) @variable.parameter
247+
248+
249+
250+; -------
251+; Operators
252+; -------
253+
174254[
175−"*"
176−"'"
177−"->"
178−"=>"
179−"<="
180−"="
181−"=="
182−"!"
183−"!="
184−"%"
185−"%="
186−"&"
187−"&="
188−"&&"
189−"|"
190−"|="
191−"||"
192−"^"
193−"^="
194−"*"
195−"*="
196−"-"
197−"-="
198−"+"
199−"+="
200−"/"
201−"/="
202−">"
203−"<"
204−">="
205−">>"
206−"<<"
207−">>="
208−"@"
209−".."
210−"..="
211−"'"
255+ "*"
256+ "'"
257+ "->"
258+ "=>"
259+ "<="
260+ "="
261+ "=="
262+ "!"
263+ "!="
264+ "%"
265+ "%="
266+ "&"
267+ "&="
268+ "&&"
269+ "|"
270+ "|="
271+ "||"
272+ "^"
273+ "^="
274+ "*"
275+ "*="
276+ "-"
277+ "-="
278+ "+"
279+ "+="
280+ "/"
281+ "/="
282+ ">"
283+ "<"
284+ ">="
285+ ">>"
286+ "<<"
287+ ">>="
288+ "@"
289+ ".."
290+ "..="
291+ "'"
212292] @operator
213293
294+
295+
296+; -------
297+; Paths
298+; -------
299+
300+(use_declaration
301+ argument: (identifier) @namespace)
302+(use_wildcard
303+ (identifier) @namespace)
304+(extern_crate_declaration
305+ name: (identifier) @namespace)
306+(mod_item
307+ name: (identifier) @namespace)
308+(scoped_use_list
309+ path: (identifier)? @namespace)
310+(use_list
311+ (identifier) @namespace)
312+(use_as_clause
313+ path: (identifier)? @namespace
314+ alias: (identifier) @namespace)
315+
316+; ---
317+; Remaining Paths
318+; ---
319+
320+(scoped_identifier
321+ path: (identifier)? @namespace
322+ name: (identifier) @namespace)
323+(scoped_type_identifier
324+ path: (identifier) @namespace)
325+
326+
327+
328+; -------
329+; Remaining Identifiers
330+; -------
331+
214332"?" @special
333+
334+(type_identifier) @type
335+(identifier) @variable
336+(field_identifier) @variable