Skip to content

Commit

Permalink
Merge pull request #56 from Ma27/path-interpolation
Browse files Browse the repository at this point in the history
syntax: correctly highlight paths with interpolation
  • Loading branch information
Ma27 authored Jan 9, 2024
2 parents 1e8d3cc + 240ce59 commit 048c71f
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 1 deletion.
3 changes: 3 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ stdenv.mkDerivation rec {
endfunction
command! Syntax call Syntax()
set backspace=2
set hlsearch
'';

checkPhase = ''
Expand Down
4 changes: 3 additions & 1 deletion syntax/nix.vim
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ syn region nixString matchgroup=nixStringDelimiter start=+''+ skip=+''['$\\]+ en
syn match nixFunctionCall "[a-zA-Z_][a-zA-Z0-9_'-]*"

syn match nixPath "[a-zA-Z0-9._+-]*\%(/[a-zA-Z0-9._+-]\+\)\+"
syn region nixInterpolatedPath start="\%(\~\|[a-zA-Z0-9._+-]*\)/[a-zA-Z0-9._+/-]*\ze${" skip="[a-zA-Z0-9._+/-]*\ze${" end="}\@<=[a-zA-Z0-9._+/-]*\%([^a-zA-Z0-9._+/-]\|$\)\@=" contains=nixInterpolation
syn match nixHomePath "\~\%(/[a-zA-Z0-9._+-]\+\)\+"
syn match nixSearchPath "[a-zA-Z0-9._+-]\+\%(\/[a-zA-Z0-9._+-]\+\)*" contained
syn match nixPathDelimiter "[<>]" contained
Expand Down Expand Up @@ -123,7 +124,7 @@ syn region nixWithExpr matchgroup=nixWithExprKeyword start="\<with\>" matchgroup

syn region nixAssertExpr matchgroup=nixAssertKeyword start="\<assert\>" matchgroup=NONE end=";" contains=@nixExpr

syn 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,nixInterpolation
syn cluster nixExpr contains=nixBoolean,nixNull,nixOperator,nixParen,nixInteger,nixRecKeyword,nixConditional,nixBuiltin,nixSimpleBuiltin,nixComment,nixFunctionCall,nixFunctionArgument,nixArgOperator,nixSimpleFunctionArgument,nixInterpolatedPath,nixPath,nixHomePath,nixSearchPathRef,nixURI,nixAttributeSet,nixList,nixSimpleString,nixString,nixLetExpr,nixIfExpr,nixWithExpr,nixAssertExpr,nixInterpolation

" These definitions override @nixExpr and have to come afterwards:

Expand Down Expand Up @@ -179,6 +180,7 @@ hi def link nixNamespacedBuiltin Special
hi def link nixNull Constant
hi def link nixOperator Operator
hi def link nixPath Include
hi def link nixInterpolatedPath nixPath
hi def link nixPathDelimiter Delimiter
hi def link nixRecKeyword Keyword
hi def link nixSearchPath Include
Expand Down
173 changes: 173 additions & 0 deletions test/nix.vader
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,179 @@ Execute (syntax):
AssertEqual SyntaxOf('\./\.'), 'nixPath'
AssertEqual SyntaxOf('/etc/passwd'), 'nixPath'

Given nix (path-with-interpolation-simple):
./snens/${bar}/baz

Execute (syntax):
AssertEqual SyntaxOf('./'), 'nixInterpolatedPath'
AssertEqual SyntaxOf('${'), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('/baz'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 8), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 15), 'nixInterpolatedPath'

Given nix (path-with-interpolation-in-path-part):
./snens/${bar}kek/end

Execute (syntax):
AssertEqual SyntaxOf('./'), 'nixInterpolatedPath'
AssertEqual SyntaxOf('${'), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('/end'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 8), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 14), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 15), 'nixInterpolatedPath'

Given nix (path-with-interpolation-and-prefix):
./snens/pre${bar}

Execute (syntax):
AssertEqual SyntaxOf('./'), 'nixInterpolatedPath'
AssertEqual SyntaxOf('${'), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 8), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 12), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 17), 'nixInterpolationDelimiter'

Given nix (path-with-interpolation-suffix):
./snens/pre${bar}suff

Execute (syntax):
AssertEqual SyntaxOf('./'), 'nixInterpolatedPath'
AssertEqual SyntaxOf('${'), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 8), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 12), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 17), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('suff'), 'nixInterpolatedPath'

Given nix (path-with-interpolation-in-mid-part):
./snens/pre${bar}/kek

Execute (syntax):
AssertEqual SyntaxOf('./'), 'nixInterpolatedPath'
AssertEqual SyntaxOf('${'), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('/kek'), 'nixInterpolatedPath'
AssertEqual SyntaxOf('bar'), 'nixInterpolationParam'

Given nix (path-with-multiple-interpolations):
./snens/${bar}${baz}

Execute (syntax):
AssertEqual SyntaxOf('./snens'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 9), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('bar'), 'nixInterpolationParam'
AssertEqual SyntaxOf('baz'), 'nixInterpolationParam'
AssertEqual SyntaxAt(1, 20), 'nixInterpolationDelimiter'

Given nix (path-with-multiple-interpolations-and-infix):
./snens/${bar}in${baz}

Execute (syntax):
AssertEqual SyntaxOf('./snens'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 9), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('in'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 17), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('bar'), 'nixInterpolationParam'
AssertEqual SyntaxOf('baz'), 'nixInterpolationParam'
AssertEqual SyntaxAt(1, 22), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 14), 'nixInterpolationDelimiter'

Given nix (path-with-multiple-interpolations-and-suffix):
./snens/${bar}in${baz}suff
AssertEqual SyntaxOf('./snens'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 9), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('in'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 17), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('bar'), 'nixInterpolationParam'
AssertEqual SyntaxOf('baz'), 'nixInterpolationParam'
AssertEqual SyntaxAt(1, 22), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 14), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('suff'), 'nixInterpolatedPath'

Execute (syntax):

Given nix (path-with-multiple-interpolations-and-prefix):
./snens/pre${bar}in${baz}

Execute (syntax):
AssertEqual SyntaxOf('./snens'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 12), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('in'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 20), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('bar'), 'nixInterpolationParam'
AssertEqual SyntaxOf('baz'), 'nixInterpolationParam'
AssertEqual SyntaxAt(1, 25), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 17), 'nixInterpolationDelimiter'

Given nix (path-with-multiple-interpolations-and-dir-suffix):
./snens/${bar}in${baz}/suff

Execute (syntax):
AssertEqual SyntaxOf('./snens'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 9), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('in'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 17), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('bar'), 'nixInterpolationParam'
AssertEqual SyntaxOf('baz'), 'nixInterpolationParam'
AssertEqual SyntaxAt(1, 22), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 14), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 23), 'nixInterpolatedPath'

Given nix (path-with-multiple-interpolations-in-separate-dirs):
./snens/${bar}/${baz}

Execute (syntax):
AssertEqual SyntaxOf('./snens'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 9), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 16), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('bar'), 'nixInterpolationParam'
AssertEqual SyntaxOf('baz'), 'nixInterpolationParam'
AssertEqual SyntaxAt(1, 21), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 14), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 15), 'nixInterpolatedPath'

Given nix (homepath-with-interpolation):
~/${snens}

Execute (syntax):
AssertEqual SyntaxOf('\~/'), 'nixInterpolatedPath'
AssertEqual SyntaxOf('${'), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('snens'), 'nixInterpolationParam'

Given nix (path-with-invalid-infix):
./snens/${bar}in!${baz}

Execute (syntax):
AssertEqual SyntaxOf('./snens'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 8), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 9), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('bar'), 'nixInterpolationParam'
AssertEqual SyntaxOf('in'), 'nixInterpolatedPath'
# invalid syntax. Just make sure this isn't misinterpreted as a path.
AssertEqual SyntaxOf('!'), 'nixOperator'
AssertEqual SyntaxAt(1, 18), ''
AssertEqual SyntaxOf('baz'), 'nixArgumentDefinition'

Given nix (path-with-recursive-interpolation):
./snens/${bar.${baz}}/suff

Execute (syntax):
AssertEqual SyntaxOf('./snens'), 'nixInterpolatedPath'
AssertEqual SyntaxAt(1, 9), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 15), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 21), 'nixInterpolationDelimiter'
AssertEqual SyntaxAt(1, 11), 'nixInterpolationParam'
AssertEqual SyntaxAt(1, 22), 'nixInterpolatedPath'

Given nix (path-with-multiline-subexpr):
foo/${
"bar"
}

Execute (syntax):
AssertEqual SyntaxOf('foo/'), 'nixInterpolatedPath'
AssertEqual SyntaxOf('${'), 'nixInterpolationDelimiter'
AssertEqual SyntaxOf('"'), 'nixStringDelimiter'
AssertEqual SyntaxOf('bar'), 'nixSimpleString'
AssertEqual SyntaxOf('}'), 'nixInterpolationDelimiter'

Given nix (let):
let
foo = true;
Expand Down

0 comments on commit 048c71f

Please sign in to comment.