kakoune

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

go.kak (7543B)


      1 # https://golang.org/
      2 #
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*\.go %{
      8     set-option buffer filetype go
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=go %{
     15     require-module go
     16 
     17     set-option window static_words %opt{go_static_words}
     18 
     19     # cleanup trailing whitespaces when exiting insert mode
     20     hook window ModeChange pop:insert:.* -group go-trim-indent %{ try %{ execute-keys -draft xs^\h+$<ret>d } }
     21     hook window InsertChar \n -group go-indent go-indent-on-new-line
     22     hook window InsertChar \{ -group go-indent go-indent-on-opening-curly-brace
     23     hook window InsertChar \} -group go-indent go-indent-on-closing-curly-brace
     24     hook window InsertChar \n -group go-comment-insert go-insert-comment-on-new-line
     25     hook window InsertChar \n -group go-closing-delimiter-insert go-insert-closing-delimiter-on-new-line
     26 
     27     alias window alt go-alternative-file
     28 
     29     hook -once -always window WinSetOption filetype=.* %{
     30         remove-hooks window go-.+
     31         unalias window alt go-alternative-file
     32     }
     33 }
     34 
     35 hook -group go-highlight global WinSetOption filetype=go %{
     36     add-highlighter window/go ref go
     37     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/go }
     38 }
     39 
     40 provide-module go %§
     41 
     42 # Highlighters
     43 # ‾‾‾‾‾‾‾‾‾‾‾‾
     44 
     45 add-highlighter shared/go regions
     46 add-highlighter shared/go/code default-region group
     47 add-highlighter shared/go/back_string region '`' '`' fill string
     48 add-highlighter shared/go/double_string region '"' (?<!\\)(\\\\)*" fill string
     49 add-highlighter shared/go/single_string region "'" (?<!\\)(\\\\)*' fill string
     50 add-highlighter shared/go/comment region /\* \*/ fill comment
     51 add-highlighter shared/go/comment_line region '//' $ fill comment
     52 
     53 add-highlighter shared/go/code/numeric regex %{-?([0-9]*\.(?!0[xX]))?\b([0-9]+|0[xX][0-9a-fA-F]+)\.?([eE][+-]?[0-9]+)?i?\b} 0:value
     54 add-highlighter shared/go/code/function regex "\b(\w*)\b\h*(?:\[[\w\s\.,]*\])?\h*\(" 1:function
     55 add-highlighter shared/go/code/operator regex "(\+|-|\*|/|%|\+\+|--|\+=|-=|\*=|/=|%=|==|!=|>|<|>=|<=|&|&&|\|\||!|<-|:=|\.\.\.)" 1:operator
     56 
     57 evaluate-commands %sh{
     58     # Grammar
     59     keywords='break default func interface select case defer go map struct
     60               chan else goto package switch const fallthrough if range type
     61               continue for import return var'
     62     types='any bool byte chan comparable complex128 complex64 error float32 float64 int int16 int32
     63            int64 int8 interface intptr map rune string struct uint uint16 uint32 uint64 uint8 uintptr'
     64     values='false true nil iota'
     65     functions='append cap close complex copy delete imag len make new panic print println real recover'
     66 
     67     join() { sep=$2; eval set -- $1; IFS="$sep"; echo "$*"; }
     68 
     69     # Add the language's grammar to the static completion list
     70     printf %s\\n "declare-option str-list go_static_words $(join "${keywords} ${attributes} ${types} ${values} ${functions}" ' ')"
     71 
     72     # Highlight keywords
     73     printf %s "
     74         add-highlighter shared/go/code/keyword   regex \b($(join "${keywords}" '|'))\b 0:keyword
     75         add-highlighter shared/go/code/attribute regex \b($(join "${attributes}" '|'))\b 0:attribute
     76         add-highlighter shared/go/code/type      regex \b($(join "${types}" '|'))\b 0:type
     77         add-highlighter shared/go/code/value     regex \b($(join "${values}" '|'))\b 0:value
     78         add-highlighter shared/go/code/builtin   regex \b($(join "${functions}" '|'))\b 0:builtin
     79     "
     80 }
     81 
     82 # Commands
     83 # ‾‾‾‾‾‾‾‾
     84 
     85 define-command go-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ evaluate-commands %sh{
     86     case $kak_buffile in
     87         *_test.go)
     88             altfile=${kak_buffile%_test.go}.go
     89             test ! -f "$altfile" && echo "fail 'implementation file not found'" && exit
     90         ;;
     91         *.go)
     92             altfile=${kak_buffile%.go}_test.go
     93             test ! -f "$altfile" && echo "fail 'test file not found'" && exit
     94         ;;
     95         *)
     96             echo "fail 'alternative file not found'" && exit
     97         ;;
     98     esac
     99     printf "edit -- '%s'" "$(printf %s "$altfile" | sed "s/'/''/g")"
    100 }}
    101 
    102 define-command -hidden go-indent-on-new-line %~
    103     evaluate-commands -draft -itersel %=
    104         # preserve previous line indent
    105         try %{ execute-keys -draft <semicolon>K<a-&> }
    106         # cleanup trailing white spaces on the previous line
    107         try %{ execute-keys -draft kx s \h+$ <ret>d }
    108         try %<
    109             try %{ # line comment
    110                 execute-keys -draft kx s ^\h*// <ret>
    111             } catch %{ # block comment
    112                 execute-keys -draft <a-?> /\* <ret> <a-K>\*/<ret>
    113             }
    114         > catch %<
    115             # indent after lines with an unclosed { or (
    116             try %< execute-keys -draft [c[({],[)}] <ret> <a-k> \A[({]\N*\n\N*\n?\z <ret> j<a-gt> >
    117             # indent after a switch's case/default statements
    118             try %[ execute-keys -draft kx <a-k> ^\h*(case|default).*:$ <ret> j<a-gt> ]
    119             # deindent closing brace(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 
    125 define-command -hidden go-indent-on-opening-curly-brace %[
    126     # align indent with opening paren when { is entered on a new line after the closing paren
    127     try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ]
    128 ]
    129 
    130 define-command -hidden go-indent-on-closing-curly-brace %[
    131     # align to opening curly brace when alone on a line
    132     try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ]
    133 ]
    134 
    135 define-command -hidden go-insert-comment-on-new-line %[
    136     evaluate-commands -no-hooks -draft -itersel %[
    137         # copy // comments prefix and following white spaces
    138         try %{
    139             execute-keys -draft <semicolon><c-s>kx s ^\h*\K/{2,}\h* <ret> y<c-o>P<esc>
    140             # check for empty comments and delete them
    141             try %{
    142                 execute-keys kx<a-K>^\h*//+\h*$<ret>
    143             } catch %{
    144                 execute-keys Jx_d
    145             }
    146         }
    147     ]
    148 ]
    149 
    150 define-command -hidden go-insert-closing-delimiter-on-new-line %[
    151     evaluate-commands -no-hooks -draft -itersel %[
    152         # Wisely add '}'.
    153         evaluate-commands -save-regs x %[
    154             # Save previous line indent in register x.
    155             try %[ execute-keys -draft kxs^\h+<ret>"xy ] catch %[ reg x '' ]
    156             try %[
    157                 # Validate previous line and that it is not closed yet.
    158                 execute-keys -draft kx <a-k>^<c-r>x.*\{\h*\(?\h*$<ret> j}iJx <a-K>^<c-r>x\)?\h*\}<ret>
    159                 # Insert closing '}'.
    160                 execute-keys -draft o<c-r>x}<esc>
    161                 # Delete trailing '}' on the line below the '{'.
    162                 execute-keys -draft xs\}$<ret>d
    163             ]
    164         ]
    165 
    166         # Wisely add ')'.
    167         evaluate-commands -save-regs x %[
    168             # Save previous line indent in register x.
    169             try %[ execute-keys -draft kxs^\h+<ret>"xy ] catch %[ reg x '' ]
    170             try %[
    171                 # Validate previous line and that it is not closed yet.
    172                 execute-keys -draft kx <a-k>^<c-r>x.*\(\h*$<ret> J}iJx <a-K>^<c-r>x\)<ret>
    173                 # Insert closing ')'.
    174                 execute-keys -draft o<c-r>x)<esc>
    175                 # Delete trailing ')' on the line below the '('.
    176                 execute-keys -draft xs\)\h*\}?\h*$<ret>d
    177             ]
    178         ]
    179     ]
    180 ]
    181 
    182 §