From 039a10a34cd8dd638b6436dfac11a70effe7a728 Mon Sep 17 00:00:00 2001 From: Tony Gong Date: Fri, 22 Apr 2022 20:51:00 -0700 Subject: [PATCH] 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