kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

nix.kak (5532B)


      1 # Nix package manager language
      2 # https://nixos.org/nix/manual/
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](nix) %{
      8     set-option buffer filetype nix
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=nix %{
     15     require-module nix
     16 
     17     hook window ModeChange pop:insert:.* -group nix-trim-indent nix-trim-indent
     18     hook window InsertChar .* -group nix-indent nix-indent-on-char
     19     hook window InsertChar \n -group nix-insert nix-insert-on-new-line
     20     hook window InsertChar \n -group nix-indent nix-indent-on-new-line
     21 
     22     set-option buffer extra_word_chars _ -
     23     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window nix-.+ }
     24 }
     25 
     26 hook -group nix-highlight global WinSetOption filetype=nix %{
     27     add-highlighter window/nix ref nix
     28     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/nix }
     29 }
     30 
     31 provide-module nix %§
     32 
     33 # Highlighters
     34 # ‾‾‾‾‾‾‾‾‾‾‾‾
     35 
     36 add-highlighter shared/nix regions
     37 add-highlighter shared/nix/code  default-region group
     38 # Define strings. They can contain interpolated nix code,
     39 # which itself can contain strings.
     40 # Note that we currently cannot properly support nesting of the same-delimiter strings
     41 # because of https://github.com/mawww/kakoune/issues/1670
     42 add-highlighter shared/nix/double_string region '"'    (?<!\\)(?:\\\\)*"         regions
     43 # this is hard one: it is terminated by '', but ''$, ''\* and ''' are escapes.
     44 add-highlighter shared/nix/indent_string region "''"   (?<!')(?:''')*''(?![\\'$]) regions
     45 add-highlighter shared/nix/comment1      region '#'    '$'                       fill comment
     46 add-highlighter shared/nix/comment2      region /\*    \*/                       fill comment
     47 
     48 
     49 #add-highlighter shared/nix/code/ regex "([a-zA-Z_][a-zA-Z0-9-_.]*)\s*([=])" 1:variable
     50 add-highlighter shared/nix/code/ regex "(\b|-)[0-9]*\.?[0-9eE]+\b" 0:value
     51 
     52 add-highlighter shared/nix/double_string/str default-region fill string
     53 add-highlighter shared/nix/double_string/variable region -recurse \{ (?<!\\)(\\\\)*\$\{ \} ref nix
     54 add-highlighter shared/nix/indent_string/str default-region fill string
     55 # FIXME: the opening regex is not ideal. See https://nixos.org/nix/manual/#idm140737317975776
     56 # It should usually match "${", should match "'''${" (as "'''" is escaped itself),
     57 # but should not match "''${" and "''\${".
     58 # Seems that negative lookbehind semantics is not enough for some complex cases.
     59 add-highlighter shared/nix/indent_string/variable region -recurse \{ (?<![^']'')\$\{ \} ref nix
     60 
     61 add-highlighter shared/nix/code/ regex \b(true|false|null|let|in|with|if|then|else)\b 0:keyword
     62 add-highlighter shared/nix/code/ regex \b(rec)\b\s*\{ 1:keyword
     63 # Those are builtin functions available in global scope.
     64 # They should not be assigned to.
     65 add-highlighter shared/nix/code/ regex '[^.]\s*\b(builtins|inherit|baseNameOf|derivation|dirOf|fetchTarball|import|isNull|map|removeAttrs|throw|toString)\b\s*[^=]' 1:builtin
     66 
     67 add-highlighter shared/nix/code/ regex '\b\s*(\.)\s*\b'  1:operator
     68 add-highlighter shared/nix/code/ regex '[^-a-zA-Z0-9_](-+)' 1:operator
     69 add-highlighter shared/nix/code/ regex '\?'        0:operator
     70 add-highlighter shared/nix/code/ regex '\+\+=?'    0:operator
     71 add-highlighter shared/nix/code/ regex '(\*|/|\+)' 0:operator
     72 add-highlighter shared/nix/code/ regex '!'         0:operator
     73 add-highlighter shared/nix/code/ regex '//=?'      0:operator
     74 add-highlighter shared/nix/code/ regex '[<>]=?\??' 0:operator
     75 add-highlighter shared/nix/code/ regex '(==|!=)'   0:operator
     76 add-highlighter shared/nix/code/ regex '(&&|\|\|)' 0:operator
     77 add-highlighter shared/nix/code/ regex '->'        0:operator
     78 add-highlighter shared/nix/code/ regex \bor\b      0:operator
     79 
     80 # override any operators matched before
     81 # path:
     82 add-highlighter shared/nix/code/ regex '\s\(*(\.?\.?[-A-Za-z0-9/_+.]*/[-A-Za-z0-9/_+.]*)[;?]?' 1:meta
     83 # imported path:
     84 add-highlighter shared/nix/code/ regex <[-A-Za-z0-9/_+.]+> 0:meta
     85 # RFC 2396 URIs can be used without quoting. Strangely, "string" ends URL but ''indented'' one doesn't
     86 # List of prohibited characters was tested manually in nix-repl as it is not properly documented.
     87 add-highlighter shared/nix/code/ regex '([^:/?#\s]+):([^#(){}\[\]";`|\s\\]+)' 0:string
     88 
     89 # Commands
     90 # ‾‾‾‾‾‾‾‾
     91 
     92 define-command -hidden nix-trim-indent %{
     93     # remove trailing white spaces
     94     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
     95 }
     96 
     97 define-command -hidden nix-indent-on-char %<
     98     evaluate-commands -draft -itersel %<
     99         # align closer token to its opener when alone on a line
    100         try %/ execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m s \A|.\z <ret> 1<a-&> /
    101     >
    102 >
    103 
    104 define-command -hidden nix-insert-on-new-line %<
    105     evaluate-commands -draft -itersel %<
    106         # copy // comments prefix and following white spaces
    107         try %{ execute-keys -draft k x s ^\h*\K#\h* <ret> y gh j P }
    108     >
    109 >
    110 
    111 define-command -hidden nix-indent-on-new-line %<
    112     evaluate-commands -draft -itersel %<
    113         # preserve previous line indent
    114         try %{ execute-keys -draft <semicolon> K <a-&> }
    115         # filter previous line
    116         try %{ execute-keys -draft k : nix-trim-indent <ret> }
    117         # indent after lines beginning / ending with opener token
    118         try %_ execute-keys -draft k x <a-k> ^\h*[[{]|[[{]$ <ret> j <a-gt> _
    119         # deindent closer token(s) when after cursor
    120         try %_ execute-keys -draft x <a-k> ^\h*[}\]] <ret> gh / [}\]] <ret> m <a-S> 1<a-&> _
    121     >
    122 >
    123 
    124 §