diff --git a/rc/filetype/elixir.kak b/rc/filetype/elixir.kak index f06ffb1c..5b215942 100644 --- a/rc/filetype/elixir.kak +++ b/rc/filetype/elixir.kak @@ -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 s \h+$ 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 s ^\h*\K--\h* y gh j P } - } -} + # wisely add end structure + evaluate-commands -save-regs x %[ + try %{ execute-keys -draft k s ^ \h + \" x y } catch %{ reg x '' } + try %[ + evaluate-commands -draft %[ + # Check if previous line opens a block + execute-keys -draft k ^x(.+\bdo$) + # 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 ^x(end|else)[^0-9A-Za-z_!?] + ] + execute-keys -draft oxend # insert a new line with containing end + ] + ] + ] +] define-command -hidden elixir-indent-on-new-line %{ evaluate-commands -draft -itersel %{ diff --git a/test/indent/elixir/do-keyword/cmd b/test/indent/elixir/do-keyword/cmd new file mode 100644 index 00000000..127363ac --- /dev/null +++ b/test/indent/elixir/do-keyword/cmd @@ -0,0 +1 @@ +cf()jodef f1() do1jodef f2(), do: 2 diff --git a/test/indent/elixir/do-keyword/in b/test/indent/elixir/do-keyword/in new file mode 100644 index 00000000..5e903f7f --- /dev/null +++ b/test/indent/elixir/do-keyword/in @@ -0,0 +1 @@ +test do%( ) diff --git a/test/indent/elixir/do-keyword/out b/test/indent/elixir/do-keyword/out new file mode 100644 index 00000000..ad2373b1 --- /dev/null +++ b/test/indent/elixir/do-keyword/out @@ -0,0 +1,10 @@ +test do + f() +end + +def f1() do + 1 +end + +def f2(), do: 2 + diff --git a/test/indent/elixir/do-keyword/rc b/test/indent/elixir/do-keyword/rc new file mode 100644 index 00000000..5cc4387c --- /dev/null +++ b/test/indent/elixir/do-keyword/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/elixir.kak" +set buffer filetype elixir