From 904cec3c4a329cf89fc3219d359239910d61f3f6 Mon Sep 17 00:00:00 2001 From: thing1 Date: Tue, 28 Jan 2025 09:14:32 +0000 Subject: init commit --- autoload/filetype/nix.kak | 124 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 autoload/filetype/nix.kak (limited to 'autoload/filetype/nix.kak') diff --git a/autoload/filetype/nix.kak b/autoload/filetype/nix.kak new file mode 100644 index 0000000..35e5e4e --- /dev/null +++ b/autoload/filetype/nix.kak @@ -0,0 +1,124 @@ +# 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 pop:insert:.* -group nix-trim-indent nix-trim-indent + hook window InsertChar .* -group nix-indent nix-indent-on-char + hook window InsertChar \n -group nix-insert nix-insert-on-new-line + hook window InsertChar \n -group nix-indent nix-indent-on-new-line + + set-option buffer extra_word_chars _ - + 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/_+.]*/[-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 x 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-insert-on-new-line %< + evaluate-commands -draft -itersel %< + # copy // comments prefix and following white spaces + try %{ execute-keys -draft k x s ^\h*\K#\h* y gh j P } + > +> + +define-command -hidden nix-indent-on-new-line %< + evaluate-commands -draft -itersel %< + # 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 x ^\h*[[{]|[[{]$ j _ + # deindent closer token(s) when after cursor + try %_ execute-keys -draft x ^\h*[}\]] gh / [}\]] m 1 _ + > +> + +§ -- cgit v1.2.3