kakoune

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

nim.kak (5487B)


      1 # https://nim-lang.org/
      2 #
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*\.nim(s|ble)? %{
      8     set-option buffer filetype nim
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=nim %{
     15     require-module nim
     16 
     17     set-option window static_words %opt{nim_static_words}
     18 
     19     hook window InsertChar \n -group nim-insert nim-insert-on-new-line
     20     hook window InsertChar \n -group nim-indent nim-indent-on-new-line
     21     # cleanup trailing whitespaces on current line insert end
     22     hook window ModeChange pop:insert:.* -group nim-trim-indent %{ try %{ exec -draft <semicolon> x s ^\h+$ <ret> d } }
     23 
     24     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window nim-.+ }
     25 }
     26 
     27 hook -group nim-highlight global WinSetOption filetype=nim %{
     28     add-highlighter window/nim ref nim
     29     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/nim }
     30 }
     31 
     32 provide-module nim %{
     33 
     34 # Highlighters
     35 # ‾‾‾‾‾‾‾‾‾‾‾‾
     36 
     37 add-highlighter shared/nim regions
     38 add-highlighter shared/nim/code default-region group
     39 add-highlighter shared/nim/triple_string region '([A-Za-z](_?\w)*)?"""' '"""(?!")' fill string
     40 add-highlighter shared/nim/raw_string region [A-Za-z](_?[A-Za-z])*" (?<!")"(?!") fill string
     41 add-highlighter shared/nim/string region (?<!'\\)" ((?<!\\)(\\\\)*"|$) group
     42 add-highlighter shared/nim/inline_documentation region '##' $ fill documentation
     43 add-highlighter shared/nim/documentation region '##\[' '\]##' fill documentation
     44 add-highlighter shared/nim/comment region '#\[' '\]#' group
     45 add-highlighter shared/nim/comment_line region (?<![^'].')#(?!'\[) $ group
     46 
     47 add-highlighter shared/nim/string/fill fill string
     48 add-highlighter shared/nim/comment/fill fill comment
     49 add-highlighter shared/nim/comment_line/fill fill comment
     50 
     51 evaluate-commands %sh{
     52     # Grammar
     53     opchars='[=+-/<>@$~&%|!?^.:\\*]'
     54     opnocol='[=+-/<>@$~&%|!?^.\\*]'
     55     letter='A-Za-z\u000080-\u10FFFF'
     56     customsuffix="'[${letter}](_?[${letter}0-9])*"
     57     suffix="(${customsuffix}|[iIuU](8|16|32|64)|[fF](32|64)?|[dDuU])?"
     58     floatsuffix="(${customsuffix}|[fF](32|64)?|[dD])?"
     59     hexdigit='[0-9a-fA-F]'
     60     octdigit='[0-7]'
     61     bindigit='[01]'
     62     hexlit="0[xX]${hexdigit}(_?${hexdigit})*"
     63     declit="\d(_?\d)*"
     64     octlit="0o${octdigit}(_?${octdigit})*"
     65     binlit="0[bB]${bindigit}(_?${bindigit})*"
     66     intlit="\b(${declit}|${hexlit}|${octlit}|${binlit})${suffix}\b"
     67     exponent="([eE][+-]?${declit})"
     68     floatlit="\b${declit}(\.${declit}${exponent}?|${exponent})${floatsuffix}\b"
     69 
     70     keywords="addr asm bind block break case cast concept const continue
     71     converter defer discard distinct do elif else end enum except export
     72     finally for func if import include interface iterator let macro
     73     method mixin nil out proc ptr raise ref return static template try type
     74     unsafeAddr using var when while yield with without atomic generic"
     75     operators="or xor and is isnot in notin of div mod shl shr not as from"
     76     types="int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 float
     77     float32 float64 bool char object seq array cstring string tuple varargs
     78     typedesc pointer byte set typed untyped void auto"
     79     values="false true on off"
     80 
     81     join() { sep=$2; set -- $1; IFS="$sep"; echo "$*"; }
     82 
     83     static_words="$(join "${keywords} ${types} ${operator} ${values}" ' ')"
     84 
     85     # Add the language's grammar to the static completion list
     86     printf %s\\n "declare-option str-list nim_static_words ${static_words}"
     87 
     88     keywords="$(join "${keywords}" '|')"
     89     operators="$(join "${operators}" '|')"
     90     types="$(join "${types}" '|')"
     91     values="$(join "${values}" '|')"
     92 
     93     # Highlight keywords
     94     printf %s "
     95         add-highlighter shared/nim/code/ regex ${opchars}+ 0:operator
     96         add-highlighter shared/nim/code/ regex (?<!${opchars}):{1,2}(?!${opchars}) 0:meta
     97         add-highlighter shared/nim/code/ regex :${opnocol}${opchars}* 0:operator
     98         add-highlighter shared/nim/code/ regex (?<!${opchars})(\*)(:)(?!${opchars}) 1:operator 2:meta
     99         add-highlighter shared/nim/code/ regex \b(${keywords})\b 0:keyword
    100         add-highlighter shared/nim/code/ regex \b(${operators})\b 0:operator
    101         add-highlighter shared/nim/code/ regex \b(${types})\b 0:type
    102         add-highlighter shared/nim/code/ regex \b(${values})\b 0:value
    103         add-highlighter shared/nim/code/ regex ${intlit} 0:value
    104         add-highlighter shared/nim/code/ regex ${floatlit} 0:value
    105     "
    106 }
    107 
    108 add-highlighter shared/nim/code/ regex '(,|;|`|\(\.?|\.?\)|\[[.:]?|\.?\]|\{\.?|\.?\})' 0:meta
    109 add-highlighter shared/nim/code/ regex %{'(\\([rcnlftvabe\\"']|0*[12]?\d?\d|x[0-9a-fA-F]{2})|[^'\n])'} 0:value
    110 
    111 # Commands
    112 # ‾‾‾‾‾‾‾‾
    113 
    114 define-command -hidden nim-insert-on-new-line %{
    115     evaluate-commands -draft -itersel %{
    116         # copy '#' comment prefix and following white spaces
    117         try %{ exec -draft k x s ^\h*#\h* <ret> y jgh P }
    118     }
    119 }
    120 
    121 define-command -hidden nim-indent-on-new-line %{
    122     evaluate-commands -draft -itersel %{
    123         # preserve previous line indent
    124         try %{ exec -draft <semicolon> K <a-&> }
    125         # cleanup trailing whitespaces from previous line
    126         try %{ exec -draft k x s \h+$ <ret> d }
    127         # indent after line ending with enum, tuple, object, type, import, export, const, let, var, ':' or '='
    128         try %{ exec -draft , k x <a-k> (:|=|\b(?:enum|tuple|object|const|let|var|import|export|type))$ <ret> j <a-gt> }
    129     }
    130 }
    131 
    132 }