Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: correctly highlight paths with interpolation #56

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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