2015-07-21 11:22:57 +03:00
|
|
|
# https://golang.org/
|
|
|
|
#
|
|
|
|
|
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2015-09-04 14:51:36 +03:00
|
|
|
hook global BufCreate .*\.go %{
|
2017-11-03 10:34:41 +03:00
|
|
|
set-option buffer filetype go
|
2015-07-21 11:22:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
# Highlighters
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2018-07-01 12:53:35 +03:00
|
|
|
add-highlighter shared/go regions
|
|
|
|
add-highlighter shared/go/code default-region group
|
2018-07-02 13:59:12 +03:00
|
|
|
add-highlighter shared/go/back_string region '`' '`' fill string
|
|
|
|
add-highlighter shared/go/double_string region '"' (?<!\\)(\\\\)*" fill string
|
|
|
|
add-highlighter shared/go/single_string region "'" (?<!\\)(\\\\)*' fill string
|
|
|
|
add-highlighter shared/go/comment region /\* \*/ fill comment
|
|
|
|
add-highlighter shared/go/comment_line region '//' $ fill comment
|
2015-07-21 11:22:57 +03:00
|
|
|
|
2018-07-01 12:53:35 +03:00
|
|
|
add-highlighter shared/go/code/ regex %{-?([0-9]*\.(?!0[xX]))?\b([0-9]+|0[xX][0-9a-fA-F]+)\.?([eE][+-]?[0-9]+)?i?\b} 0:value
|
2016-03-14 15:08:51 +03:00
|
|
|
|
2018-05-07 00:29:52 +03:00
|
|
|
evaluate-commands %sh{
|
2016-03-14 15:08:51 +03:00
|
|
|
# Grammar
|
2016-11-28 17:06:26 +03:00
|
|
|
keywords="break|default|func|interface|select|case|defer|go|map|struct"
|
2016-11-11 16:07:45 +03:00
|
|
|
keywords="${keywords}|chan|else|goto|package|switch|const|fallthrough|if|range|type"
|
|
|
|
keywords="${keywords}|continue|for|import|return|var"
|
2016-11-28 17:06:26 +03:00
|
|
|
types="bool|byte|chan|complex128|complex64|error|float32|float64|int|int16|int32"
|
2016-03-14 19:29:11 +03:00
|
|
|
types="${types}|int64|int8|interface|intptr|map|rune|string|struct|uint|uint16|uint32|uint64|uint8"
|
2016-11-11 16:07:45 +03:00
|
|
|
values="false|true|nil|iota"
|
|
|
|
functions="append|cap|close|complex|copy|delete|imag|len|make|new|panic|print|println|real|recover"
|
2016-03-14 15:08:51 +03:00
|
|
|
|
|
|
|
# Add the language's grammar to the static completion list
|
2016-10-08 22:42:43 +03:00
|
|
|
printf %s\\n "hook global WinSetOption filetype=go %{
|
2018-06-22 06:14:51 +03:00
|
|
|
set-option window static_words ${keywords} ${attributes} ${types} ${values} ${functions}
|
2018-06-17 06:30:40 +03:00
|
|
|
}" | tr '|' ' '
|
2016-03-14 15:08:51 +03:00
|
|
|
|
|
|
|
# Highlight keywords
|
2016-03-17 16:30:14 +03:00
|
|
|
printf %s "
|
2018-07-01 12:53:35 +03:00
|
|
|
add-highlighter shared/go/code/ regex \b(${keywords})\b 0:keyword
|
|
|
|
add-highlighter shared/go/code/ regex \b(${attributes})\b 0:attribute
|
|
|
|
add-highlighter shared/go/code/ regex \b(${types})\b 0:type
|
|
|
|
add-highlighter shared/go/code/ regex \b(${values})\b 0:value
|
|
|
|
add-highlighter shared/go/code/ regex \b(${functions})\b 0:builtin
|
2016-03-14 15:08:51 +03:00
|
|
|
"
|
|
|
|
}
|
2015-07-21 11:22:57 +03:00
|
|
|
|
|
|
|
# Commands
|
|
|
|
# ‾‾‾‾‾‾‾‾
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command -hidden go-indent-on-new-line %~
|
2017-11-03 11:09:45 +03:00
|
|
|
evaluate-commands -draft -itersel %=
|
2015-07-21 11:22:57 +03:00
|
|
|
# preserve previous line indent
|
2017-11-03 11:09:45 +03:00
|
|
|
try %{ execute-keys -draft \;K<a-&> }
|
2015-07-21 11:22:57 +03:00
|
|
|
# indent after lines ending with { or (
|
2017-11-03 11:09:45 +03:00
|
|
|
try %[ execute-keys -draft k<a-x> <a-k> [{(]\h*$ <ret> j<a-gt> ]
|
2015-07-21 11:22:57 +03:00
|
|
|
# cleanup trailing white spaces on the previous line
|
2017-11-03 11:09:45 +03:00
|
|
|
try %{ execute-keys -draft k<a-x> s \h+$ <ret>d }
|
2015-07-21 11:22:57 +03:00
|
|
|
# align to opening paren of previous line
|
2017-11-03 11:09:45 +03:00
|
|
|
try %{ execute-keys -draft [( <a-k> \A\([^\n]+\n[^\n]*\n?\z <ret> s \A\(\h*.|.\z <ret> '<a-;>' & }
|
2015-07-21 11:22:57 +03:00
|
|
|
# copy // comments prefix
|
2018-05-13 10:24:37 +03:00
|
|
|
try %{ execute-keys -draft \;<c-s>k<a-x> s ^\h*\K/{2,} <ret> y<c-o>P<esc> }
|
2015-07-21 11:22:57 +03:00
|
|
|
# indent after a switch's case/default statements
|
2017-11-03 11:09:45 +03:00
|
|
|
try %[ execute-keys -draft k<a-x> <a-k> ^\h*(case|default).*:$ <ret> j<a-gt> ]
|
2015-07-21 11:22:57 +03:00
|
|
|
# indent after if|else|while|for
|
2017-11-03 11:09:45 +03:00
|
|
|
try %[ execute-keys -draft \;<a-F>)MB <a-k> \A(if|else|while|for)\h*\(.*\)\h*\n\h*\n?\z <ret> s \A|.\z <ret> 1<a-&>1<a-space><a-gt> ]
|
2015-07-21 11:22:57 +03:00
|
|
|
=
|
|
|
|
~
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command -hidden go-indent-on-opening-curly-brace %[
|
2015-07-21 11:22:57 +03:00
|
|
|
# align indent with opening paren when { is entered on a new line after the closing paren
|
2017-11-03 11:09:45 +03:00
|
|
|
try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ]
|
2015-07-21 11:22:57 +03:00
|
|
|
]
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command -hidden go-indent-on-closing-curly-brace %[
|
2015-07-21 11:22:57 +03:00
|
|
|
# align to opening curly brace when alone on a line
|
2017-11-03 11:09:45 +03:00
|
|
|
try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ]
|
2015-07-21 11:22:57 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
# Initialization
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2018-07-01 12:53:35 +03:00
|
|
|
hook -group go-highlight global WinSetOption filetype=go %{ add-highlighter window/go ref go }
|
2015-07-21 11:22:57 +03:00
|
|
|
|
2016-10-08 22:42:43 +03:00
|
|
|
hook global WinSetOption filetype=go %{
|
2015-07-21 11:22:57 +03:00
|
|
|
# cleanup trailing whitespaces when exiting insert mode
|
2018-01-21 02:35:05 +03:00
|
|
|
hook window ModeChange insert:.* -group go-hooks %{ try %{ execute-keys -draft <a-x>s^\h+$<ret>d } }
|
2017-01-13 03:56:30 +03:00
|
|
|
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
|
2015-07-21 11:22:57 +03:00
|
|
|
}
|
|
|
|
|
2017-10-28 06:00:51 +03:00
|
|
|
hook -group go-highlight global WinSetOption filetype=(?!go).* %{ remove-highlighter window/go }
|
2015-09-06 18:14:17 +03:00
|
|
|
|
2016-10-08 22:42:43 +03:00
|
|
|
hook global WinSetOption filetype=(?!go).* %{
|
2018-09-19 20:59:57 +03:00
|
|
|
remove-hooks window go-.+
|
2015-07-21 11:22:57 +03:00
|
|
|
}
|