codekingpro/portable-devtools
114k
1" Vim syntax file2" Language: Nix3" Maintainer: James Fleming <james@electronic-quill.net>4" (Github username: equill)5" Original Author: Daiderd Jordan <daiderd@gmail.com>6" Acknowledgement: Based on vim-nix maintained by Daiderd Jordan <daiderd@gmail.com>7" https://github.com/LnL7/vim-nix8" License: MIT9" Last Change: 2023 Aug 1910 11if exists("b:current_syntax")12 finish13endif14 15let s:cpo_save = &cpo16set cpo&vim17 18syn keyword nixBoolean true false19syn keyword nixNull null20syn keyword nixRecKeyword rec21 22syn keyword nixOperator or23syn match nixOperator '!=\|!'24syn match nixOperator '<=\?'25syn match nixOperator '>=\?'26syn match nixOperator '&&'27syn match nixOperator '//\='28syn match nixOperator '=='29syn match nixOperator '?'30syn match nixOperator '||'31syn match nixOperator '++\='32syn match nixOperator '-'33syn match nixOperator '\*'34syn match nixOperator '->'35 36syn match nixParen '[()]'37syn match nixInteger '\d\+'38 39syn keyword nixTodo FIXME NOTE TODO OPTIMIZE XXX HACK contained40syn match nixComment '#.*' contains=nixTodo,@Spell41syn region nixComment start=+/\*+ end=+\*/+ contains=nixTodo,@Spell42 43syn region nixInterpolation matchgroup=nixInterpolationDelimiter start="\${" end="}" contained contains=@nixExpr,nixInterpolationParam44 45syn match nixSimpleStringSpecial /\\\%([nrt"\\$]\|$\)/ contained46syn match nixStringSpecial /''['$]/ contained47syn match nixStringSpecial /\$\$/ contained48syn match nixStringSpecial /''\\[nrt]/ contained49 50syn match nixSimpleStringSpecial /\$\$/ contained51 52syn match nixInvalidSimpleStringEscape /\\[^nrt"\\$]/ contained53syn match nixInvalidStringEscape /''\\[^nrt]/ contained54 55syn region nixSimpleString matchgroup=nixStringDelimiter start=+"+ skip=+\\"+ end=+"+ contains=nixInterpolation,nixSimpleStringSpecial,nixInvalidSimpleStringEscape56syn region nixString matchgroup=nixStringDelimiter start=+''+ skip=+''['$\\]+ end=+''+ contains=nixInterpolation,nixStringSpecial,nixInvalidStringEscape57 58syn match nixFunctionCall "[a-zA-Z_][a-zA-Z0-9_'-]*"59 60syn match nixPath "[a-zA-Z0-9._+-]*\%(/[a-zA-Z0-9._+-]\+\)\+"61syn match nixHomePath "\~\%(/[a-zA-Z0-9._+-]\+\)\+"62syn match nixSearchPath "[a-zA-Z0-9._+-]\+\%(\/[a-zA-Z0-9._+-]\+\)*" contained63syn match nixPathDelimiter "[<>]" contained64syn match nixSearchPathRef "<[a-zA-Z0-9._+-]\+\%(\/[a-zA-Z0-9._+-]\+\)*>" contains=nixSearchPath,nixPathDelimiter65syn match nixURI "[a-zA-Z][a-zA-Z0-9.+-]*:[a-zA-Z0-9%/?:@&=$,_.!~*'+-]\+"66 67syn match nixAttributeDot "\." contained68syn match nixAttribute "[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%([^a-zA-Z0-9_'.-]\|$\)" contained69syn region nixAttributeAssignment start="=" end="\ze;" contained contains=@nixExpr70syn region nixAttributeDefinition start=/\ze[a-zA-Z_"$]/ end=";" contained contains=nixComment,nixAttribute,nixInterpolation,nixSimpleString,nixAttributeDot,nixAttributeAssignment71 72syn region nixInheritAttributeSubExpr start="("ms=e+1 end="\ze)" contained contains=nixAttributeDot,@nixExpr73syn region nixInheritAttributeScope start="\ze(" end=")" contained contains=nixInheritAttributeSubExpr74syn region nixAttributeDefinition matchgroup=nixInherit start="\<inherit\>" end=";" contained contains=nixComment,nixInheritAttributeScope,nixAttribute75 76syn region nixAttributeSet start="{" end="}" contains=nixComment,nixAttributeDefinition77 78" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv79syn region nixArgumentDefinitionWithDefault matchgroup=nixArgumentDefinition start="[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*?\@=" matchgroup=NONE end="[,}]\@=" transparent contained contains=@nixExpr80" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv81syn match nixArgumentDefinition "[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*[,}]\@=" contained82syn match nixArgumentEllipsis "\.\.\." contained83syn match nixArgumentSeparator "," contained84 85" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv86syn match nixArgOperator '@\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*[a-zA-Z_][a-zA-Z0-9_'-]*\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*:'he=s+1 contained contains=nixAttribute87 88" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv89syn match nixArgOperator '[a-zA-Z_][a-zA-Z0-9_'-]*\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*@'hs=e-1 contains=nixAttribute nextgroup=nixFunctionArgument90 91" This is a bit more complicated, because function arguments can be passed in a92" very similar form on how attribute sets are defined and two regions with the93" same start patterns will shadow each other. Instead of a region we could use a94" match on {\_.\{-\}}, which unfortunately doesn't take nesting into account.95"96" So what we do instead is that we look forward until we are sure that it's a97" function argument. Unfortunately, we need to catch comments and both vertical98" and horizontal white space, which the following regex should hopefully do:99"100" "\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*"101"102" It is also used throughout the whole file and is marked with 'v's as well.103"104" Fortunately the matching rules for function arguments are much simpler than105" for real attribute sets, because we can stop when we hit the first ellipsis or106" default value operator, but we also need to paste the "whitespace & comments107" eating" regex all over the place (marked with 'v's):108"109" Region match 1: { foo ? ... } or { foo, ... } or { ... } (ellipsis)110" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv {----- identifier -----}vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv111syn region nixFunctionArgument start="{\ze\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*\%([a-zA-Z_][a-zA-Z0-9_'-]*\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*[,?}]\|\.\.\.\)" end="}" contains=nixComment,nixArgumentDefinitionWithDefault,nixArgumentDefinition,nixArgumentEllipsis,nixArgumentSeparator nextgroup=nixArgOperator112 113" Now it gets more tricky, because we need to look forward for the colon, but114" there could be something like "{}@foo:", even though it's highly unlikely.115"116" Region match 2: {}117" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv@vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv{----- identifier -----} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv118syn region nixFunctionArgument start="{\ze\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*}\%(\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*@\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*[a-zA-Z_][a-zA-Z0-9_'-]*\)\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*:" end="}" contains=nixComment nextgroup=nixArgOperator119 120" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv121syn match nixSimpleFunctionArgument "[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*:\([\n ]\)\@="122 123syn region nixList matchgroup=nixListBracket start="\[" end="\]" contains=@nixExpr124 125syn region nixLetExpr matchgroup=nixLetExprKeyword start="\<let\>" end="\<in\>" contains=nixComment,nixAttributeDefinition126 127syn keyword nixIfExprKeyword then contained128syn region nixIfExpr matchgroup=nixIfExprKeyword start="\<if\>" end="\<else\>" contains=@nixExpr,nixIfExprKeyword129 130syn region nixWithExpr matchgroup=nixWithExprKeyword start="\<with\>" matchgroup=NONE end=";" contains=@nixExpr131 132syn region nixAssertExpr matchgroup=nixAssertKeyword start="\<assert\>" matchgroup=NONE end=";" contains=@nixExpr133 134syn cluster nixExpr contains=nixBoolean,nixNull,nixOperator,nixParen,nixInteger,nixRecKeyword,nixConditional,nixBuiltin,nixSimpleBuiltin,nixComment,nixFunctionCall,nixFunctionArgument,nixArgOperator,nixSimpleFunctionArgument,nixPath,nixHomePath,nixSearchPathRef,nixURI,nixAttributeSet,nixList,nixSimpleString,nixString,nixLetExpr,nixIfExpr,nixWithExpr,nixAssertExpr,nixInterpolation135 136" These definitions override @nixExpr and have to come afterwards:137 138syn match nixInterpolationParam "[a-zA-Z_][a-zA-Z0-9_'-]*\%(\.[a-zA-Z_][a-zA-Z0-9_'-]*\)*" contained139 140" Non-namespaced Nix builtins as of version 2.0:141syn keyword nixSimpleBuiltin142 \ abort baseNameOf derivation derivationStrict dirOf fetchGit143 \ fetchMercurial fetchTarball import isNull map mapAttrs placeholder removeAttrs144 \ scopedImport throw toString145 146 147" Namespaced and non-namespaced Nix builtins as of version 2.0:148syn keyword nixNamespacedBuiltin contained149 \ abort add addErrorContext all any attrNames attrValues baseNameOf150 \ catAttrs compareVersions concatLists concatStringsSep currentSystem151 \ currentTime deepSeq derivation derivationStrict dirOf div elem elemAt152 \ fetchGit fetchMercurial fetchTarball fetchurl filter \ filterSource153 \ findFile foldl' fromJSON functionArgs genList \ genericClosure getAttr154 \ getEnv hasAttr hasContext hashString head import intersectAttrs isAttrs155 \ isBool isFloat isFunction isInt isList isNull isString langVersion156 \ length lessThan listToAttrs map mapAttrs match mul nixPath nixVersion157 \ parseDrvName partition path pathExists placeholder readDir readFile158 \ removeAttrs replaceStrings scopedImport seq sort split splitVersion159 \ storeDir storePath stringLength sub substring tail throw toFile toJSON160 \ toPath toString toXML trace tryEval typeOf unsafeDiscardOutputDependency161 \ unsafeDiscardStringContext unsafeGetAttrPos valueSize fromTOML bitAnd162 \ bitOr bitXor floor ceil163 164syn match nixBuiltin "builtins\.[a-zA-Z']\+"he=s+9 contains=nixComment,nixNamespacedBuiltin165 166hi def link nixArgOperator Operator167hi def link nixArgumentDefinition Identifier168hi def link nixArgumentEllipsis Operator169hi def link nixAssertKeyword Keyword170hi def link nixAttribute Identifier171hi def link nixAttributeDot Operator172hi def link nixBoolean Boolean173hi def link nixBuiltin Special174hi def link nixComment Comment175hi def link nixConditional Conditional176hi def link nixHomePath Include177hi def link nixIfExprKeyword Keyword178hi def link nixInherit Keyword179hi def link nixInteger Integer180hi def link nixInterpolation Macro181hi def link nixInterpolationDelimiter Delimiter182hi def link nixInterpolationParam Macro183hi def link nixInvalidSimpleStringEscape Error184hi def link nixInvalidStringEscape Error185hi def link nixLetExprKeyword Keyword186hi def link nixNamespacedBuiltin Special187hi def link nixNull Constant188hi def link nixOperator Operator189hi def link nixPath Include190hi def link nixPathDelimiter Delimiter191hi def link nixRecKeyword Keyword192hi def link nixSearchPath Include193hi def link nixSimpleBuiltin Keyword194hi def link nixSimpleFunctionArgument Identifier195hi def link nixSimpleString String196hi def link nixSimpleStringSpecial SpecialChar197hi def link nixString String198hi def link nixStringDelimiter Delimiter199hi def link nixStringSpecial Special200hi def link nixTodo Todo201hi def link nixURI Include202hi def link nixWithExprKeyword Keyword203 204" This could lead up to slow syntax highlighting for large files, but usually205" large files such as all-packages.nix are one large attribute set, so if we'd206" use sync patterns we'd have to go back to the start of the file anyway207syn sync fromstart208 209let b:current_syntax = "nix"210 211let &cpo = s:cpo_save212unlet s:cpo_save213 