lua.kak: Rework 'end' auto insertion logic

Fixes #1525
This commit is contained in:
Maxime Coste 2018-03-04 10:35:38 +11:00
parent a2fc89a6ec
commit 65258934d3
9 changed files with 49 additions and 18 deletions

View File

@ -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 <a-x>s\h+$<ret>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 <space>K<a-&> }
# filter previous line
try %{ execute-keys -draft k:lua-filter-around-selections<ret> }
# remove trailing white spaces from previous line
try %{ execute-keys -draft k<a-x>s\h+$<ret>d }
# indent after start structure
try %{ execute-keys -draft k<a-x><a-k>^\h*(else|elseif|for|function|if|while)\b<ret>j<a-gt> }
}
}
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 k<a-x>s^\h*\K--\h*<ret>yghjP }
# wisely add end structure
evaluate-commands -save-regs x %{
try %{ execute-keys -draft k<a-x>s^\h+<ret>"xy } catch %{ reg x '' }
try %{ execute-keys -draft k<a-x><a-k>^<c-r>x(for|function|if|while)<ret>j<a-a>iX<a-\;>K<a-K>^<c-r>x(for|function|if|while).*\n<c-r>x(else|end|elseif[^\n]*)$<ret>jxypjaend<esc><a-lt> }
}
}
}
evaluate-commands -save-regs x %[
try %{ execute-keys -draft k<a-x>s^\h+<ret>"xy } catch %{ reg x '' } # Save previous line indent in register x
try %[ execute-keys -draft k<a-x> <a-k>^<c-r>x(for|function|if|while)<ret> J}iJ<a-x> <a-K>^<c-r>x(else|end|elseif)$<ret> # Validate previous line and that it is not closed yet
execute-keys -draft o<c-r>xend<esc> ] # 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
}

View File

@ -0,0 +1 @@
c<ret>foo()<esc>

View File

@ -0,0 +1,9 @@
function fun1()%( )
end
function fun2()%( )
function fun3()
if true then%( )
end

View File

@ -0,0 +1,14 @@
function fun1()
foo()
end
function fun2()
foo()
end
function fun3()
if true then
foo()
end
end

View File

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

View File

@ -0,0 +1 @@
c<ret>foo()<esc>

View File

@ -0,0 +1,2 @@
bugdemo = {}
function bugdemo.bug()%( )

View File

@ -0,0 +1,4 @@
bugdemo = {}
function bugdemo.bug()
foo()
end

View File

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