Add auto-inserting of "end" keyword to Elixir

fixup! Add auto-inserting of "end" keyword to Elixir
This commit is contained in:
Dmitry Matveyev 2021-06-06 00:59:22 +06:00
parent d758bbf09b
commit 0f49e7375e
5 changed files with 33 additions and 5 deletions

View File

@ -19,8 +19,8 @@ hook global WinSetOption filetype=elixir %{
require-module elixir
hook window ModeChange pop:insert:.* -group elixir-trim-indent elixir-trim-indent
hook window InsertChar \n -group elixir-insert elixir-insert-on-new-line
hook window InsertChar \n -group elixir-indent elixir-indent-on-new-line
hook window InsertChar \n -group elixir-insert elixir-insert-on-new-line
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window elixir-.+ }
}
@ -92,12 +92,25 @@ define-command -hidden elixir-trim-indent %{
try %{ execute-keys -draft -itersel <a-x> s \h+$ <ret> d }
}
define-command -hidden elixir-insert-on-new-line %{
evaluate-commands -draft -itersel %{
define-command -hidden elixir-insert-on-new-line %[
evaluate-commands -no-hooks -draft -itersel %[
# copy -- comments prefix and following white spaces
try %{ execute-keys -draft k <a-x> s ^\h*\K--\h* <ret> y gh j P }
}
}
# wisely add end structure
evaluate-commands -save-regs x %[
try %{ execute-keys -draft k <a-x> s ^ \h + <ret> \" x y } catch %{ reg x '' }
try %[
evaluate-commands -draft %[
# Check if previous line opens a block
execute-keys -draft k<a-x> <a-k>^<c-r>x(.+\bdo$)<ret>
# Check that we do not already have an end for this indent level which is first set via `elixir-indent-on-new-line` hook
execute-keys -draft }i J <a-x> <a-K> ^<c-r>x(end|else)[^0-9A-Za-z_!?]<ret>
]
execute-keys -draft o<c-r>xend<esc> # insert a new line with containing end
]
]
]
]
define-command -hidden elixir-indent-on-new-line %{
evaluate-commands -draft -itersel %{

View File

@ -0,0 +1 @@
c<ret>f()<esc>jo<ret>def f1() do<ret>1<esc>jo<ret>def f2(), do: 2<ret>

View File

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

View File

@ -0,0 +1,10 @@
test do
f()
end
def f1() do
1
end
def f2(), do: 2

View File

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