2019-05-10 01:59:00 +02:00
|
|
|
# Nix package manager language
|
|
|
|
# https://nixos.org/nix/manual/
|
|
|
|
|
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global BufCreate .*[.](nix) %{
|
|
|
|
set-option buffer filetype nix
|
|
|
|
}
|
|
|
|
|
|
|
|
# Initialization
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global WinSetOption filetype=nix %{
|
|
|
|
require-module nix
|
|
|
|
|
2022-04-30 11:22:47 +02:00
|
|
|
hook window ModeChange pop:insert:.* -group nix-trim-indent nix-trim-indent
|
2019-05-10 01:59:00 +02:00
|
|
|
hook window InsertChar .* -group nix-indent nix-indent-on-char
|
2021-04-17 10:17:01 +02:00
|
|
|
hook window InsertChar \n -group nix-insert nix-insert-on-new-line
|
2019-05-10 01:59:00 +02:00
|
|
|
hook window InsertChar \n -group nix-indent nix-indent-on-new-line
|
|
|
|
|
2020-10-19 17:53:08 +02:00
|
|
|
set-option buffer extra_word_chars _ -
|
2019-05-10 01:59:00 +02:00
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window nix-.+ }
|
|
|
|
}
|
|
|
|
|
|
|
|
hook -group nix-highlight global WinSetOption filetype=nix %{
|
|
|
|
add-highlighter window/nix ref nix
|
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/nix }
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:07:05 +02:00
|
|
|
provide-module nix %§
|
2019-05-10 01:59:00 +02:00
|
|
|
|
|
|
|
# Highlighters
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
add-highlighter shared/nix regions
|
|
|
|
add-highlighter shared/nix/code default-region group
|
|
|
|
# Define strings. They can contain interpolated nix code,
|
|
|
|
# which itself can contain strings.
|
|
|
|
# Note that we currently cannot properly support nesting of the same-delimiter strings
|
|
|
|
# because of https://github.com/mawww/kakoune/issues/1670
|
|
|
|
add-highlighter shared/nix/double_string region '"' (?<!\\)(?:\\\\)*" regions
|
|
|
|
# this is hard one: it is terminated by '', but ''$, ''\* and ''' are escapes.
|
|
|
|
add-highlighter shared/nix/indent_string region "''" (?<!')(?:''')*''(?![\\'$]) regions
|
|
|
|
add-highlighter shared/nix/comment1 region '#' '$' fill comment
|
|
|
|
add-highlighter shared/nix/comment2 region /\* \*/ fill comment
|
|
|
|
|
|
|
|
|
|
|
|
#add-highlighter shared/nix/code/ regex "([a-zA-Z_][a-zA-Z0-9-_.]*)\s*([=])" 1:variable
|
|
|
|
add-highlighter shared/nix/code/ regex "(\b|-)[0-9]*\.?[0-9eE]+\b" 0:value
|
|
|
|
|
|
|
|
add-highlighter shared/nix/double_string/str default-region fill string
|
|
|
|
add-highlighter shared/nix/double_string/variable region -recurse \{ (?<!\\)(\\\\)*\$\{ \} ref nix
|
|
|
|
add-highlighter shared/nix/indent_string/str default-region fill string
|
|
|
|
# FIXME: the opening regex is not ideal. See https://nixos.org/nix/manual/#idm140737317975776
|
|
|
|
# It should usually match "${", should match "'''${" (as "'''" is escaped itself),
|
|
|
|
# but should not match "''${" and "''\${".
|
|
|
|
# Seems that negative lookbehind semantics is not enough for some complex cases.
|
|
|
|
add-highlighter shared/nix/indent_string/variable region -recurse \{ (?<![^']'')\$\{ \} ref nix
|
|
|
|
|
|
|
|
add-highlighter shared/nix/code/ regex \b(true|false|null|let|in|with|if|then|else)\b 0:keyword
|
|
|
|
add-highlighter shared/nix/code/ regex \b(rec)\b\s*\{ 1:keyword
|
|
|
|
# Those are builtin functions available in global scope.
|
|
|
|
# They should not be assigned to.
|
|
|
|
add-highlighter shared/nix/code/ regex '[^.]\s*\b(builtins|inherit|baseNameOf|derivation|dirOf|fetchTarball|import|isNull|map|removeAttrs|throw|toString)\b\s*[^=]' 1:builtin
|
|
|
|
|
|
|
|
add-highlighter shared/nix/code/ regex '\b\s*(\.)\s*\b' 1:operator
|
2020-10-14 13:55:06 +02:00
|
|
|
add-highlighter shared/nix/code/ regex '[^-a-zA-Z0-9_](-+)' 1:operator
|
2019-05-10 01:59:00 +02:00
|
|
|
add-highlighter shared/nix/code/ regex '\?' 0:operator
|
|
|
|
add-highlighter shared/nix/code/ regex '\+\+=?' 0:operator
|
|
|
|
add-highlighter shared/nix/code/ regex '(\*|/|\+)' 0:operator
|
|
|
|
add-highlighter shared/nix/code/ regex '!' 0:operator
|
|
|
|
add-highlighter shared/nix/code/ regex '//=?' 0:operator
|
|
|
|
add-highlighter shared/nix/code/ regex '[<>]=?\??' 0:operator
|
|
|
|
add-highlighter shared/nix/code/ regex '(==|!=)' 0:operator
|
|
|
|
add-highlighter shared/nix/code/ regex '(&&|\|\|)' 0:operator
|
|
|
|
add-highlighter shared/nix/code/ regex '->' 0:operator
|
|
|
|
add-highlighter shared/nix/code/ regex \bor\b 0:operator
|
|
|
|
|
|
|
|
# override any operators matched before
|
|
|
|
# path:
|
2021-08-23 12:06:14 +02:00
|
|
|
add-highlighter shared/nix/code/ regex '\s\(*(\.?\.?[-A-Za-z0-9/_+.]*/[-A-Za-z0-9/_+.]*)[;?]?' 1:meta
|
2019-05-10 01:59:00 +02:00
|
|
|
# imported path:
|
|
|
|
add-highlighter shared/nix/code/ regex <[-A-Za-z0-9/_+.]+> 0:meta
|
|
|
|
# RFC 2396 URIs can be used without quoting. Strangely, "string" ends URL but ''indented'' one doesn't
|
|
|
|
# List of prohibited characters was tested manually in nix-repl as it is not properly documented.
|
|
|
|
add-highlighter shared/nix/code/ regex '([^:/?#\s]+):([^#(){}\[\]";`|\s\\]+)' 0:string
|
|
|
|
|
|
|
|
# Commands
|
|
|
|
# ‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
define-command -hidden nix-trim-indent %{
|
|
|
|
# remove trailing white spaces
|
2022-03-16 23:20:07 +01:00
|
|
|
try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
|
2019-05-10 01:59:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
define-command -hidden nix-indent-on-char %<
|
|
|
|
evaluate-commands -draft -itersel %<
|
|
|
|
# align closer token to its opener when alone on a line
|
2020-07-28 05:59:08 +02:00
|
|
|
try %/ execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m s \A|.\z <ret> 1<a-&> /
|
2019-05-10 01:59:00 +02:00
|
|
|
>
|
|
|
|
>
|
|
|
|
|
2021-04-17 10:17:01 +02:00
|
|
|
define-command -hidden nix-insert-on-new-line %<
|
2019-05-10 01:59:00 +02:00
|
|
|
evaluate-commands -draft -itersel %<
|
|
|
|
# copy // comments prefix and following white spaces
|
2022-03-16 23:20:07 +01:00
|
|
|
try %{ execute-keys -draft k x s ^\h*\K#\h* <ret> y gh j P }
|
2021-04-17 10:17:01 +02:00
|
|
|
>
|
|
|
|
>
|
|
|
|
|
|
|
|
define-command -hidden nix-indent-on-new-line %<
|
|
|
|
evaluate-commands -draft -itersel %<
|
2019-05-10 01:59:00 +02:00
|
|
|
# preserve previous line indent
|
2019-10-22 11:02:06 +02:00
|
|
|
try %{ execute-keys -draft <semicolon> K <a-&> }
|
2019-05-10 01:59:00 +02:00
|
|
|
# filter previous line
|
|
|
|
try %{ execute-keys -draft k : nix-trim-indent <ret> }
|
|
|
|
# indent after lines beginning / ending with opener token
|
2022-03-16 23:20:07 +01:00
|
|
|
try %_ execute-keys -draft k x <a-k> ^\h*[[{]|[[{]$ <ret> j <a-gt> _
|
2020-07-28 05:58:48 +02:00
|
|
|
# deindent closer token(s) when after cursor
|
2022-03-16 23:20:07 +01:00
|
|
|
try %_ execute-keys -draft x <a-k> ^\h*[}\]] <ret> gh / [}\]] <ret> m <a-S> 1<a-&> _
|
2019-05-10 01:59:00 +02:00
|
|
|
>
|
|
|
|
>
|
|
|
|
|
2019-08-12 18:07:05 +02:00
|
|
|
§
|