Add support for autoformatting after do keyword in Ruby

This commit is contained in:
Dmitry Matveyev 2019-06-08 00:25:33 +03:00
parent 09e1ec97a9
commit 01159a9e72
5 changed files with 20 additions and 7 deletions

View File

@ -17,8 +17,8 @@ hook global WinSetOption filetype=ruby %{
set-option window static_words %opt{ruby_static_words} set-option window static_words %opt{ruby_static_words}
hook window InsertChar .* -group ruby-indent ruby-indent-on-char hook window InsertChar .* -group ruby-indent ruby-indent-on-char
hook window InsertChar \n -group ruby-insert ruby-insert-on-new-line
hook window InsertChar \n -group ruby-indent ruby-indent-on-new-line hook window InsertChar \n -group ruby-indent ruby-indent-on-new-line
hook window InsertChar \n -group ruby-insert ruby-insert-on-new-line
alias window alt ruby-alternative-file alias window alt ruby-alternative-file
@ -142,7 +142,7 @@ define-command -hidden ruby-indent-on-char %{
try %{ execute-keys -draft <a-x> <a-k> ^ \h * (else|elsif) $ <ret> <a-\;> <a-?> ^ \h * (if) <ret> s \A | \z <ret> ) <a-&> } try %{ execute-keys -draft <a-x> <a-k> ^ \h * (else|elsif) $ <ret> <a-\;> <a-?> ^ \h * (if) <ret> s \A | \z <ret> ) <a-&> }
try %{ execute-keys -draft <a-x> <a-k> ^ \h * (when) $ <ret> <a-\;> <a-?> ^ \h * (case) <ret> s \A | \z <ret> ) <a-&> } try %{ execute-keys -draft <a-x> <a-k> ^ \h * (when) $ <ret> <a-\;> <a-?> ^ \h * (case) <ret> s \A | \z <ret> ) <a-&> }
try %{ execute-keys -draft <a-x> <a-k> ^ \h * (rescue) $ <ret> <a-\;> <a-?> ^ \h * (begin) <ret> s \A | \z <ret> ) <a-&> } try %{ execute-keys -draft <a-x> <a-k> ^ \h * (rescue) $ <ret> <a-\;> <a-?> ^ \h * (begin) <ret> s \A | \z <ret> ) <a-&> }
try %{ execute-keys -draft <a-x> <a-k> ^ \h * (end) $ <ret> <a-\;> <a-?> ^ \h * (begin|case|class|def|do|for|if|module|unless|until|while) <ret> s \A | \z <ret> ) <a-&> } try %{ execute-keys -draft <a-x> <a-k> ^ \h * (end) $ <ret> <a-\;> <a-?> ^ \h * (begin|case|class|def|for|if|module|unless|until|while) <ret> s \A | \z <ret> ) <a-&> }
} }
} }
@ -153,22 +153,23 @@ define-command -hidden ruby-indent-on-new-line %{
# filter previous line # filter previous line
try %{ execute-keys -draft k : ruby-trim-indent <ret> } try %{ execute-keys -draft k : ruby-trim-indent <ret> }
# indent after start structure # indent after start structure
try %{ execute-keys -draft k <a-x> <a-k> ^ \h * (begin|case|class|def|do|else|elsif|ensure|for|if|module|rescue|unless|until|when|while) \b <ret> j <a-gt> } try %{ execute-keys -draft k <a-x> <a-k> ^ \h * (begin|case|class|def|else|elsif|ensure|for|if|module|rescue|unless|until|when|while|.+\bdo$|.+\bdo\h\|.+(?=\|)) \b <ret> j <a-gt> }
} }
} }
define-command -hidden ruby-insert-on-new-line %[ define-command -hidden ruby-insert-on-new-line %[
evaluate-commands -no-hooks -draft -itersel %[ evaluate-commands -no-hooks -draft -itersel %[
# copy _#_ comment prefix and following white spaces # copy _#_ comment prefix and following white spaces
try %{ execute-keys -draft k <a-x> s '^\h*\K#\h*' <ret> y gh j P } try %{ execute-keys -draft k <a-x> s '^\h*\K#\h*' <ret> y j <a-x>\; P }
# wisely add end structure # wisely add end structure
evaluate-commands -save-regs x %[ evaluate-commands -save-regs x %[
try %{ execute-keys -draft k <a-x> s ^ \h + <ret> \" x y } catch %{ reg x '' } try %{ execute-keys -draft k <a-x> s ^ \h + <ret> \" x y } catch %{ reg x '' }
try %[ try %[
evaluate-commands -draft %[ evaluate-commands -draft %[
execute-keys -draft k<a-x> <a-k>^<c-r>x(begin|case|class|def|do|for|if|module|unless|until|while)<ret> # Check if previous line opens a block # Check if previous line opens a block
# Check that we do not already have an end for this indent level, or that we have another block opening at that indent level first execute-keys -draft k<a-x> <a-k>^<c-r>x(begin|case|class|def|for|if|module|unless|until|while|.+\bdo$|.+\bdo\h\|.+(?=\|))\b<ret>
execute-keys -draft Ge <a-K>\A(^\n|^<c-r>x(?!begin)(?!case)(?!class)(?!def)(?!do)(?!for)(?!if)(?!module)(?!unless)(?!until)(?!while)[^\n]*\n)+<c-r>xend$<ret> # Check that we do not already have an end for this indent level which is first set via `ruby-indent-on-new-line` hook
execute-keys -draft }i J <a-x> <a-K> ^<c-r>xend$<ret>
] ]
execute-keys -draft o<c-r>xend<esc> # insert a new line with containing end execute-keys -draft o<c-r>xend<esc> # insert a new line with containing end
] ]

View File

@ -0,0 +1 @@
c<ret><esc>jo<ret>e.map do |element|<ret><esc>

View File

@ -0,0 +1 @@
test do%( )

View File

@ -0,0 +1,7 @@
test do
end
e.map do |element|
end

View File

@ -0,0 +1,3 @@
source "%val{runtime}/colors/default.kak"
source "%val{runtime}/rc/filetype/ruby.kak"
set buffer filetype ruby