Provide better support for Go language.

1. Highlight short variable declaration operator :=.
2. 'while' is not go keyword.
3. Auto insert ')', '}', when line ends with '(' or '{'.
This commit is contained in:
Michał Kruszewski 2020-09-04 20:21:35 +02:00
parent 7d358e9ea1
commit 0f63dd339a
5 changed files with 69 additions and 8 deletions

View File

@ -21,6 +21,7 @@ hook global WinSetOption filetype=go %{
hook window InsertChar \n -group go-indent go-indent-on-new-line 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-opening-curly-brace
hook window InsertChar \} -group go-indent go-indent-on-closing-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
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window go-.+ } hook -once -always window WinSetOption filetype=.* %{ remove-hooks window go-.+ }
} }
@ -67,6 +68,7 @@ evaluate-commands %sh{
add-highlighter shared/go/code/ regex \b($(join "${types}" '|'))\b 0:type add-highlighter shared/go/code/ regex \b($(join "${types}" '|'))\b 0:type
add-highlighter shared/go/code/ regex \b($(join "${values}" '|'))\b 0:value add-highlighter shared/go/code/ regex \b($(join "${values}" '|'))\b 0:value
add-highlighter shared/go/code/ regex \b($(join "${functions}" '|'))\b 0:builtin add-highlighter shared/go/code/ regex \b($(join "${functions}" '|'))\b 0:builtin
add-highlighter shared/go/code/ regex := 0:attribute
" "
} }
@ -87,8 +89,6 @@ define-command -hidden go-indent-on-new-line %~
try %{ execute-keys -draft <semicolon><c-s>k<a-x> s ^\h*\K/{2,} <ret> y<c-o>P<esc> } try %{ execute-keys -draft <semicolon><c-s>k<a-x> s ^\h*\K/{2,} <ret> y<c-o>P<esc> }
# indent after a switch's case/default statements # indent after a switch's case/default statements
try %[ execute-keys -draft k<a-x> <a-k> ^\h*(case|default).*:$ <ret> j<a-gt> ] try %[ execute-keys -draft k<a-x> <a-k> ^\h*(case|default).*:$ <ret> j<a-gt> ]
# indent after if|else|while|for
try %[ execute-keys -draft <semicolon><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> ]
# deindent closing brace(s) when after cursor # deindent closing brace(s) when after cursor
try %[ execute-keys -draft <a-x> <a-k> ^\h*[})] <ret> gh / [})] <ret> m <a-S> 1<a-&> ] try %[ execute-keys -draft <a-x> <a-k> ^\h*[})] <ret> gh / [})] <ret> m <a-S> 1<a-&> ]
= =
@ -104,4 +104,36 @@ define-command -hidden go-indent-on-closing-curly-brace %[
try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ] try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ]
] ]
define-command -hidden go-insert-on-new-line %[
evaluate-commands -no-hooks -draft -itersel %[
# Wisely add '}'.
evaluate-commands -save-regs x %[
# Save previous line indent in register x.
try %[ execute-keys -draft k<a-x>s^\h+<ret>"xy ] catch %[ reg x '' ]
try %[
# Validate previous line and that it is not closed yet.
execute-keys -draft k<a-x> <a-k>^<c-r>x.*\{\h*\(?\h*$<ret> J}iJ<a-x> <a-K>^<c-r>x(\)?\h*\})$<ret>
# Insert closing '}'.
execute-keys -draft o<c-r>x}<esc>
# Delete trailing '}' on the line below the '{'.
execute-keys -draft Xs\}$<ret>d
]
]
# Wisely add ')'.
evaluate-commands -save-regs x %[
# Save previous line indent in register x.
try %[ execute-keys -draft k<a-x>s^\h+<ret>"xy ] catch %[ reg x '' ]
try %[
# Validate previous line and that it is not closed yet.
execute-keys -draft k<a-x> <a-k>^<c-r>x.*\(\h*$<ret> J}iJ<a-x> <a-K>^<c-r>x(\)\h*\}?)$<ret>
# Insert closing ')'.
execute-keys -draft o<c-r>x)<esc>
# Delete trailing ')' on the line below the '('.
execute-keys -draft Xs\)\h*\}?\h*$<ret>d
]
]
]
]
§ §

View File

@ -6,7 +6,8 @@ func foo(x int) int {
} }
func foo(x int) int { func foo(x int) int {
bar()} bar()
}
func foo(x int) int { func foo(x int) int {
bar() bar()

View File

@ -1,31 +1,43 @@
1
{%( )} {%( )}
2
{%( ) {%( )
} }
3
{%( )bar()} {%( )bar()}
4
{%( )bar() {%( )bar()
} }
5
{ {
bar()%( )} bar()%( )}
6
{ {
bar()%( )} bar()%( )}
7
{(%( ))} {(%( ))}
8
{(%( ) {(%( )
)} )}
9
{(%( )foo())} {(%( )foo())}
10
{(%( )foo() {(%( )foo()
)} )}
11
{( {(
bar()%( ))} bar()%( ))}
12
{( {(
bar()%( ))} bar()%( ))}

View File

@ -1,43 +1,58 @@
1
{ {
} }
2
{ {
} }
{ 3
bar()}
{ {
bar() bar()
} }
4
{ {
bar() bar()
} }
5
{
bar()
}
6
{ {
bar() bar()
} }
7
{( {(
)} )}
8
{( {(
)} )}
9
{( {(
foo())} foo()
)
}
10
{( {(
foo() foo()
)} )}
11
{( {(
bar() bar()
)} )}
12
{( {(
bar() bar()
)} )}

View File

@ -6,7 +6,8 @@ if true {
} }
if true { if true {
bar()} bar()
}
if true { if true {
bar() bar()