Skip to content

Commit 3d0aa6a

Browse files
committed
improve inserting comments on new lines for go
Now it matches the behavior from c-family, python, and zig
1 parent 9226072 commit 3d0aa6a

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

rc/filetype/go.kak

+17-5
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,24 @@ define-command -hidden go-indent-on-closing-curly-brace %[
132132
try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ]
133133
]
134134

135-
define-command -hidden go-insert-comment-on-new-line %[
136-
evaluate-commands -no-hooks -draft -itersel %[
137-
# copy // comments prefix and following white spaces
138-
try %{ execute-keys -draft <semicolon><c-s>kx s ^\h*\K/{2,}\h* <ret> y<c-o>P<esc> }
135+
define-command -hidden go-insert-comment-on-new-line %[ evaluate-commands -itersel -draft %[
136+
execute-keys <semicolon>
137+
try %[
138+
evaluate-commands -draft -save-regs '/"' %[
139+
# copy the commenting prefix
140+
execute-keys -save-regs '' k x1s^\h*(//+\h*)<ret> y
141+
try %[
142+
# if the previous comment isn't empty, create a new one
143+
execute-keys x<a-K>^\h*//+\h*$<ret> jxs^\h*<ret>P
144+
] catch %[
145+
# if there is no text in the previous comment, remove it completely
146+
execute-keys d
147+
]
148+
]
149+
# trim trailing whitespace on the previous line
150+
try %[ execute-keys -draft k x s\h+$<ret> d ]
139151
]
140-
]
152+
] ]
141153

142154
define-command -hidden go-insert-closing-delimiter-on-new-line %[
143155
evaluate-commands -no-hooks -draft -itersel %[

0 commit comments

Comments
 (0)