From 0f49e7375e1a414f2a144694c8b1f91354a0804e Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Sun, 6 Jun 2021 00:59:22 +0600 Subject: [PATCH 1/6] Add auto-inserting of "end" keyword to Elixir fixup! Add auto-inserting of "end" keyword to Elixir --- rc/filetype/elixir.kak | 23 ++++++++++++++++++----- test/indent/elixir/do-keyword/cmd | 1 + test/indent/elixir/do-keyword/in | 1 + test/indent/elixir/do-keyword/out | 10 ++++++++++ test/indent/elixir/do-keyword/rc | 3 +++ 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 test/indent/elixir/do-keyword/cmd create mode 100644 test/indent/elixir/do-keyword/in create mode 100644 test/indent/elixir/do-keyword/out create mode 100644 test/indent/elixir/do-keyword/rc diff --git a/rc/filetype/elixir.kak b/rc/filetype/elixir.kak index f06ffb1c91..5b2159423b 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 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 From 8867e40929d81791d508bb2c767ff185dace3e34 Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Sun, 6 Jun 2021 01:05:45 +0600 Subject: [PATCH 2/6] Fix elixir copying comment # sign Elixir uses # for comments, not --. Implementation is copied from Nim. --- rc/filetype/elixir.kak | 4 ++-- test/indent/elixir/insert-comment-hash/cmd | 1 + test/indent/elixir/insert-comment-hash/in | 1 + test/indent/elixir/insert-comment-hash/out | 2 ++ test/indent/elixir/insert-comment-hash/rc | 3 +++ 5 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 test/indent/elixir/insert-comment-hash/cmd create mode 100644 test/indent/elixir/insert-comment-hash/in create mode 100644 test/indent/elixir/insert-comment-hash/out create mode 100644 test/indent/elixir/insert-comment-hash/rc diff --git a/rc/filetype/elixir.kak b/rc/filetype/elixir.kak index 5b2159423b..2485d7332f 100644 --- a/rc/filetype/elixir.kak +++ b/rc/filetype/elixir.kak @@ -94,8 +94,8 @@ define-command -hidden elixir-trim-indent %{ 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 } + # copy '#' comment prefix and following white spaces + try %{ exec -draft k s ^\h*#\h* y jgh P } # wisely add end structure evaluate-commands -save-regs x %[ try %{ execute-keys -draft k s ^ \h + \" x y } catch %{ reg x '' } diff --git a/test/indent/elixir/insert-comment-hash/cmd b/test/indent/elixir/insert-comment-hash/cmd new file mode 100644 index 0000000000..e3036a4075 --- /dev/null +++ b/test/indent/elixir/insert-comment-hash/cmd @@ -0,0 +1 @@ +c 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..bde83b7ae5 --- /dev/null +++ b/test/indent/elixir/insert-comment-hash/out @@ -0,0 +1,2 @@ +# Comment +# comment2 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 From 785f7fe9ed34dc1c04987533857b3ec8533c8c4c Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Sun, 6 Jun 2021 01:34:09 +0600 Subject: [PATCH 3/6] Add more tests for "do" auto-insert --- .../elixir/following-blocks-should-not-prevent-end/cmd | 1 + .../elixir/following-blocks-should-not-prevent-end/in | 4 ++++ .../elixir/following-blocks-should-not-prevent-end/out | 6 ++++++ .../elixir/following-blocks-should-not-prevent-end/rc | 3 +++ .../elixir/function-definition-shouldnt-duplicate/cmd | 1 + .../indent/elixir/function-definition-shouldnt-duplicate/in | 3 +++ .../elixir/function-definition-shouldnt-duplicate/out | 5 +++++ .../indent/elixir/function-definition-shouldnt-duplicate/rc | 3 +++ 8 files changed, 26 insertions(+) create mode 100644 test/indent/elixir/following-blocks-should-not-prevent-end/cmd create mode 100644 test/indent/elixir/following-blocks-should-not-prevent-end/in create mode 100644 test/indent/elixir/following-blocks-should-not-prevent-end/out create mode 100644 test/indent/elixir/following-blocks-should-not-prevent-end/rc create mode 100644 test/indent/elixir/function-definition-shouldnt-duplicate/cmd create mode 100644 test/indent/elixir/function-definition-shouldnt-duplicate/in create mode 100644 test/indent/elixir/function-definition-shouldnt-duplicate/out create mode 100644 test/indent/elixir/function-definition-shouldnt-duplicate/rc 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 From 167cffb3dad62755e1ecd1df0430cab800e41bb9 Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Sun, 6 Jun 2021 14:08:40 +0600 Subject: [PATCH 4/6] Better comment inserting tests and fix implementation --- rc/filetype/elixir.kak | 2 +- test/indent/elixir/insert-comment-hash/cmd | 2 +- test/indent/elixir/insert-comment-hash/out | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rc/filetype/elixir.kak b/rc/filetype/elixir.kak index 2485d7332f..692de2d29e 100644 --- a/rc/filetype/elixir.kak +++ b/rc/filetype/elixir.kak @@ -95,7 +95,7 @@ define-command -hidden elixir-trim-indent %{ define-command -hidden elixir-insert-on-new-line %[ evaluate-commands -no-hooks -draft -itersel %[ # copy '#' comment prefix and following white spaces - try %{ exec -draft k s ^\h*#\h* y jgh P } + try %{ exec -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 '' } diff --git a/test/indent/elixir/insert-comment-hash/cmd b/test/indent/elixir/insert-comment-hash/cmd index e3036a4075..bd828f3573 100644 --- a/test/indent/elixir/insert-comment-hash/cmd +++ b/test/indent/elixir/insert-comment-hash/cmd @@ -1 +1 @@ -c +cjo# comment3comment4jo# indentedindented2 diff --git a/test/indent/elixir/insert-comment-hash/out b/test/indent/elixir/insert-comment-hash/out index bde83b7ae5..1dba5ac915 100644 --- a/test/indent/elixir/insert-comment-hash/out +++ b/test/indent/elixir/insert-comment-hash/out @@ -1,2 +1,8 @@ # Comment # comment2 + +# comment3 +# comment4 + +# indented +# indented2 From 8574c3827aefdc29785aa7f40bfc639b65e7bf3b Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Sun, 6 Jun 2021 14:25:06 +0600 Subject: [PATCH 5/6] Event better tests for comments --- test/indent/elixir/insert-comment-hash/cmd | 2 +- test/indent/elixir/insert-comment-hash/out | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/test/indent/elixir/insert-comment-hash/cmd b/test/indent/elixir/insert-comment-hash/cmd index bd828f3573..c1f758279d 100644 --- a/test/indent/elixir/insert-comment-hash/cmd +++ b/test/indent/elixir/insert-comment-hash/cmd @@ -1 +1 @@ -cjo# comment3comment4jo# indentedindented2 +cjo# comment3comment4jo# indentedindented2jodef f() do# commentcomment2jjodef f() do# commenthhhi diff --git a/test/indent/elixir/insert-comment-hash/out b/test/indent/elixir/insert-comment-hash/out index 1dba5ac915..d4587bbe73 100644 --- a/test/indent/elixir/insert-comment-hash/out +++ b/test/indent/elixir/insert-comment-hash/out @@ -6,3 +6,13 @@ # indented # indented2 + +def f() do + # comment + # comment2 +end + +def f() do + # comm + # ent +end From 2f22fef5bdfecbaf80997e2701a2df8c663af771 Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Sun, 6 Jun 2021 14:29:51 +0600 Subject: [PATCH 6/6] Rename exec->execute-keys for consistency --- rc/filetype/elixir.kak | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rc/filetype/elixir.kak b/rc/filetype/elixir.kak index 692de2d29e..e62ca429b3 100644 --- a/rc/filetype/elixir.kak +++ b/rc/filetype/elixir.kak @@ -95,7 +95,7 @@ define-command -hidden elixir-trim-indent %{ define-command -hidden elixir-insert-on-new-line %[ evaluate-commands -no-hooks -draft -itersel %[ # copy '#' comment prefix and following white spaces - try %{ exec -draft k s ^\h*\K#\h* y jgi P } + 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 '' }