Skip to content

Commit

Permalink
Merge remote-tracking branch 'ttttcrngyblflpp/golang_comment_indent'
Browse files Browse the repository at this point in the history
  • Loading branch information
mawww committed Jun 13, 2022
2 parents a8d6a5b + 680dc8c commit b92100c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 8 deletions.
27 changes: 19 additions & 8 deletions rc/filetype/go.kak
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -101,16 +102,22 @@ define-command -hidden go-indent-on-new-line %~
evaluate-commands -draft -itersel %=
# preserve previous line indent
try %{ execute-keys -draft <semicolon>K<a-&> }
# indent after lines ending with { or (
try %[ execute-keys -draft k<a-x> <a-k> [{(]\h*$ <ret> j<a-gt> ]
# cleanup trailing white spaces on the previous line
try %{ execute-keys -draft k<a-x> s \h+$ <ret>d }
# align to opening paren of previous line
try %{ execute-keys -draft [( <a-k> \A\([^\n]+\n[^\n]*\n?\z <ret> s \A\(\h*.|.\z <ret> '<a-;>' & }
# indent after a switch's case/default statements
try %[ execute-keys -draft k<a-x> <a-k> ^\h*(case|default).*:$ <ret> j<a-gt> ]
# deindent closing brace(s) when after cursor
try %[ execute-keys -draft <a-x> <a-k> ^\h*[})] <ret> gh / [})] <ret> m <a-S> 1<a-&> ]
try %{
try %{ # line comment
execute-keys -draft k<a-x> s ^\h*// <ret>
} catch %{ # block comment
execute-keys -draft <a-?> /\* <ret> <a-K>\*/<ret>
}
} catch %{
# indent after lines with an unclosed { or (
try %< execute-keys -draft [c[({],[)}] <ret> <a-k> \A[({][^\n]*\n[^\n]*\n?\z <ret> j<a-gt> >
# indent after a switch's case/default statements
try %[ execute-keys -draft k<a-x> <a-k> ^\h*(case|default).*:$ <ret> j<a-gt> ]
# deindent closing brace(s) when after cursor
try %[ execute-keys -draft <a-x> <a-k> ^\h*[})] <ret> gh / [})] <ret> m <a-S> 1<a-&> ]
}
=
~

Expand All @@ -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 <semicolon><c-s>k<a-x> s ^\h*\K/{2,}\h* <ret> y<c-o>P<esc> }
]
]

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.
Expand Down
6 changes: 6 additions & 0 deletions test/indent/go/deindent-generic-closing-brace/in
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ a := []int{
someFunction(%( )
),
}

14
foobar(baz,%( )

15
foobar{ // some comment%( )
8 changes: 8 additions & 0 deletions test/indent/go/deindent-generic-closing-brace/out
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ a := []int{

),
}

14
foobar(baz,


15
foobar{ // some comment

1 change: 1 addition & 0 deletions test/indent/go/insert-comment/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c<ret>
11 changes: 11 additions & 0 deletions test/indent/go/insert-comment/in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
1
// foo(bar,%( )

2
// foo(bar,%( )

3
// foo{%( )

4
// foo{%( )
15 changes: 15 additions & 0 deletions test/indent/go/insert-comment/out
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1
// foo(bar,
//

2
// foo(bar,
//

3
// foo{
//

4
// foo{
//
4 changes: 4 additions & 0 deletions test/indent/go/insert-comment/rc
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b92100c

Please sign in to comment.