# https://golang.org/ # # Detection # ‾‾‾‾‾‾‾‾‾ hook global BufCreate .*\.go %{ set buffer filetype go } # Highlighters # ‾‾‾‾‾‾‾‾‾‾‾‾ add-highlighter shared/ regions -default code go \ back_string '`' '`' '' \ double_string '"' (? } # indent after lines ending with { or ( try %[ exec -draft k [{(]\h*$ j ] # cleanup trailing white spaces on the previous line try %{ exec -draft k s \h+$ d } # align to opening paren of previous line try %{ exec -draft [( \A\([^\n]+\n[^\n]*\n?\z s \A\(\h*.|.\z '' & } # copy // comments prefix try %{ exec -draft \;k s ^\h*\K/{2,} yP } # indent after a switch's case/default statements try %[ exec -draft k ^\h*(case|default).*:$ j ] # indent after if|else|while|for try %[ exec -draft \;)MB \A(if|else|while|for)\h*\(.*\)\h*\n\h*\n?\z s \A|.\z 11 ] = ~ def -hidden go-indent-on-opening-curly-brace %[ # align indent with opening paren when { is entered on a new line after the closing paren try %[ exec -draft -itersel h)M \A\(.*\)\h*\n\h*\{\z s \A|.\z 1 ] ] def -hidden go-indent-on-closing-curly-brace %[ # align to opening curly brace when alone on a line try %[ exec -itersel -draft ^\h+\}$hms\A|.\z1 ] ] # Initialization # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ hook -group go-highlight global WinSetOption filetype=go %{ add-highlighter window ref go } hook global WinSetOption filetype=go %{ # cleanup trailing whitespaces when exiting insert mode hook window InsertEnd .* -group go-hooks %{ try %{ exec -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 -group go-highlight global WinSetOption filetype=(?!go).* %{ remove-highlighter window/go } hook global WinSetOption filetype=(?!go).* %{ remove-hooks window go-hooks remove-hooks window go-indent }