diff options
| author | Hugo Musso Gualandi <hugo_musso_gualandi@hotmail.com> | 2021-09-10 01:43:59 -0300 |
|---|---|---|
| committer | Hugo Musso Gualandi <hugo_musso_gualandi@hotmail.com> | 2021-09-11 01:27:41 -0300 |
| commit | 8755fc679e265727ed5cb858c156dfdba51ae718 (patch) | |
| tree | ebcac9b2b1c44d1c3cba78d183c8359183bf46f1 | |
| parent | ffc69544db0c883ff1992d7f2825233e6bc7e472 (diff) | |
lua.kak: improve the indentation logic
This commit makes several improvements to the Lua indentation logic.
- Don't indent if the keyword is inside a string or comment
- Indent inside "do end"
- Indent inside "repeat until"
- Indent after a line ending with "{" or "("
- More accurate un-indentation for the "end" keyword
For the last point, previously we tried to match the indentation of the
starting keyword of the block. However, sometimes this guessed wrong
and produced the wrong indentation, as the following example shows. The
new logic is to indent the "end" by one less level than the contents of
the block.
while true do
if false then
end
end -- This was incorrectly matched with the "if"
| -rw-r--r-- | rc/filetype/lua.kak | 24 |
1 files changed, 18 insertions, 6 deletions
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 <a-x><a-k>^\h*(else)$<ret><a-semicolon><a-?>^\h*(if)<ret>s\A|.\z<ret>)<a-&> ] - try %[ execute-keys -draft <a-x><a-k>^\h*(end)$<ret><a-semicolon><a-?>^\h*(for|function|if|while)<ret>s\A|.\z<ret>)<a-&> ] + # unindent middle and end structures + try %[ execute-keys -draft \ + <a-h><a-k>^\h*(\b(end|else|elseif|until)\b|[)}])$<ret> \ + :lua-indent-on-new-line<ret> \ + <a-lt> + ] ] ] @@ -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 <ret> ] # preserve previous non-empty line indent - try %[ execute-keys -draft <space><a-?>^[^\n]+$<ret>s\A|.\z<ret>)<a-&> ] - # indent after start structure - try %[ execute-keys -draft <a-?>^[^\n]*\w+[^\n]*$<ret><a-k>\b(else|elseif|for|function|if|while)\b<ret><a-K>\bend\b<ret><a-:><semicolon><a-gt> ] + try %[ execute-keys -draft <space>gh<a-?>^[^\n]+$<ret>s\A|.\z<ret>)<a-&> ] + # 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 \ + <space> K<a-x> \ + <a-K>\A\h*--<ret> \ + <a-K>\A[^\n]*\b(end|until)\b<ret> \ + <a-k>\A(\h*\b(do|else|elseif|for|function|if|repeat|while)\b|[^\n]*[({]$|[^\n]*\bfunction\b\h*[(])<ret> \ + <a-:><semicolon><a-gt> + ] ] ] |
