diff --git a/rc/filetype/nix.kak b/rc/filetype/nix.kak new file mode 100644 index 00000000..b6077739 --- /dev/null +++ b/rc/filetype/nix.kak @@ -0,0 +1,115 @@ +# 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 + + hook window ModeChange insert:.* -group nix-trim-indent nix-trim-indent + hook window InsertChar .* -group nix-indent nix-indent-on-char + hook window InsertChar \n -group nix-indent nix-indent-on-new-line + + 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 } +} + +provide-module nix %( + +# 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 '"' (?]=?\??' 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: +add-highlighter shared/nix/code/ regex '\s(\.?\.?/[-A-Za-z0-9/_+.]+)[;?]?' 1:meta +# 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 + try %{ execute-keys -draft -itersel s \h+$ d } +} + +define-command -hidden nix-indent-on-char %< + evaluate-commands -draft -itersel %< + # align closer token to its opener when alone on a line + try %/ execute-keys -draft ^\h+[]}]$ m s \A|.\z 1 / + > +> + +define-command -hidden nix-indent-on-new-line %< + evaluate-commands -draft -itersel %< + # copy // comments prefix and following white spaces + try %{ execute-keys -draft k s ^\h*\K#\h* y gh j P } + # preserve previous line indent + try %{ execute-keys -draft \; K } + # filter previous line + try %{ execute-keys -draft k : nix-trim-indent } + # indent after lines beginning / ending with opener token + try %_ execute-keys -draft k ^\h*[[{]|[[{]$ j _ + > +> + +)