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 [attrs] content
148 ((apply_expression
149 function: (apply_expression function: [
150 ((_) @_func)
151 (apply_expression function: (_) @_func)
152 ])
153 argument: (indented_string_expression (string_fragment) @injection.content))
154 (#match? @_func "(^|\\.)write[BD]ash(Bin)?$")
155 (#set! injection.language "bash")
156 (#set! injection.combined))
157
158 ; pkgs.writers.writeFish[Bin] name [attrs] content
159 ((apply_expression
160 function: (apply_expression function: [
161 ((_) @_func)
162 (apply_expression function: (_) @_func)
163 ])
164 argument: (indented_string_expression (string_fragment) @injection.content))
165 (#match? @_func "(^|\\.)writeFish(Bin)?$")
166 (#set! injection.language "fish")
167 (#set! injection.combined))
168
169 ; pkgs.writers.* usage examples: nixpkgs/pkgs/build-support/writers/test.nix
170
171 ; pkgs.writers.writeRust[Bin] name attrs content
172 (apply_expression
173 (apply_expression
174 function: (apply_expression
175 function: ((_) @_func)))
176 argument: (indented_string_expression (string_fragment) @injection.content)
177 (#match? @_func "(^|\\.)writeRust(Bin)?$")
178 (#set! injection.language "rust")
179 (#set! injection.combined))
180
181 ; pkgs.writers.writeHaskell[Bin] name attrs content
182 (apply_expression
183 (apply_expression
184 function: (apply_expression
185 function: ((_) @_func)))
186 argument: (indented_string_expression (string_fragment) @injection.content)
187 (#match? @_func "(^|\\.)writeHaskell(Bin)?$")
188 (#set! injection.language "haskell")
189 (#set! injection.combined))
190
191 ; pkgs.writers.writeNim[Bin] name attrs content
192 (apply_expression
193 (apply_expression
194 function: (apply_expression
195 function: ((_) @_func)))
196 argument: (indented_string_expression (string_fragment) @injection.content)
197 (#match? @_func "(^|\\.)writeNim(Bin)?$")
198 (#set! injection.language "nim")
199 (#set! injection.combined))
200
201 ; pkgs.writers.writeJS[Bin] name attrs content
202 (apply_expression
203 (apply_expression
204 function: (apply_expression
205 function: ((_) @_func)))
206 argument: (indented_string_expression (string_fragment) @injection.content)
207 (#match? @_func "(^|\\.)writeJS(Bin)?$")
208 (#set! injection.language "javascript")
209 (#set! injection.combined))
210
211 ; pkgs.writers.writePerl[Bin] name attrs content
212 (apply_expression
213 (apply_expression
214 function: (apply_expression
215 function: ((_) @_func)))
216 argument: (indented_string_expression (string_fragment) @injection.content)
217 (#match? @_func "(^|\\.)writePerl(Bin)?$")
218 (#set! injection.language "perl")
219 (#set! injection.combined))
220
221 ; pkgs.writers.write{Python,PyPy}{2,3}[Bin] name attrs content
222 (apply_expression
223 (apply_expression
224 function: (apply_expression
225 function: ((_) @_func)))
226 argument: (indented_string_expression (string_fragment) @injection.content)
227 (#match? @_func "(^|\\.)write(Python|PyPy)[23](Bin)?$")
228 (#set! injection.language "python")
229 (#set! injection.combined))
230
231 ; pkgs.writers.writeNu[Bin] name [attrs] content
232 ((apply_expression
233 function: (apply_expression function: [
234 ((_) @_func)
235 (apply_expression function: (_) @_func)
236 ])
237 argument: (indented_string_expression (string_fragment) @injection.content))
238 (#match? @_func "(^|\\.)writeNu(Bin)?$")
239 (#set! injection.language "nu")
240 (#set! injection.combined))
241
242 ; pkgs.writers.writeRuby[Bin] name attrs content
243 (apply_expression
244 (apply_expression
245 function: (apply_expression
246 function: ((_) @_func)))
247 argument: (indented_string_expression (string_fragment) @injection.content)
248 (#match? @_func "(^|\\.)writeRuby(Bin)?$")
249 (#set! injection.language "ruby")
250 (#set! injection.combined))
251
252 ; pkgs.writers.writeLua[Bin] name attrs content
253 (apply_expression
254 (apply_expression
255 function: (apply_expression
256 function: ((_) @_func)))
257 argument: (indented_string_expression (string_fragment) @injection.content)
258 (#match? @_func "(^|\\.)writeLua(Bin)?$")
259 (#set! injection.language "lua")
260 (#set! injection.combined))
261
262 ; pkgs.writers.writeNginxConfig name attrs content
263 (apply_expression
264 (apply_expression
265 function: (apply_expression
266 function: ((_) @_func)))
267 argument: (indented_string_expression (string_fragment) @injection.content)
268 (#match? @_func "(^|\\.)writeNginxConfig$")
269 (#set! injection.language "nginx")
270 (#set! injection.combined))
271
272 ; pkgs.writers.writeGuile[Bin] name attrs content
273 (apply_expression
274 (apply_expression
275 function: (apply_expression
276 function: ((_) @_func)))
277 argument: (indented_string_expression (string_fragment) @injection.content)
278 (#match? @_func "(^|\\.)writeGuile(Bin)?$")
279 (#set! injection.language "scheme")
280 (#set! injection.combined))
281
282 ; pkgs.writers.writeBabashka[Bin] name attrs content
283 (apply_expression
284 (apply_expression
285 function: (apply_expression
286 function: ((_) @_func)))
287 argument: (indented_string_expression (string_fragment) @injection.content)
288 (#match? @_func "(^|\\.)writeBabashka(Bin)?$")
289 (#set! injection.language "clojure")
290 (#set! injection.combined))
291
292 ; Filename-based injection for indented strings.
293 ;
294 ; Detect the language from the file extension of a preceding filename
295 ; argument in a curried call:
296 ;
297 ; pkgs.writeText "index.html" ''
298 ; <div>Hello</div>
299 ; ''
300 ; pkgs.writeShellScriptBin "run.sh" ''
301 ; echo hi
302 ; ''
303 ;
304 ; The pattern matches `f "name.ext" '' ... ''` for any function `f`,
305 ; minus a small denylist of common nixpkgs idioms that take a
306 ; filename-shaped string but are not file writers (`removeSuffix`,
307 ; `trace`, `throw`, etc.). Outside the denylist, false positives are
308 ; tolerated: the worst case is mis-highlighting, whereas a false
309 ; negative means no highlighting at all.
310 ;
311 ; Concept harvested from nix-community/tree-sitter-nix#169 by
312 ; @nuketownada; rewritten as a hand-maintained list rather than
313 ; generated from a Nix derivation, with the function denylist added
314 ; per adversarial review of #53.
315 ((apply_expression
316 function: (apply_expression
317 function: (_) @_inner_func
318 argument: (string_expression (string_fragment) @_filename))
319 argument: (indented_string_expression (string_fragment) @injection.content))
320 (#match? @_filename "\\.(sh|bash)$")
321 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
322 (#set! injection.language "bash")
323 (#set! injection.combined))
324
325 ((apply_expression
326 function: (apply_expression
327 function: (_) @_inner_func
328 argument: (string_expression (string_fragment) @_filename))
329 argument: (indented_string_expression (string_fragment) @injection.content))
330 (#match? @_filename "\\.(py)$")
331 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
332 (#set! injection.language "python")
333 (#set! injection.combined))
334
335 ((apply_expression
336 function: (apply_expression
337 function: (_) @_inner_func
338 argument: (string_expression (string_fragment) @_filename))
339 argument: (indented_string_expression (string_fragment) @injection.content))
340 (#match? @_filename "\\.(html|htm)$")
341 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
342 (#set! injection.language "html")
343 (#set! injection.combined))
344
345 ((apply_expression
346 function: (apply_expression
347 function: (_) @_inner_func
348 argument: (string_expression (string_fragment) @_filename))
349 argument: (indented_string_expression (string_fragment) @injection.content))
350 (#match? @_filename "\\.(css)$")
351 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
352 (#set! injection.language "css")
353 (#set! injection.combined))
354
355 ((apply_expression
356 function: (apply_expression
357 function: (_) @_inner_func
358 argument: (string_expression (string_fragment) @_filename))
359 argument: (indented_string_expression (string_fragment) @injection.content))
360 (#match? @_filename "\\.(js|mjs|cjs)$")
361 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
362 (#set! injection.language "javascript")
363 (#set! injection.combined))
364
365 ((apply_expression
366 function: (apply_expression
367 function: (_) @_inner_func
368 argument: (string_expression (string_fragment) @_filename))
369 argument: (indented_string_expression (string_fragment) @injection.content))
370 (#match? @_filename "\\.(ts|mts|cts)$")
371 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
372 (#set! injection.language "typescript")
373 (#set! injection.combined))
374
375 ((apply_expression
376 function: (apply_expression
377 function: (_) @_inner_func
378 argument: (string_expression (string_fragment) @_filename))
379 argument: (indented_string_expression (string_fragment) @injection.content))
380 (#match? @_filename "\\.(json)$")
381 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
382 (#set! injection.language "json")
383 (#set! injection.combined))
384
385 ((apply_expression
386 function: (apply_expression
387 function: (_) @_inner_func
388 argument: (string_expression (string_fragment) @_filename))
389 argument: (indented_string_expression (string_fragment) @injection.content))
390 (#match? @_filename "\\.(yml|yaml)$")
391 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
392 (#set! injection.language "yaml")
393 (#set! injection.combined))
394
395 ((apply_expression
396 function: (apply_expression
397 function: (_) @_inner_func
398 argument: (string_expression (string_fragment) @_filename))
399 argument: (indented_string_expression (string_fragment) @injection.content))
400 (#match? @_filename "\\.(toml)$")
401 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
402 (#set! injection.language "toml")
403 (#set! injection.combined))
404
405 ((apply_expression
406 function: (apply_expression
407 function: (_) @_inner_func
408 argument: (string_expression (string_fragment) @_filename))
409 argument: (indented_string_expression (string_fragment) @injection.content))
410 (#match? @_filename "\\.(lua)$")
411 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
412 (#set! injection.language "lua")
413 (#set! injection.combined))
414
415 ((apply_expression
416 function: (apply_expression
417 function: (_) @_inner_func
418 argument: (string_expression (string_fragment) @_filename))
419 argument: (indented_string_expression (string_fragment) @injection.content))
420 (#match? @_filename "\\.(nix)$")
421 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
422 (#set! injection.language "nix")
423 (#set! injection.combined))
424
425 ((apply_expression
426 function: (apply_expression
427 function: (_) @_inner_func
428 argument: (string_expression (string_fragment) @_filename))
429 argument: (indented_string_expression (string_fragment) @injection.content))
430 (#match? @_filename "\\.(xml)$")
431 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
432 (#set! injection.language "xml")
433 (#set! injection.combined))
434
435 ((apply_expression
436 function: (apply_expression
437 function: (_) @_inner_func
438 argument: (string_expression (string_fragment) @_filename))
439 argument: (indented_string_expression (string_fragment) @injection.content))
440 (#match? @_filename "\\.(md)$")
441 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
442 (#set! injection.language "markdown")
443 (#set! injection.combined))
444
445 ((apply_expression
446 function: (apply_expression
447 function: (_) @_inner_func
448 argument: (string_expression (string_fragment) @_filename))
449 argument: (indented_string_expression (string_fragment) @injection.content))
450 (#match? @_filename "\\.(sql)$")
451 (#not-match? @_inner_func "(^|\\.)(removeSuffix|hasSuffix|hasPrefix|removePrefix|trace|throw|warn|warnIf|abort|assertMsg|seq|deepSeq|writeShellScript|writeShellScriptBin|runCommand|runCommandLocal|runCommandCC|runCommandNoCC)$")
452 (#set! injection.language "sql")
453 (#set! injection.combined))
454
455 ((apply_expression
456 function: (apply_expression function: (_) @_func
457 argument: (string_expression (string_fragment) @injection.filename))
458 argument: (indented_string_expression (string_fragment) @injection.content))
459 (#match? @_func "(^|\\.)write(Text|Script(Bin)?)$")
460 (#set! injection.combined))
461
462 ; Let Helix infer a language from a shebang at the start of an indented string.
463 ((indented_string_expression (string_fragment) @injection.shebang @injection.content)
464 (#set! injection.combined))