diff --git a/rc/filetype/lua.kak b/rc/filetype/lua.kak index d0227a7a..9660253d 100644 --- a/rc/filetype/lua.kak +++ b/rc/filetype/lua.kak @@ -127,10 +127,21 @@ define-command -hidden lua-insert-on-new-line %[ try %[ execute-keys -draft ks^\h*\K--\h* y gh j P ] # wisely add end structure 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\b[^\n]*(do|for|function|if|while)\bend\b J}iJ ^x(else|end|elseif)$ # Validate previous line and that it is not closed yet - execute-keys -draft oxend # auto insert end - execute-keys -draft k\(function\bjjA) ] # auto insert ) for anonymous function + # save previous line indent in register x + try %[ execute-keys -draft ks^\h+"xy ] catch %[ reg x '' ] + try %[ + # check that starts with a block keyword that is not closed on the same line + execute-keys -draft \ + k \ + ^\h*\b(else|elseif|do|for|function|if|while)\b|[^\n]\bfunction\b\h*[(] \ + \bend\b + # check that the block is empty and is not closed on a different line + execute-keys -draft i ^[^\n]*\n[^\n]*\n j ^x\b(else|elseif|end)\b + # auto insert end + execute-keys -draft oxend + # auto insert ) for anonymous function + execute-keys -draft k\([^)\n]*function\bjjA) + ] ] ] ]