diff --git a/rc/filetype/go.kak b/rc/filetype/go.kak index c993c889..c32346a9 100644 --- a/rc/filetype/go.kak +++ b/rc/filetype/go.kak @@ -22,6 +22,7 @@ hook global WinSetOption filetype=go %{ 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 window InsertChar \n -group go-insert-closing-delimiter go-insert-closing-delimiter-on-new-line alias window alt go-alternative-file @@ -101,16 +102,22 @@ define-command -hidden go-indent-on-new-line %~ evaluate-commands -draft -itersel %= # preserve previous line indent try %{ execute-keys -draft 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 ] + try %{ + try %{ # line comment + execute-keys -draft k s ^\h*// + } catch %{ # block comment + execute-keys -draft /\* \*/ + } + } catch %{ + # indent after lines with an unclosed { or ( + try %< execute-keys -draft [c[({],[)}] \A[({][^\n]*\n[^\n]*\n?\z j > + # 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 ] + } = ~ @@ -128,7 +135,11 @@ 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 } + ] +] +define-command -hidden go-insert-closing-delimiter-on-new-line %[ + evaluate-commands -no-hooks -draft -itersel %[ # Wisely add '}'. evaluate-commands -save-regs x %[ # Save previous line indent in register x. diff --git a/test/indent/go/deindent-generic-closing-brace/in b/test/indent/go/deindent-generic-closing-brace/in index b360ff42..71631915 100644 --- a/test/indent/go/deindent-generic-closing-brace/in +++ b/test/indent/go/deindent-generic-closing-brace/in @@ -47,3 +47,9 @@ a := []int{ someFunction(%( ) ), } + +14 +foobar(baz,%( ) + +15 +foobar{ // some comment%( ) diff --git a/test/indent/go/deindent-generic-closing-brace/out b/test/indent/go/deindent-generic-closing-brace/out index 26d936fe..d36e493b 100644 --- a/test/indent/go/deindent-generic-closing-brace/out +++ b/test/indent/go/deindent-generic-closing-brace/out @@ -63,3 +63,11 @@ a := []int{ ), } + +14 +foobar(baz, + + +15 +foobar{ // some comment + diff --git a/test/indent/go/insert-comment/cmd b/test/indent/go/insert-comment/cmd new file mode 100644 index 00000000..e3036a40 --- /dev/null +++ b/test/indent/go/insert-comment/cmd @@ -0,0 +1 @@ +c diff --git a/test/indent/go/insert-comment/in b/test/indent/go/insert-comment/in new file mode 100644 index 00000000..fef0c47d --- /dev/null +++ b/test/indent/go/insert-comment/in @@ -0,0 +1,11 @@ +1 +// foo(bar,%( ) + +2 + // foo(bar,%( ) + +3 +// foo{%( ) + +4 + // foo{%( ) diff --git a/test/indent/go/insert-comment/out b/test/indent/go/insert-comment/out new file mode 100644 index 00000000..4d654000 --- /dev/null +++ b/test/indent/go/insert-comment/out @@ -0,0 +1,15 @@ +1 +// foo(bar, +// + +2 + // foo(bar, + // + +3 +// foo{ +// + +4 + // foo{ + // diff --git a/test/indent/go/insert-comment/rc b/test/indent/go/insert-comment/rc new file mode 100644 index 00000000..6e3ca7fd --- /dev/null +++ b/test/indent/go/insert-comment/rc @@ -0,0 +1,4 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/go.kak" +set buffer filetype go +set-option global disabled_hooks go-insert-closing-delimiter