diff --git a/rc/base/lua.kak b/rc/base/lua.kak index 9bac6c5c..53c84de4 100644 --- a/rc/base/lua.kak +++ b/rc/base/lua.kak @@ -52,13 +52,6 @@ define-command lua-alternative-file -docstring 'Jump to the alternate file (impl printf %s\\n "edit $altfile" }} -define-command -hidden lua-filter-around-selections %{ - evaluate-commands -no-hooks -draft -itersel %{ - # remove trailing white spaces - try %{ execute-keys -draft s\h+$d } - } -} - define-command -hidden lua-indent-on-char %{ evaluate-commands -no-hooks -draft -itersel %{ # align middle and end structures to start and indent when necessary, elseif is already covered by else @@ -71,24 +64,25 @@ define-command -hidden lua-indent-on-new-line %{ evaluate-commands -no-hooks -draft -itersel %{ # preserve previous line indent try %{ execute-keys -draft K } - # filter previous line - try %{ execute-keys -draft k:lua-filter-around-selections } + # remove trailing white spaces from previous line + try %{ execute-keys -draft ks\h+$d } # indent after start structure try %{ execute-keys -draft k^\h*(else|elseif|for|function|if|while)\bj } } } -define-command -hidden lua-insert-on-new-line %{ - evaluate-commands -no-hooks -draft -itersel %{ +define-command -hidden lua-insert-on-new-line %[ + evaluate-commands -no-hooks -draft -itersel %[ # copy -- comment prefix and following white spaces try %{ execute-keys -draft ks^\h*\K--\h*yghjP } # wisely add end structure - evaluate-commands -save-regs x %{ - try %{ execute-keys -draft ks^\h+"xy } catch %{ reg x '' } - try %{ execute-keys -draft k^x(for|function|if|while)jiXK^x(for|function|if|while).*\nx(else|end|elseif[^\n]*)$jxypjaend } - } - } -} + evaluate-commands -save-regs x %[ + try %{ execute-keys -draft ks^\h+"xy } catch %{ reg x '' } # Save previous line indent in register x + try %[ execute-keys -draft k ^x(for|function|if|while) J}iJ ^x(else|end|elseif)$ # Validate previous line and that it is not closed yet + execute-keys -draft oxend ] # auto insert end + ] + ] +] # Initialization # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ @@ -97,8 +91,8 @@ hook -group lua-highlight global WinSetOption filetype=lua %{ add-highlighter wi hook global WinSetOption filetype=lua %{ hook window InsertChar .* -group lua-indent lua-indent-on-char - hook window InsertChar \n -group lua-insert lua-insert-on-new-line hook window InsertChar \n -group lua-indent lua-indent-on-new-line + hook window InsertChar \n -group lua-insert lua-insert-on-new-line alias window alt lua-alternative-file } diff --git a/test/indent/lua/insert-end-only-when-needed/cmd b/test/indent/lua/insert-end-only-when-needed/cmd new file mode 100644 index 00000000..ee78551e --- /dev/null +++ b/test/indent/lua/insert-end-only-when-needed/cmd @@ -0,0 +1 @@ +cfoo() diff --git a/test/indent/lua/insert-end-only-when-needed/in b/test/indent/lua/insert-end-only-when-needed/in new file mode 100644 index 00000000..14415ac6 --- /dev/null +++ b/test/indent/lua/insert-end-only-when-needed/in @@ -0,0 +1,9 @@ +function fun1()%( ) +end + +function fun2()%( ) + + +function fun3() + if true then%( ) +end diff --git a/test/indent/lua/insert-end-only-when-needed/out b/test/indent/lua/insert-end-only-when-needed/out new file mode 100644 index 00000000..b1644048 --- /dev/null +++ b/test/indent/lua/insert-end-only-when-needed/out @@ -0,0 +1,14 @@ +function fun1() + foo() +end + +function fun2() + foo() +end + + +function fun3() + if true then + foo() + end +end diff --git a/test/indent/lua/insert-end-only-when-needed/rc b/test/indent/lua/insert-end-only-when-needed/rc new file mode 100644 index 00000000..7550d15c --- /dev/null +++ b/test/indent/lua/insert-end-only-when-needed/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/base/lua.kak" +set buffer filetype lua diff --git a/test/regression/1525-lua-indent-error/cmd b/test/regression/1525-lua-indent-error/cmd new file mode 100644 index 00000000..ee78551e --- /dev/null +++ b/test/regression/1525-lua-indent-error/cmd @@ -0,0 +1 @@ +cfoo() diff --git a/test/regression/1525-lua-indent-error/in b/test/regression/1525-lua-indent-error/in new file mode 100644 index 00000000..a5bc61ac --- /dev/null +++ b/test/regression/1525-lua-indent-error/in @@ -0,0 +1,2 @@ +bugdemo = {} +function bugdemo.bug()%( ) diff --git a/test/regression/1525-lua-indent-error/out b/test/regression/1525-lua-indent-error/out new file mode 100644 index 00000000..671972fa --- /dev/null +++ b/test/regression/1525-lua-indent-error/out @@ -0,0 +1,4 @@ +bugdemo = {} +function bugdemo.bug() + foo() +end diff --git a/test/regression/1525-lua-indent-error/rc b/test/regression/1525-lua-indent-error/rc new file mode 100644 index 00000000..7550d15c --- /dev/null +++ b/test/regression/1525-lua-indent-error/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/base/lua.kak" +set buffer filetype lua