Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
; tree-sitter highlighting resolves in .scm order. For a given byte
; offset, the last matching capture wins. This file is ordered from
; generic to specific: base variables and keywords first, then more
; targeted builtin / call / member patterns that override them.

; Base ----------------------------------------------------------------

; Generic identifier reference. Specific rules (builtins, calls,
; booleans, constants) later in the file override this.
(variable_expression (identifier) @variable)

; Comments ------------------------------------------------------------

(comment) @comment
(doc_comment) @comment.line.documentation

; Keywords ------------------------------------------------------------

[
  "assert"
  "in"
  "inherit"
  "let"
  "rec"
  "with"
] @keyword

[
  "if"
  "then"
  "else"
] @keyword.control.conditional

"or" @keyword.operator

; Literals ------------------------------------------------------------

(integer_expression) @constant.numeric.integer
(float_expression) @constant.numeric.float

[
  (string_expression)
  (indented_string_expression)
] @string

(escape_sequence) @constant.character.escape
(dollar_escape) @constant.character.escape

[
  (path_expression)
  (hpath_expression)
  (spath_expression)
] @string.special.path

(uri_expression) @string.special.url

; Functions -----------------------------------------------------------

; Parameters: `x: body` and `{ a, b }: body`.
(function_expression
  universal: (identifier) @variable.parameter)

(formal
  name: (identifier) @variable.parameter
  "?"? @punctuation.delimiter)

(ellipses) @variable.parameter.builtin

; Attrset members -----------------------------------------------------

(binding
  attrpath: (attrpath (identifier)) @variable.other.member)

(select_expression
  attrpath: (attrpath (identifier)) @variable.other.member)

(inherit attrs: (inherited_attrs attr: (identifier) @variable.other.member))
(inherit_from attrs: (inherited_attrs attr: (identifier) @variable.other.member))

; Function calls ------------------------------------------------------
; After member rules so `lib.isBool` in apply position is @function,
; not @variable.other.member.

(apply_expression
  function: [
    (variable_expression (identifier) @function)
    (select_expression
      attrpath: (attrpath
        attr: (identifier) @function .))])

; Function-valued arguments of Nix builtins and nixpkgs lib functions.
; Applications are left-associative, so each argument position has a
; differently nested `apply_expression`.
((apply_expression
  function: [
    (variable_expression (identifier) @_higher_order @function)
    (select_expression
      attrpath: (attrpath
        attr: (identifier) @_higher_order @function .))]
  argument: [
    (variable_expression (identifier) @function)
    (select_expression
      attrpath: (attrpath
        attr: (identifier) @function .))])
 (#any-of? @_higher_order
   "all" "any" "collect" "compareLists" "composeExtensions" "concatImapStrings"
   "concatMap" "concatMapAttrs" "concatMapStrings" "converge" "count" "crossLists"
   "extends" "filter" "filterAttrs" "filterAttrsRecursive" "filterSource" "findFirst"
   "findFirstIndex" "findSingle" "fix" "fix'" "flip" "fold" "foldAttrs" "foldl"
   "foldl'" "foldlAttrs" "foldr" "functionArgs" "genList" "groupBy" "groupBy'"
   "ifilter0" "imap0" "imap1" "makeExtensible" "makeOverridable" "makeScope"
   "makeScopeWithSplicing" "map" "mapAttrs" "mapAttrs'" "mapAttrsRecursive"
   "mapAttrsRecursiveCond" "mapAttrsToList" "mapAttrsToListRecursive"
   "mapAttrsToListRecursiveCond" "mapCartesianProduct" "mapCrossIndex" "mapDerivationAttrset"
   "mapNullable" "mirrorFunctionArgs" "mkAliasAndWrapDefinitions" "optionDescriptionPhrase"
   "partition" "recursiveUpdateUntil"
   "setFunctionArgs" "sort" "sortOn" "splitByAndCompare" "splitStringBy" "stringAsChars"
   "toposort" "traceValFn" "traceValSeqFn" "traceValSeqNFn" "updateName"
   "zipAttrsWith" "zipListsWith"))

((apply_expression
  function: (apply_expression
    function: [
      (variable_expression (identifier) @_higher_order @function)
      (select_expression
        attrpath: (attrpath
          attr: (identifier) @_higher_order @function .))])
  argument: [
    (variable_expression (identifier) @function)
    (select_expression
      attrpath: (attrpath
        attr: (identifier) @function .))])
 (#any-of? @_higher_order
   "addCheck" "coercedTo" "composeExtensions" "concatImapStringsSep"
   "concatMapAttrsStringSep" "concatMapStringsSep" "extends" "forEach" "genAttrs"
   "genAttrs'" "listDfs" "makeExtensibleWithCustomName" "makeScope"
   "makeScopeWithSplicing" "mapAttrsRecursiveCond" "mapAttrsToListRecursiveCond"
   "mirrorFunctionArgs" "mkDerivedConfig" "overrideDerivation" "splitByAndCompare"
   "zipAttrsWithNames"))

((apply_expression
  function: (apply_expression
    function: (apply_expression
      function: [
        (variable_expression (identifier) @_higher_order @function)
        (select_expression
          attrpath: (attrpath
            attr: (identifier) @_higher_order @function .))]))
  argument: [
    (variable_expression (identifier) @function)
    (select_expression
      attrpath: (attrpath
        attr: (identifier) @function .))])
 (#any-of? @_higher_order
   "groupBy'" "mkChangedOptionModule" "mkMergedOptionModule" "splitByAndCompare"
   "traceFnSeqN"))

; Pipe operators evaluate the side pointed to by the operator as a function:
; `value |> lib.foo` and `lib.foo <| value`.
(binary_expression
  operator: "|>"
  right: [
    (variable_expression (identifier) @function)
    (select_expression
      attrpath: (attrpath
        attr: (identifier) @function .))])

(binary_expression
  left: [
    (variable_expression (identifier) @function)
    (select_expression
      attrpath: (attrpath
        attr: (identifier) @function .))]
  operator: "<|")

(binding
  attrpath: (attrpath
    attr: (identifier) @function)
  expression: (function_expression))

; Builtins ------------------------------------------------------------

; `builtins.*` method-style calls: highlight the attr as a builtin
; function. `builtins` itself is painted by the @constant.builtin rule
; further down.
((select_expression
  expression: (variable_expression
    name: (identifier) @_id)
  attrpath: (attrpath
    attr: (identifier) @function.builtin))
 (#eq? @_id "builtins"))

; In apply position: `map f xs` -> `map` is a function builtin.
((apply_expression
  function: (variable_expression
    (identifier) @function.builtin))
 (#match? @function.builtin "^(__add|__addDrvOutputDependencies|__addErrorContext|__all|__any|__appendContext|__attrNames|__attrValues|__bitAnd|__bitOr|__bitXor|__catAttrs|__ceil|__compareVersions|__concatLists|__concatMap|__concatStringsSep|__convertHash|__deepSeq|__div|__elem|__elemAt|__fetchurl|__filter|__filterSource|__findFile|__flakeRefToString|__floor|__foldl'|__fromJSON|__functionArgs|__genList|__genericClosure|__getAttr|__getContext|__getEnv|__getFlake|__groupBy|__hasAttr|__hasContext|__hashFile|__hashString|__head|__intersectAttrs|__isAttrs|__isBool|__isFloat|__isFunction|__isInt|__isList|__isPath|__isString|__length|__lessThan|__listToAttrs|__mapAttrs|__match|__mul|__parseDrvName|__parseFlakeRef|__partition|__path|__pathExists|__readDir|__readFile|__readFileType|__replaceStrings|__seq|__sort|__split|__splitVersion|__storePath|__stringLength|__sub|__substring|__tail|__toFile|__toJSON|__toPath|__toXML|__trace|__traceVerbose|__tryEval|__typeOf|__unsafeDiscardOutputDependency|__unsafeDiscardStringContext|__unsafeGetAttrPos|__warn|__zipAttrsWith|baseNameOf|break|derivation|derivationStrict|dirOf|fetchGit|fetchMercurial|fetchTarball|fetchTree|fromTOML|isNull|map|placeholder|removeAttrs|scopedImport|toString)$"))

; `import` behaves like a keyword.
((variable_expression (identifier) @keyword.control.import)
 (#eq? @keyword.control.import "import"))

; `abort` / `throw` are control-flow exceptions.
((variable_expression (identifier) @keyword.control.exception)
 (#any-of? @keyword.control.exception "abort" "throw"))

; Booleans / constants ------------------------------------------------

((variable_expression (identifier) @constant.builtin.boolean)
 (#any-of? @constant.builtin.boolean "true" "false"))

((variable_expression (identifier) @constant.builtin)
 (#any-of? @constant.builtin
   "builtins" "null"
   "__curPos" "__currentSystem" "__currentTime" "__langVersion"
   "__nixPath" "__nixVersion" "__storeDir"))

; Operators -----------------------------------------------------------

(unary_expression
  operator: _ @operator)

(binary_expression
  operator: _ @operator)

[
  "="
  "@"
] @operator

; Punctuation ---------------------------------------------------------

[
  ";"
  "."
  ","
  ":"
] @punctuation.delimiter

[
  "("
  ")"
  "["
  "]"
  "{"
  "}"
] @punctuation.bracket

(interpolation
  "${" @punctuation.special
  "}" @punctuation.special) @embedded

(has_attr_expression
  expression: (_)
  "?" @operator
  attrpath: (attrpath
    attr: (identifier) @variable.other.member))