# https://golang.org/ # # Detection # ‾‾‾‾‾‾‾‾‾ hook global BufCreate .*\.go %{ set-option buffer filetype go } # Initialization # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ hook global WinSetOption filetype=go %{ require-module go set-option window static_words %opt{go_static_words} # cleanup trailing whitespaces when exiting insert mode hook window ModeChange pop:insert:.* -group go-trim-indent %{ try %{ execute-keys -draft s^\h+$d } } hook window InsertChar \n -group go-indent go-indent-on-new-line hook window InsertChar \{ -group go-indent go-indent-on-opening-curly-brace hook window InsertChar \} -group go-indent go-indent-on-closing-curly-brace hook window InsertChar \n -group go-insert go-insert-on-new-line alias window alt go-alternative-file hook -once -always window WinSetOption filetype=.* %{ remove-hooks window go-.+ unalias window alt go-alternative-file } } hook -group go-highlight global WinSetOption filetype=go %{ add-highlighter window/go ref go hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/go } } provide-module go %§ # Highlighters # ‾‾‾‾‾‾‾‾‾‾‾‾ add-highlighter shared/go regions add-highlighter shared/go/code default-region group add-highlighter shared/go/back_string region '`' '`' fill string add-highlighter shared/go/double_string region '"' (?K } # indent after lines ending with { or ( try %[ execute-keys -draft k [{(]\h*$ j ] # cleanup trailing white spaces on the previous line try %{ execute-keys -draft k s \h+$ d } # align to opening paren of previous line try %{ execute-keys -draft [( \A\([^\n]+\n[^\n]*\n?\z s \A\(\h*.|.\z '' & } # indent after a switch's case/default statements try %[ execute-keys -draft k ^\h*(case|default).*:$ j ] # deindent closing brace(s) when after cursor try %[ execute-keys -draft ^\h*[})] gh / [})] m 1 ] = ~ define-command -hidden go-indent-on-opening-curly-brace %[ # align indent with opening paren when { is entered on a new line after the closing paren try %[ execute-keys -draft -itersel h)M \A\(.*\)\h*\n\h*\{\z s \A|.\z 1 ] ] define-command -hidden go-indent-on-closing-curly-brace %[ # align to opening curly brace when alone on a line try %[ execute-keys -itersel -draft ^\h+\}$hms\A|.\z1 ] ] define-command -hidden go-insert-on-new-line %[ evaluate-commands -no-hooks -draft -itersel %[ # copy // comments prefix and following white spaces try %{ execute-keys -draft k s ^\h*\K/{2,}\h* yP } # Wisely add '}'. evaluate-commands -save-regs x %[ # Save previous line indent in register x. try %[ execute-keys -draft ks^\h+"xy ] catch %[ reg x '' ] try %[ # Validate previous line and that it is not closed yet. execute-keys -draft k ^x.*\{\h*\(?\h*$ j}iJ ^x\)?\h*\} # Insert closing '}'. execute-keys -draft ox} # Delete trailing '}' on the line below the '{'. execute-keys -draft Xs\}$d ] ] # Wisely add ')'. evaluate-commands -save-regs x %[ # Save previous line indent in register x. try %[ execute-keys -draft ks^\h+"xy ] catch %[ reg x '' ] try %[ # Validate previous line and that it is not closed yet. execute-keys -draft k ^x.*\(\h*$ J}iJ ^x\) # Insert closing ')'. execute-keys -draft ox) # Delete trailing ')' on the line below the '('. execute-keys -draft Xs\)\h*\}?\h*$d ] ] ] ] §