diff --git a/rc/filetype/elixir.kak b/rc/filetype/elixir.kak index f06ffb1c91..e62ca429b3 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 %{ - # copy -- comments prefix and following white spaces - try %{ execute-keys -draft k s ^\h*\K--\h* y gh j P } - } -} +define-command -hidden elixir-insert-on-new-line %[ + evaluate-commands -no-hooks -draft -itersel %[ + # copy '#' comment prefix and following white spaces + try %{ execute-keys -draft k s ^\h*\K#\h* y jgi 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 0000000000..127363acb3 --- /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 0000000000..5e903f7fbb --- /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 0000000000..ad2373b155 --- /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 0000000000..5cc4387c1c --- /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 diff --git a/test/indent/elixir/following-blocks-should-not-prevent-end/cmd b/test/indent/elixir/following-blocks-should-not-prevent-end/cmd new file mode 100644 index 0000000000..7600303054 --- /dev/null +++ b/test/indent/elixir/following-blocks-should-not-prevent-end/cmd @@ -0,0 +1 @@ +c diff --git a/test/indent/elixir/following-blocks-should-not-prevent-end/in b/test/indent/elixir/following-blocks-should-not-prevent-end/in new file mode 100644 index 0000000000..5d7be301c0 --- /dev/null +++ b/test/indent/elixir/following-blocks-should-not-prevent-end/in @@ -0,0 +1,4 @@ +def foo() do%( ) + +def bar() do +end diff --git a/test/indent/elixir/following-blocks-should-not-prevent-end/out b/test/indent/elixir/following-blocks-should-not-prevent-end/out new file mode 100644 index 0000000000..661652a204 --- /dev/null +++ b/test/indent/elixir/following-blocks-should-not-prevent-end/out @@ -0,0 +1,6 @@ +def foo() do + +end + +def bar() do +end diff --git a/test/indent/elixir/following-blocks-should-not-prevent-end/rc b/test/indent/elixir/following-blocks-should-not-prevent-end/rc new file mode 100644 index 0000000000..5cc4387c1c --- /dev/null +++ b/test/indent/elixir/following-blocks-should-not-prevent-end/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/elixir.kak" +set buffer filetype elixir diff --git a/test/indent/elixir/function-definition-shouldnt-duplicate/cmd b/test/indent/elixir/function-definition-shouldnt-duplicate/cmd new file mode 100644 index 0000000000..7600303054 --- /dev/null +++ b/test/indent/elixir/function-definition-shouldnt-duplicate/cmd @@ -0,0 +1 @@ +c diff --git a/test/indent/elixir/function-definition-shouldnt-duplicate/in b/test/indent/elixir/function-definition-shouldnt-duplicate/in new file mode 100644 index 0000000000..12dbbdd4de --- /dev/null +++ b/test/indent/elixir/function-definition-shouldnt-duplicate/in @@ -0,0 +1,3 @@ +defmodule Module do + def test do%( ) +end diff --git a/test/indent/elixir/function-definition-shouldnt-duplicate/out b/test/indent/elixir/function-definition-shouldnt-duplicate/out new file mode 100644 index 0000000000..d0a93b4999 --- /dev/null +++ b/test/indent/elixir/function-definition-shouldnt-duplicate/out @@ -0,0 +1,5 @@ +defmodule Module do + def test do + + end +end diff --git a/test/indent/elixir/function-definition-shouldnt-duplicate/rc b/test/indent/elixir/function-definition-shouldnt-duplicate/rc new file mode 100644 index 0000000000..5cc4387c1c --- /dev/null +++ b/test/indent/elixir/function-definition-shouldnt-duplicate/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/elixir.kak" +set buffer filetype elixir diff --git a/test/indent/elixir/insert-comment-hash/cmd b/test/indent/elixir/insert-comment-hash/cmd new file mode 100644 index 0000000000..c1f758279d --- /dev/null +++ b/test/indent/elixir/insert-comment-hash/cmd @@ -0,0 +1 @@ +cjo# comment3comment4jo# indentedindented2jodef f() do# commentcomment2jjodef f() do# commenthhhi diff --git a/test/indent/elixir/insert-comment-hash/in b/test/indent/elixir/insert-comment-hash/in new file mode 100644 index 0000000000..9072f4dbb7 --- /dev/null +++ b/test/indent/elixir/insert-comment-hash/in @@ -0,0 +1 @@ +# Comment %( )comment2 diff --git a/test/indent/elixir/insert-comment-hash/out b/test/indent/elixir/insert-comment-hash/out new file mode 100644 index 0000000000..d4587bbe73 --- /dev/null +++ b/test/indent/elixir/insert-comment-hash/out @@ -0,0 +1,18 @@ +# Comment +# comment2 + +# comment3 +# comment4 + +# indented +# indented2 + +def f() do + # comment + # comment2 +end + +def f() do + # comm + # ent +end diff --git a/test/indent/elixir/insert-comment-hash/rc b/test/indent/elixir/insert-comment-hash/rc new file mode 100644 index 0000000000..5cc4387c1c --- /dev/null +++ b/test/indent/elixir/insert-comment-hash/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/elixir.kak" +set buffer filetype elixir