diff --git a/rc/filetype/lua.kak b/rc/filetype/lua.kak index cb0775b5..d0227a7a 100644 --- a/rc/filetype/lua.kak +++ b/rc/filetype/lua.kak @@ -92,9 +92,12 @@ define-command -hidden lua-trim-indent %[ 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 - try %[ execute-keys -draft ^\h*(else)$^\h*(if)s\A|.\z) ] - try %[ execute-keys -draft ^\h*(end)$^\h*(for|function|if|while)s\A|.\z) ] + # unindent middle and end structures + try %[ execute-keys -draft \ + ^\h*(\b(end|else|elseif|until)\b|[)}])$ \ + :lua-indent-on-new-line \ + + ] ] ] @@ -103,9 +106,18 @@ define-command -hidden lua-indent-on-new-line %[ # remove trailing white spaces from previous line try %[ execute-keys -draft k : lua-trim-indent ] # preserve previous non-empty line indent - try %[ execute-keys -draft ^[^\n]+$s\A|.\z) ] - # indent after start structure - try %[ execute-keys -draft ^[^\n]*\w+[^\n]*$\b(else|elseif|for|function|if|while)\b\bend\b ] + try %[ execute-keys -draft gh^[^\n]+$s\A|.\z) ] + # add one indentation level if the previous line is not a comment and: + # - starts with a block keyword that is not closed on the same line, + # - or contains an unclosed function expression, + # - or ends with an enclosed '(' or '{' + try %[ execute-keys -draft \ + K \ + \A\h*-- \ + \A[^\n]*\b(end|until)\b \ + \A(\h*\b(do|else|elseif|for|function|if|repeat|while)\b|[^\n]*[({]$|[^\n]*\bfunction\b\h*[(]) \ + + ] ] ]