Debian Codegraph @

runtime/queries/nix/injections.scm

raw symbols SCIP
1 ; Highlight links, TODOs, and similar markers inside comments by reusing the
2 ; dedicated comment language.
3 ((comment) @injection.content
4 (#set! injection.language "comment"))
5
6 ; Mark arbitrary languages via a preceding comment.
7 ; Helix does not use upstream's #gsub! predicates here, so the comment text is
8 ; passed through as-is.
9 ((((comment) @injection.language) .
10 (indented_string_expression (string_fragment) @injection.content))
11 (#set! injection.combined))
12
13 ; nixos testScript binding - value is Python.
14 ((binding
15 attrpath: (attrpath (identifier) @_path)
16 expression: (indented_string_expression
17 (string_fragment) @injection.content))
18 (#eq? @_path "testScript")
19 (#set! injection.language "python")
20 (#set! injection.combined))
21
22 ; nixos testScript binding with `let ... in ''...''` - value is Python.
23 ((binding
24 attrpath: (attrpath (identifier) @_path)
25 expression: (let_expression
26 body: (indented_string_expression
27 (string_fragment) @injection.content)))
28 (#eq? @_path "testScript")
29 (#set! injection.language "python")
30 (#set! injection.combined))
31
32 ; Common binding-name -> bash injections.
33 ; Covers Phase/Hook/Script conventions used across nixpkgs stdenv.
34 ((binding
35 attrpath: (attrpath (identifier) @_path)
36 expression: [
37 (indented_string_expression (string_fragment) @injection.content)
38 (binary_expression (indented_string_expression (string_fragment) @injection.content))
39 ])
40 (#match? @_path "(^\\w*Phase|command|(pre|post)\\w*|(.*\\.)?\\w*([sS]cript|[hH]ook)|(.*\\.)?startup)$")
41 (#set! injection.language "bash")
42 (#set! injection.combined))
43
44 ; builtins.{match,split} regex str
45 ; Example: nix/tests/lang/eval-okay-regex-{match,split}.nix
46 ((apply_expression
47 function: (_) @_func
48 argument: (indented_string_expression (string_fragment) @injection.content))
49 (#match? @_func "(^|\\.)(match|split)$")
50 (#set! injection.language "regex")
51 (#set! injection.combined))
52
53 ; builtins.fromJSON json
54 ; Example: nix/tests/lang/eval-okay-fromjson.nix
55 ((apply_expression
56 function: (_) @_func
57 argument: (indented_string_expression (string_fragment) @injection.content))
58 (#match? @_func "(^|\\.)fromJSON$")
59 (#set! injection.language "json")
60 (#set! injection.combined))
61
62 ; builtins.fromTOML toml
63 ; Example: nix/tests/functional/lang/eval-okay-fromTOML.nix
64 ((apply_expression
65 function: (_) @_func
66 argument: (indented_string_expression (string_fragment) @injection.content))
67 (#match? @_func "(^|\\.)fromTOML$")
68 (#set! injection.language "toml")
69 (#set! injection.combined))
70
71 ; pkgs.writeShellScript / writeShellScriptBin - 2nd argument is bash.
72 ((apply_expression
73 function: (apply_expression function: (_) @_func)
74 argument: (indented_string_expression (string_fragment) @injection.content))
75 (#match? @_func "(^|\\.)writeShellScript(Bin)?$")
76 (#set! injection.language "bash")
77 (#set! injection.combined))
78
79 ; pkgs.runCommand variants - 3rd positional argument is bash.
80 (apply_expression
81 (apply_expression
82 function: (apply_expression
83 function: ((_) @_func)))
84 argument: (indented_string_expression (string_fragment) @injection.content)
85 (#match? @_func "(^|\\.)runCommand(((No)?(CC))?(Local)?)?$")
86 (#set! injection.language "bash")
87 (#set! injection.combined))
88
89 ; pkgs.writeShellApplication - the `text` attribute is bash.
90 (apply_expression
91 function: ((_) @_func)
92 argument: (_ (_)* (_ (_)* (binding
93 attrpath: (attrpath (identifier) @_path)
94 expression: (indented_string_expression
95 (string_fragment) @injection.content))))
96 (#match? @_func "(^|\\.)writeShellApplication$")
97 (#match? @_path "^text$")
98 (#set! injection.language "bash")
99 (#set! injection.combined))
100
101 ; writeShellApplication with `text = let ... in "..."` - follow the let body.
102 (apply_expression
103 function: ((_) @_func)
104 argument: (_ (_)* (_ (_)* (binding
105 attrpath: (attrpath (identifier) @_path)
106 expression: (let_expression
107 body: (indented_string_expression
108 (string_fragment) @injection.content)))))
109 (#match? @_func "(^|\\.)writeShellApplication$")
110 (#match? @_path "^text$")
111 (#set! injection.language "bash")
112 (#set! injection.combined))
113
114 ; lib.literalExpression / lib.literalExpressionPrefix - the string
115 ; argument is a Nix expression shown in docs; highlight as nix.
116 ; Uses specific node-type alternation rather than (_) to avoid
117 ; interference with other query captures.
118 ((apply_expression
119 function: [
120 (variable_expression (identifier) @_func)
121 (select_expression attrpath: (attrpath attr: (identifier) @_func .))
122 ]
123 argument: (indented_string_expression
124 (string_fragment) @injection.content))
125 (#match? @_func "^literalExpression(Prefix)?$")
126 (#set! injection.language "nix")
127 (#set! injection.combined))
128
129 ((apply_expression
130 function: [
131 (variable_expression (identifier) @_func)
132 (select_expression attrpath: (attrpath attr: (identifier) @_func .))
133 ]
134 argument: (string_expression
135 (string_fragment) @injection.content))
136 (#match? @_func "^literalExpression(Prefix)?$")
137 (#set! injection.language "nix"))
138
139 ; pkgs.writeCBin name content
140 ((apply_expression
141 function: (apply_expression function: (_) @_func)
142 argument: (indented_string_expression (string_fragment) @injection.content))
143 (#match? @_func "(^|\\.)writeC(Bin)?$")
144 (#set! injection.language "c")
145 (#set! injection.combined))
146
147 ; pkgs.writers.write{Bash,Dash}[Bin] name content
148 ((apply_expression
149 function: (apply_expression function: (_) @_func)
150 argument: (indented_string_expression (string_fragment) @injection.content))
151 (#match? @_func "(^|\\.)write[BD]ash(Bin)?$")
152 (#set! injection.language "bash")
153 (#set! injection.combined))
154
155 ; pkgs.writers.writeFish[Bin] name content
156 ((apply_expression
157 function: (apply_expression function: (_) @_func)
158 argument: (indented_string_expression (string_fragment) @injection.content))
159 (#match? @_func "(^|\\.)writeFish(Bin)?$")
160 (#set! injection.language "fish")
161 (#set! injection.combined))
162
163 ; pkgs.writers.* usage examples: nixpkgs/pkgs/build-support/writers/test.nix
164
165 ; pkgs.writers.writeRust[Bin] name attrs content
166 (apply_expression
167 (apply_expression
168 function: (apply_expression
169 function: ((_) @_func)))
170 argument: (indented_string_expression (string_fragment) @injection.content)
171 (#match? @_func "(^|\\.)writeRust(Bin)?$")
172 (#set! injection.language "rust")
173 (#set! injection.combined))
174
175 ; pkgs.writers.writeHaskell[Bin] name attrs content
176 (apply_expression
177 (apply_expression
178 function: (apply_expression
179 function: ((_) @_func)))
180 argument: (indented_string_expression (string_fragment) @injection.content)
181 (#match? @_func "(^|\\.)writeHaskell(Bin)?$")
182 (#set! injection.language "haskell")
183 (#set! injection.combined))
184
185 ; pkgs.writers.writeNim[Bin] name attrs content
186 (apply_expression
187 (apply_expression
188 function: (apply_expression
189 function: ((_) @_func)))
190 argument: (indented_string_expression (string_fragment) @injection.content)
191 (#match? @_func "(^|\\.)writeNim(Bin)?$")
192 (#set! injection.language "nim")
193 (#set! injection.combined))
194
195 ; pkgs.writers.writeJS[Bin] name attrs content
196 (apply_expression
197 (apply_expression
198 function: (apply_expression
199 function: ((_) @_func)))
200 argument: (indented_string_expression (string_fragment) @injection.content)
201 (#match? @_func "(^|\\.)writeJS(Bin)?$")
202 (#set! injection.language "javascript")
203 (#set! injection.combined))
204
205 ; pkgs.writers.writePerl[Bin] name attrs content
206 (apply_expression
207 (apply_expression
208 function: (apply_expression
209 function: ((_) @_func)))
210 argument: (indented_string_expression (string_fragment) @injection.content)
211 (#match? @_func "(^|\\.)writePerl(Bin)?$")
212 (#set! injection.language "perl")
213 (#set! injection.combined))
214
215 ; pkgs.writers.write{Python,PyPy}{2,3}[Bin] name attrs content
216 (apply_expression
217 (apply_expression
218 function: (apply_expression
219 function: ((_) @_func)))
220 argument: (indented_string_expression (string_fragment) @injection.content)
221 (#match? @_func "(^|\\.)write(Python|PyPy)[23](Bin)?$")
222 (#set! injection.language "python")
223 (#set! injection.combined))
224
225 ; pkgs.writers.writeNu[Bin] name attrs content
226 (apply_expression
227 (apply_expression
228 function: (apply_expression
229 function: ((_) @_func)))
230 argument: (indented_string_expression (string_fragment) @injection.content)
231 (#match? @_func "(^|\\.)writeNu(Bin)?$")
232 (#set! injection.language "nu")
233 (#set! injection.combined))
234
235 ; pkgs.writers.writeRuby[Bin] name attrs content
236 (apply_expression
237 (apply_expression
238 function: (apply_expression
239 function: ((_) @_func)))
240 argument: (indented_string_expression (string_fragment) @injection.content)
241 (#match? @_func "(^|\\.)writeRuby(Bin)?$")
242 (#set! injection.language "ruby")
243 (#set! injection.combined))
244
245 ; pkgs.writers.writeLua[Bin] name attrs content
246 (apply_expression
247 (apply_expression
248 function: (apply_expression
249 function: ((_) @_func)))
250 argument: (indented_string_expression (string_fragment) @injection.content)
251 (#match? @_func "(^|\\.)writeLua(Bin)?$")
252 (#set! injection.language "lua")
253 (#set! injection.combined))
254
255 ; pkgs.writers.writeNginxConfig name attrs content
256 (apply_expression
257 (apply_expression
258 function: (apply_expression
259 function: ((_) @_func)))
260 argument: (indented_string_expression (string_fragment) @injection.content)
261 (#match? @_func "(^|\\.)writeNginxConfig$")
262 (#set! injection.language "nginx")
263 (#set! injection.combined))
264
265 ; pkgs.writers.writeGuile[Bin] name attrs content
266 (apply_expression
267 (apply_expression
268 function: (apply_expression
269 function: ((_) @_func)))
270 argument: (indented_string_expression (string_fragment) @injection.content)
271 (#match? @_func "(^|\\.)writeGuile(Bin)?$")
272 (#set! injection.language "scheme")
273 (#set! injection.combined))
274
275 ; pkgs.writers.writeBabashka[Bin] name attrs content
276 (apply_expression
277 (apply_expression
278 function: (apply_expression
279 function: ((_) @_func)))
280 argument: (indented_string_expression (string_fragment) @injection.content)
281 (#match? @_func "(^|\\.)writeBabashka(Bin)?$")
282 (#set! injection.language "clojure")
283 (#set! injection.combined))
284
285 ; Filename-based injection for indented strings.
286 ;
287 ; Detect the language from the file extension of a preceding filename
288 ; argument in a curried call:
289 ;
290 ; pkgs.writeText "index.html" ''
291 ; <div>Hello</div>
292 ; ''
293 ; pkgs.writeShellScriptBin "run.sh" ''
294 ; echo hi
295 ; ''
296 ;
297 ; The pattern matches `f "name.ext" '' ... ''` for any function `f`,
298 ; minus a small denylist of common nixpkgs idioms that take a
299 ; filename-shaped string but are not file writers (`removeSuffix`,
300 ; `trace`, `throw`, etc.). Outside the denylist, false positives are
301 ; tolerated: the worst case is mis-highlighting, whereas a false
302 ; negative means no highlighting at all.
303 ;
304 ; Concept harvested from nix-community/tree-sitter-nix#169 by
305 ; @nuketownada; rewritten as a hand-maintained list rather than
306 ; generated from a Nix derivation, with the function denylist added
307 ; per adversarial review of #53.
308 ((apply_expression
309 function: (apply_expression
310 function: (_) @_inner_func
311 argument: (string_expression (string_fragment) @_filename))
312 argument: (indented_string_expression (string_fragment) @injection.content))
313 (#match? @_filename "\\.(sh|bash)$")
314 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
315 (#set! injection.language "bash")
316 (#set! injection.combined))
317
318 ((apply_expression
319 function: (apply_expression
320 function: (_) @_inner_func
321 argument: (string_expression (string_fragment) @_filename))
322 argument: (indented_string_expression (string_fragment) @injection.content))
323 (#match? @_filename "\\.(py)$")
324 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
325 (#set! injection.language "python")
326 (#set! injection.combined))
327
328 ((apply_expression
329 function: (apply_expression
330 function: (_) @_inner_func
331 argument: (string_expression (string_fragment) @_filename))
332 argument: (indented_string_expression (string_fragment) @injection.content))
333 (#match? @_filename "\\.(html|htm)$")
334 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
335 (#set! injection.language "html")
336 (#set! injection.combined))
337
338 ((apply_expression
339 function: (apply_expression
340 function: (_) @_inner_func
341 argument: (string_expression (string_fragment) @_filename))
342 argument: (indented_string_expression (string_fragment) @injection.content))
343 (#match? @_filename "\\.(css)$")
344 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
345 (#set! injection.language "css")
346 (#set! injection.combined))
347
348 ((apply_expression
349 function: (apply_expression
350 function: (_) @_inner_func
351 argument: (string_expression (string_fragment) @_filename))
352 argument: (indented_string_expression (string_fragment) @injection.content))
353 (#match? @_filename "\\.(js|mjs|cjs)$")
354 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
355 (#set! injection.language "javascript")
356 (#set! injection.combined))
357
358 ((apply_expression
359 function: (apply_expression
360 function: (_) @_inner_func
361 argument: (string_expression (string_fragment) @_filename))
362 argument: (indented_string_expression (string_fragment) @injection.content))
363 (#match? @_filename "\\.(ts|mts|cts)$")
364 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
365 (#set! injection.language "typescript")
366 (#set! injection.combined))
367
368 ((apply_expression
369 function: (apply_expression
370 function: (_) @_inner_func
371 argument: (string_expression (string_fragment) @_filename))
372 argument: (indented_string_expression (string_fragment) @injection.content))
373 (#match? @_filename "\\.(json)$")
374 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
375 (#set! injection.language "json")
376 (#set! injection.combined))
377
378 ((apply_expression
379 function: (apply_expression
380 function: (_) @_inner_func
381 argument: (string_expression (string_fragment) @_filename))
382 argument: (indented_string_expression (string_fragment) @injection.content))
383 (#match? @_filename "\\.(yml|yaml)$")
384 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
385 (#set! injection.language "yaml")
386 (#set! injection.combined))
387
388 ((apply_expression
389 function: (apply_expression
390 function: (_) @_inner_func
391 argument: (string_expression (string_fragment) @_filename))
392 argument: (indented_string_expression (string_fragment) @injection.content))
393 (#match? @_filename "\\.(toml)$")
394 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
395 (#set! injection.language "toml")
396 (#set! injection.combined))
397
398 ((apply_expression
399 function: (apply_expression
400 function: (_) @_inner_func
401 argument: (string_expression (string_fragment) @_filename))
402 argument: (indented_string_expression (string_fragment) @injection.content))
403 (#match? @_filename "\\.(lua)$")
404 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
405 (#set! injection.language "lua")
406 (#set! injection.combined))
407
408 ((apply_expression
409 function: (apply_expression
410 function: (_) @_inner_func
411 argument: (string_expression (string_fragment) @_filename))
412 argument: (indented_string_expression (string_fragment) @injection.content))
413 (#match? @_filename "\\.(nix)$")
414 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
415 (#set! injection.language "nix")
416 (#set! injection.combined))
417
418 ((apply_expression
419 function: (apply_expression
420 function: (_) @_inner_func
421 argument: (string_expression (string_fragment) @_filename))
422 argument: (indented_string_expression (string_fragment) @injection.content))
423 (#match? @_filename "\\.(xml)$")
424 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
425 (#set! injection.language "xml")
426 (#set! injection.combined))
427
428 ((apply_expression
429 function: (apply_expression
430 function: (_) @_inner_func
431 argument: (string_expression (string_fragment) @_filename))
432 argument: (indented_string_expression (string_fragment) @injection.content))
433 (#match? @_filename "\\.(md)$")
434 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
435 (#set! injection.language "markdown")
436 (#set! injection.combined))
437
438 ((apply_expression
439 function: (apply_expression
440 function: (_) @_inner_func
441 argument: (string_expression (string_fragment) @_filename))
442 argument: (indented_string_expression (string_fragment) @injection.content))
443 (#match? @_filename "\\.(sql)$")
444 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
445 (#set! injection.language "sql")
446 (#set! injection.combined))
447
448 ((apply_expression
449 function: (apply_expression function: (_) @_func
450 argument: (string_expression (string_fragment) @injection.filename))
451 argument: (indented_string_expression (string_fragment) @injection.content))
452 (#match? @_func "(^|\\.)write(Text|Script(Bin)?)$")
453 (#set! injection.combined))
454
455 ; Let Helix infer a language from a shebang at the start of an indented string.
456 ((indented_string_expression (string_fragment) @injection.shebang @injection.content)
457 (#set! injection.combined))