From c245afc233024eaf1f3854bb7ac2b8c5276c313f Mon Sep 17 00:00:00 2001 From: aszlig Date: Tue, 8 Dec 2015 23:59:48 +0100 Subject: [PATCH 1/6] syntax: Remove superfluous commented region. It's a leftover from 75e965cf6e1b1428f2dce5130d9e7e492ecc92d8. Signed-off-by: aszlig --- syntax/nix.vim | 1 - 1 file changed, 1 deletion(-) diff --git a/syntax/nix.vim b/syntax/nix.vim index e7b3099c755..64890c6afc0 100644 --- a/syntax/nix.vim +++ b/syntax/nix.vim @@ -54,7 +54,6 @@ syn region nixAttributeDefinition start=/\ze[a-zA-Z_"$]/ end=";" contained conta syn region nixInheritAttributeScope start="(" end=")" contained contains=nixComment,nixAttribute,nixAttributeDot syn region nixAttributeDefinition matchgroup=nixInherit start="\" matchgroup=NONE end=";" contained contains=nixComment,nixInheritAttributeScope,nixAttribute -"syn region nixAttributeSet start="{\ze\_.\{-\}}\ze\%(\s\|\n\)*:\@!" end="}" contains=nixComment,nixAttributeDefinition syn region nixAttributeSet start="{" end="}" contains=nixComment,nixAttributeDefinition syn region nixArgumentDefinitionWithDefault matchgroup=nixArgumentDefinition start="[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%(\s\|\n\)*?\@=" matchgroup=NONE end="[,}]\@=" transparent contained contains=@nixExpr From 2f4b95aa98c355a6d13ee82ef6af7bdd403e6ac0 Mon Sep 17 00:00:00 2001 From: aszlig Date: Wed, 9 Dec 2015 00:01:13 +0100 Subject: [PATCH 2/6] syntax: Remove invalid and/or operators. In Nix, the only keyword that's close to that is the or operator, which is for assigning default values if attributes are non-existent. For logical and/or there is && and ||, which we already highlight. Signed-off-by: aszlig --- syntax/nix.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/syntax/nix.vim b/syntax/nix.vim index 64890c6afc0..053c9f3ff42 100644 --- a/syntax/nix.vim +++ b/syntax/nix.vim @@ -11,7 +11,7 @@ syn keyword nixBoolean true false syn keyword nixNull null syn keyword nixRecKeyword rec -syn keyword nixOperator and or not +syn keyword nixOperator or syn match nixOperator '!=\|!' syn match nixOperator '&&' syn match nixOperator '//\=' From 783eb45eaf7cdb02628d15a8c385cff549a45df7 Mon Sep 17 00:00:00 2001 From: aszlig Date: Wed, 9 Dec 2015 00:18:41 +0100 Subject: [PATCH 3/6] syntax: Use space/comment eating regex everywhere. While I was very reluctant to do this while working on 75e965c, it turns out that while using it in practice while writing and reading Nix files it really IS needed, because we have all sorts of commenting/whitespace everywhere. I did also insert a few more complications in the tests to make sure we really get as much of the corner-cases we ever can. Signed-off-by: aszlig --- syntax/nix.vim | 18 +++++++++++++----- test/nix.vader | 15 +++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/syntax/nix.vim b/syntax/nix.vim index 053c9f3ff42..71f78221181 100644 --- a/syntax/nix.vim +++ b/syntax/nix.vim @@ -56,13 +56,18 @@ syn region nixAttributeDefinition matchgroup=nixInherit start="\" matc syn region nixAttributeSet start="{" end="}" contains=nixComment,nixAttributeDefinition -syn region nixArgumentDefinitionWithDefault matchgroup=nixArgumentDefinition start="[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%(\s\|\n\)*?\@=" matchgroup=NONE end="[,}]\@=" transparent contained contains=@nixExpr -syn match nixArgumentDefinition "[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%(\s\|\n\)*[,}]\@=" contained +" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv +syn region nixArgumentDefinitionWithDefault matchgroup=nixArgumentDefinition start="[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*?\@=" matchgroup=NONE end="[,}]\@=" transparent contained contains=@nixExpr +" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv +syn match nixArgumentDefinition "[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*[,}]\@=" contained syn match nixArgumentEllipsis "\.\.\." contained syn match nixArgumentSeparator "," contained -syn match nixArgOperator '@\s*[a-zA-Z_][a-zA-Z0-9_'-]*\s*:'he=s+1 contained contains=nixAttribute -syn match nixArgOperator '[a-zA-Z_][a-zA-Z0-9_'-]*\s*@'hs=e-1 contains=nixAttribute nextgroup=nixFunctionArgument +" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv +syn match nixArgOperator '@\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*[a-zA-Z_][a-zA-Z0-9_'-]*\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*:'he=s+1 contained contains=nixAttribute + +" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv +syn match nixArgOperator '[a-zA-Z_][a-zA-Z0-9_'-]*\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*@'hs=e-1 contains=nixAttribute nextgroup=nixFunctionArgument " This is a bit more complicated, because function arguments can be passed in a " very similar form on how attribute sets are defined and two regions with the @@ -75,6 +80,8 @@ syn match nixArgOperator '[a-zA-Z_][a-zA-Z0-9_'-]*\s*@'hs=e-1 contains=nixAttrib " " "\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*" " +" It is also used throught the whole file and is marked with 'v's as well. +" " Fortunately the matching rules for function arguments are much simpler than " for real attribute sets, because we can stop when we hit the first ellipsis or " default value operator, but we also need to paste the "whitespace & comments @@ -91,7 +98,8 @@ syn region nixFunctionArgument start="{\ze\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\) " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv@vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv{----- identifier -----} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv syn 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=nixArgOperator -syn match nixSimpleFunctionArgument "[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%(\s\|\n\)*:/\@!" +" vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv +syn match nixSimpleFunctionArgument "[a-zA-Z_][a-zA-Z0-9_'-]*\ze\%(\s\|#.\{-\}\n\|\n\|/\*\_.\{-\}\*/\)*:/\@!" syn region nixList matchgroup=nixListBracket start="\[" end="\]" contains=@nixExpr diff --git a/test/nix.vader b/test/nix.vader index 4097c435241..299a9b493b4 100644 --- a/test/nix.vader +++ b/test/nix.vader @@ -32,8 +32,8 @@ Given nix (attribute-path-lots-of-spacing): a . b - . - c + .#nastycomment + x = 2 ; @@ -42,7 +42,9 @@ Given nix (attribute-path-lots-of-spacing): Execute (syntax): AssertEqual SyntaxOf('a'), 'nixAttribute' AssertEqual SyntaxOf('b'), 'nixAttribute' - AssertEqual SyntaxOf('c'), 'nixAttribute' + AssertEqual SyntaxAt(5, 3), 'nixAttributeDot' + AssertEqual SyntaxOf('nastycomment'), 'nixComment' + AssertEqual SyntaxOf('x'), 'nixAttribute' AssertEqual SyntaxOf('2'), 'nixInteger' Given nix (attribute-nested): @@ -213,12 +215,15 @@ Execute (syntax): Given nix (lambda-attrs): { # very descriptive comment - foo ? # another comment + foo + /**/ + ? # another comment /* yet another comment */ # default value here: 1 , bar ? "xxx" , yyy + # last comment , ... }: { result = null; @@ -228,6 +233,7 @@ Execute (syntax): AssertEqual SyntaxOf('very descriptive comment'), 'nixComment' AssertEqual SyntaxOf('foo'), 'nixArgumentDefinition' AssertEqual SyntaxOf('?'), 'nixOperator' + AssertEqual SyntaxOf('/\*\*/'), 'nixComment' AssertEqual SyntaxOf('another comment'), 'nixComment' AssertEqual SyntaxOf('yet another comment'), 'nixComment' AssertEqual SyntaxOf('default value here:'), 'nixComment' @@ -235,6 +241,7 @@ Execute (syntax): AssertEqual SyntaxOf('bar'), 'nixArgumentDefinition' AssertEqual SyntaxOf('xxx'), 'nixSimpleString' AssertEqual SyntaxOf('yyy'), 'nixArgumentDefinition' + AssertEqual SyntaxOf('last comment'), 'nixComment' AssertEqual SyntaxOf('\.\.\.'), 'nixArgumentEllipsis' AssertEqual SyntaxOf('result'), 'nixAttribute' AssertEqual SyntaxOf('null'), 'nixNull' From 75d1a652b34fb54e85b1a22a091e581d629cba0f Mon Sep 17 00:00:00 2001 From: aszlig Date: Wed, 9 Dec 2015 00:49:04 +0100 Subject: [PATCH 4/6] syntax: Add support for assertions. I somehow managed to forget about the assert keyword. From the rules it's quite similar to the with keyword, so nothing too fancy here :-) Signed-off-by: aszlig --- syntax/nix.vim | 5 ++++- test/nix.vader | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/syntax/nix.vim b/syntax/nix.vim index 71f78221181..bec5693b8b8 100644 --- a/syntax/nix.vim +++ b/syntax/nix.vim @@ -110,7 +110,9 @@ syn region nixIfExpr matchgroup=nixIfExprKeyword start="\" end="\" c syn region nixWithExpr matchgroup=nixWithExprKeyword start="\" matchgroup=NONE end=";" contains=@nixExpr -syn cluster nixExpr contains=nixBoolean,nixNull,nixOperator,nixParen,nixInteger,nixConditional,nixBuiltin,nixSimpleBuiltin,nixComment,nixFunctionCall,nixFunctionArgument,nixSimpleFunctionArgument,nixPath,nixHomePath,nixSearchPathDef,nixURI,nixAttributeSet,nixList,nixSimpleString,nixString,nixLetExpr,nixIfExpr,nixWithExpr +syn region nixAssertExpr matchgroup=nixAssertKeyword start="\" matchgroup=NONE end=";" contains=@nixExpr + +syn cluster nixExpr contains=nixBoolean,nixNull,nixOperator,nixParen,nixInteger,nixConditional,nixBuiltin,nixSimpleBuiltin,nixComment,nixFunctionCall,nixFunctionArgument,nixSimpleFunctionArgument,nixPath,nixHomePath,nixSearchPathDef,nixURI,nixAttributeSet,nixList,nixSimpleString,nixString,nixLetExpr,nixIfExpr,nixWithExpr,nixAssertExpr " These definitions override @nixExpr and have to come afterwards: @@ -137,6 +139,7 @@ syn match nixBuiltin "builtins\.[a-zA-Z']\+"he=s+9 contains=nixComment,nixNamesp hi def link nixArgOperator Operator hi def link nixArgumentDefinition Identifier hi def link nixArgumentEllipsis Operator +hi def link nixAssertKeyword Keyword hi def link nixAttribute Identifier hi def link nixAttributeDot Operator hi def link nixBoolean Boolean diff --git a/test/nix.vader b/test/nix.vader index 299a9b493b4..73f07478f14 100644 --- a/test/nix.vader +++ b/test/nix.vader @@ -266,6 +266,16 @@ Execute (syntax): AssertEqual SyntaxOf('foo'), 'nixFunctionCall' AssertEqual SyntaxOf('withfoo'), 'nixFunctionCall' +Given nix (assert-expr): + assert true -> false; null + +Execute (syntax): + AssertEqual SyntaxOf('assert'), 'nixAssertKeyword' + AssertEqual SyntaxOf('true'), 'nixBoolean' + AssertEqual SyntaxOf('->'), 'nixOperator' + AssertEqual SyntaxOf('false'), 'nixBoolean' + AssertEqual SyntaxOf('null'), 'nixNull' + Given nix (funarg-let-attrset): { xxx ? null }@yyy: From da6de93e7d2e19f5e9fbaf860434928c94771713 Mon Sep 17 00:00:00 2001 From: aszlig Date: Wed, 9 Dec 2015 01:09:55 +0100 Subject: [PATCH 5/6] syntax: Fix typo for nixSearchPathRef. It ended up with a D instead of an R in the @nixExpr cluster, so search path references weren't highlighted correctly when contained in other regions. Signed-off-by: aszlig --- syntax/nix.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/syntax/nix.vim b/syntax/nix.vim index bec5693b8b8..3b591d1efd2 100644 --- a/syntax/nix.vim +++ b/syntax/nix.vim @@ -112,7 +112,7 @@ syn region nixWithExpr matchgroup=nixWithExprKeyword start="\" matchgroup syn region nixAssertExpr matchgroup=nixAssertKeyword start="\" matchgroup=NONE end=";" contains=@nixExpr -syn cluster nixExpr contains=nixBoolean,nixNull,nixOperator,nixParen,nixInteger,nixConditional,nixBuiltin,nixSimpleBuiltin,nixComment,nixFunctionCall,nixFunctionArgument,nixSimpleFunctionArgument,nixPath,nixHomePath,nixSearchPathDef,nixURI,nixAttributeSet,nixList,nixSimpleString,nixString,nixLetExpr,nixIfExpr,nixWithExpr,nixAssertExpr +syn cluster nixExpr contains=nixBoolean,nixNull,nixOperator,nixParen,nixInteger,nixConditional,nixBuiltin,nixSimpleBuiltin,nixComment,nixFunctionCall,nixFunctionArgument,nixSimpleFunctionArgument,nixPath,nixHomePath,nixSearchPathRef,nixURI,nixAttributeSet,nixList,nixSimpleString,nixString,nixLetExpr,nixIfExpr,nixWithExpr,nixAssertExpr " These definitions override @nixExpr and have to come afterwards: From f3d72740cc2415f80bdc6e65b8ce5020b9b241b9 Mon Sep 17 00:00:00 2001 From: aszlig Date: Wed, 9 Dec 2015 01:13:26 +0100 Subject: [PATCH 6/6] tests: Add more tests for paths. So far we only tested URIs, but not search paths, home paths or relative/absolute paths. This should make sure we won't hit something like da6de93 again. Signed-off-by: aszlig --- test/nix.vader | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/nix.vader b/test/nix.vader index 73f07478f14..b68449712b4 100644 --- a/test/nix.vader +++ b/test/nix.vader @@ -137,6 +137,26 @@ Execute (syntax): AssertEqual SyntaxOf('github'), 'nixURI' AssertEqual SyntaxOf('nix'), 'nixURI' +Given nix (nix-search-path): + assert ; + +Execute (syntax): + AssertEqual SyntaxOf('<'), 'nixPathDelimiter' + AssertEqual SyntaxOf('foo'), 'nixSearchPath' + AssertEqual SyntaxOf('-'), 'nixSearchPath' + AssertEqual SyntaxOf('bar'), 'nixSearchPath' + AssertEqual SyntaxOf('/'), 'nixSearchPath' + AssertEqual SyntaxOf('\.'), 'nixSearchPath' + AssertEqual SyntaxOf('>'), 'nixPathDelimiter' + +Given nix (nix-paths): + [ ~/homefile ./. /etc/passwd ] + +Execute (syntax): + AssertEqual SyntaxOf('\~/homefile'), 'nixHomePath' + AssertEqual SyntaxOf('\./\.'), 'nixPath' + AssertEqual SyntaxOf('/etc/passwd'), 'nixPath' + Given nix (let): let foo = true;