diff --git a/rc/filetype/go.kak b/rc/filetype/go.kak index 5d077991..4cb91e83 100644 --- a/rc/filetype/go.kak +++ b/rc/filetype/go.kak @@ -21,6 +21,7 @@ hook global WinSetOption filetype=go %{ 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 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 "${values}" '|'))\b 0:value 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 k s ^\h*\K/{2,} yP } # indent after a switch's case/default statements try %[ execute-keys -draft k ^\h*(case|default).*:$ j ] - # indent after if|else|while|for - try %[ execute-keys -draft )MB \A(if|else|while|for)\h*\(.*\)\h*\n\h*\n?\z s \A|.\z 11 ] # deindent closing brace(s) when after cursor try %[ execute-keys -draft ^\h*[})] gh / [})] m 1 ] = @@ -104,4 +104,36 @@ define-command -hidden go-indent-on-closing-curly-brace %[ try %[ execute-keys -itersel -draft ^\h+\}$hms\A|.\z1 ] ] +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 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(\)\h*\}?)$ + # Insert closing ')'. + execute-keys -draft ox) + # Delete trailing ')' on the line below the '('. + execute-keys -draft Xs\)\h*\}?\h*$d + ] + ] + ] +] + ยง diff --git a/test/indent/go/deindent-function-closing-brace/out b/test/indent/go/deindent-function-closing-brace/out index 3a5e4869..cf5fd177 100644 --- a/test/indent/go/deindent-function-closing-brace/out +++ b/test/indent/go/deindent-function-closing-brace/out @@ -6,7 +6,8 @@ func foo(x int) int { } func foo(x int) int { - bar()} + bar() +} func foo(x int) int { bar() diff --git a/test/indent/go/deindent-generic-closing-brace/in b/test/indent/go/deindent-generic-closing-brace/in index 5a31c18f..ef741602 100644 --- a/test/indent/go/deindent-generic-closing-brace/in +++ b/test/indent/go/deindent-generic-closing-brace/in @@ -1,31 +1,43 @@ +1 {%( )} +2 {%( ) } +3 {%( )bar()} +4 {%( )bar() } +5 { bar()%( )} +6 { bar()%( )} +7 {(%( ))} +8 {(%( ) )} +9 {(%( )foo())} +10 {(%( )foo() )} +11 {( bar()%( ))} +12 {( bar()%( ))} diff --git a/test/indent/go/deindent-generic-closing-brace/out b/test/indent/go/deindent-generic-closing-brace/out index cfd6db40..e3aaa065 100644 --- a/test/indent/go/deindent-generic-closing-brace/out +++ b/test/indent/go/deindent-generic-closing-brace/out @@ -1,43 +1,58 @@ +1 { } +2 { } -{ - bar()} - +3 { bar() } +4 { bar() } +5 +{ + bar() +} + +6 { bar() } +7 {( )} +8 {( )} +9 {( - foo())} + foo() +) +} +10 {( foo() )} +11 {( bar() )} +12 {( bar() )} diff --git a/test/indent/go/deindent-if-closing-brace/out b/test/indent/go/deindent-if-closing-brace/out index 2c63bd1c..f133c197 100644 --- a/test/indent/go/deindent-if-closing-brace/out +++ b/test/indent/go/deindent-if-closing-brace/out @@ -6,7 +6,8 @@ if true { } if true { - bar()} + bar() +} if true { bar()