From 6f801c6cda41f5cc99c4bb8f8319a36fede51f23 Mon Sep 17 00:00:00 2001 From: Tony Gong Date: Sat, 4 Jun 2022 07:34:37 -0700 Subject: [PATCH 2/4] Golang separate hook group for inserting ) and } Add a separate hook group for inserting ) and } on newline because the current implementation does not work in 100% of cases and should be able to be disabled independently of copying comment characters (which is much easier in comparison to get right) if one does not care about this feature. --- rc/filetype/go.kak | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rc/filetype/go.kak b/rc/filetype/go.kak index c993c889..9df7f12c 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 @@ -128,7 +129,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. From 039a10a34cd8dd638b6436dfac11a70effe7a728 Mon Sep 17 00:00:00 2001 From: Tony Gong Date: Fri, 22 Apr 2022 20:51:00 -0700 Subject: [PATCH 3/4] Do not indent Golang comments as code When indenting on newline in Go files, only remove trailing whitespace on the previous line and copy indentation of the previous line if in comment context. Added regression tests. --- rc/filetype/go.kak | 24 ++++++++++++++++-------- test/indent/go/insert-comment/cmd | 1 + test/indent/go/insert-comment/in | 11 +++++++++++ test/indent/go/insert-comment/out | 15 +++++++++++++++ test/indent/go/insert-comment/rc | 4 ++++ 5 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 test/indent/go/insert-comment/cmd create mode 100644 test/indent/go/insert-comment/in create mode 100644 test/indent/go/insert-comment/out create mode 100644 test/indent/go/insert-comment/rc diff --git a/rc/filetype/go.kak b/rc/filetype/go.kak index 9df7f12c..13a4ce8d 100644 --- a/rc/filetype/go.kak +++ b/rc/filetype/go.kak @@ -102,16 +102,24 @@ 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 ending with { or ( + try %[ execute-keys -draft k [{(]\h*$ j ] + # 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 ] + } = ~ 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 From 680dc8cd919ed7846ca165ce0e26705df7e1b585 Mon Sep 17 00:00:00 2001 From: Tony Gong Date: Sat, 4 Jun 2022 14:53:18 -0700 Subject: [PATCH 4/4] Golang do not align to open ( or { Changed the indentation behavior such that an extra level of indentation is added after a line containing a ( or { that is not closed on the same line instead of aligning to the unclosed ( or {. This is consistent with how `go fmt` formats source code. Added regression tests. --- rc/filetype/go.kak | 6 ++---- test/indent/go/deindent-generic-closing-brace/in | 6 ++++++ test/indent/go/deindent-generic-closing-brace/out | 8 ++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/rc/filetype/go.kak b/rc/filetype/go.kak index 13a4ce8d..c32346a9 100644 --- a/rc/filetype/go.kak +++ b/rc/filetype/go.kak @@ -111,10 +111,8 @@ define-command -hidden go-indent-on-new-line %~ execute-keys -draft /\* \*/ } } catch %{ - # indent after lines ending with { or ( - try %[ execute-keys -draft k [{(]\h*$ j ] - # align to opening paren of previous line - try %{ execute-keys -draft [( \A\([^\n]+\n[^\n]*\n?\z s \A\(\h*.|.\z '' & } + # 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 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 +