Skip to content

Commit 1762e2b

Browse files
committed
syntax/nix: autogenerate namespaced builtins from nix __dump-language
We had quite a bunch of changes in the past that added builtins to get them highlighted properly[1]. Nowadays it's possible to get this from Nix itself by running `nix __dump-language`. This was done on a recent Nix and includes all experimental features by default[2] which seems fine to me since this is not an LSP, but only an approximation. This patch adds a few new builtins, e.g. `break` and `flakeRefToString` and a script to regenerate the list. This doesn't include non-scoped builtins (e.g. `map`, `baseNameOf` etc.), but those are added far less frequent, so this seems OK to me. Also, there are a few hidden builtins, most notably `unsafe*` and `scopedImport`. I think this was done on purpose by upstream and I'm wondering if we should adhere to that approach. If we still want to keep those, I think we should add those to a custom highlighting group that's maintained manually. Co-authored-by: aszlig <[email protected]> [1] #44, #42, #36, #32, 711aaf7 [2] https://github.com/NixOS/nix/blob/2.20.1/src/nix/main.cc#L372
1 parent 048c71f commit 1762e2b

File tree

2 files changed

+118
-14
lines changed

2 files changed

+118
-14
lines changed

builtins.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env nix-shell
2+
#!nix-shell -i bash -p bash jq nix
3+
4+
header="syn keyword nixNamespacedBuiltin contained"
5+
6+
nix __dump-language | jq -r '
7+
def to_vim: keys | map(" \\ " + . + "\n") | .[];
8+
[ (.builtins | to_vim)
9+
, (.constants | del(.["true","false","null","builtins"]) | to_vim)
10+
] | add
11+
' | sed -n -i -e '/^'"$header"'/ {
12+
p; n; r /dev/stdin
13+
:l; n; /^$/!bl; b
14+
}; p' syntax/nix.vim

syntax/nix.vim

+104-14
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,112 @@ syn keyword nixSimpleBuiltin
137137
\ scopedImport throw toString
138138
139139

140+
" Generated automatically: use `./builtins.sh` from the repo's root to update.
140141
" Namespaced and non-namespaced Nix builtins as of version 2.0:
141142
syn keyword nixNamespacedBuiltin contained
142-
\ abort add addErrorContext all any attrNames attrValues baseNameOf
143-
\ catAttrs compareVersions concatLists concatStringsSep currentSystem
144-
\ currentTime deepSeq derivation derivationStrict dirOf div elem elemAt
145-
\ fetchGit fetchMercurial fetchTarball fetchurl filter \ filterSource
146-
\ findFile foldl' fromJSON functionArgs genList \ genericClosure getAttr
147-
\ getEnv hasAttr hasContext hashString head import intersectAttrs isAttrs
148-
\ isBool isFloat isFunction isInt isList isNull isString langVersion
149-
\ length lessThan listToAttrs map mapAttrs match mul nixPath nixVersion
150-
\ parseDrvName partition path pathExists placeholder readDir readFile
151-
\ removeAttrs replaceStrings scopedImport seq sort split splitVersion
152-
\ storeDir storePath stringLength sub substring tail throw toFile toJSON
153-
\ toPath toString toXML trace tryEval typeOf unsafeDiscardOutputDependency
154-
\ unsafeDiscardStringContext unsafeGetAttrPos valueSize fromTOML bitAnd
155-
\ bitOr bitXor floor ceil
143+
\ abort
144+
\ add
145+
\ all
146+
\ any
147+
\ attrNames
148+
\ attrValues
149+
\ baseNameOf
150+
\ bitAnd
151+
\ bitOr
152+
\ bitXor
153+
\ break
154+
\ catAttrs
155+
\ ceil
156+
\ compareVersions
157+
\ concatLists
158+
\ concatMap
159+
\ concatStringsSep
160+
\ deepSeq
161+
\ dirOf
162+
\ div
163+
\ elem
164+
\ elemAt
165+
\ fetchClosure
166+
\ fetchGit
167+
\ fetchTarball
168+
\ fetchurl
169+
\ filter
170+
\ filterSource
171+
\ findFile
172+
\ flakeRefToString
173+
\ floor
174+
\ foldl'
175+
\ fromJSON
176+
\ fromTOML
177+
\ functionArgs
178+
\ genList
179+
\ genericClosure
180+
\ getAttr
181+
\ getContext
182+
\ getEnv
183+
\ getFlake
184+
\ groupBy
185+
\ hasAttr
186+
\ hasContext
187+
\ hashFile
188+
\ hashString
189+
\ head
190+
\ import
191+
\ intersectAttrs
192+
\ isAttrs
193+
\ isBool
194+
\ isFloat
195+
\ isFunction
196+
\ isInt
197+
\ isList
198+
\ isNull
199+
\ isPath
200+
\ isString
201+
\ length
202+
\ lessThan
203+
\ listToAttrs
204+
\ map
205+
\ mapAttrs
206+
\ match
207+
\ mul
208+
\ outputOf
209+
\ parseDrvName
210+
\ parseFlakeRef
211+
\ partition
212+
\ path
213+
\ pathExists
214+
\ placeholder
215+
\ readDir
216+
\ readFile
217+
\ readFileType
218+
\ removeAttrs
219+
\ replaceStrings
220+
\ seq
221+
\ sort
222+
\ split
223+
\ splitVersion
224+
\ storePath
225+
\ stringLength
226+
\ sub
227+
\ substring
228+
\ tail
229+
\ throw
230+
\ toFile
231+
\ toJSON
232+
\ toPath
233+
\ toString
234+
\ toXML
235+
\ trace
236+
\ traceVerbose
237+
\ tryEval
238+
\ typeOf
239+
\ zipAttrsWith
240+
\ currentSystem
241+
\ currentTime
242+
\ langVersion
243+
\ nixPath
244+
\ nixVersion
245+
\ storeDir
156246
157247
syn match nixBuiltin "builtins\.[a-zA-Z']\+"he=s+9 contains=nixComment,nixNamespacedBuiltin
158248

0 commit comments

Comments
 (0)