From 78a0b5e4c70878146ddbfd2727b4d3ae0d0209a4 Mon Sep 17 00:00:00 2001 From: cipharius Date: Tue, 23 Mar 2021 10:13:10 +0200 Subject: [PATCH] Improves Lua auto indent and end insertion Current solution makes it difficult to use common Lua practices of having one-liner if statements and using anonymous functions. New solution prevents auto-indentation and end insertion, if the previous line contains an "end" keyword. It does not attempt to match each structure with corresponding end, since using multiple end keywords in single line is a very rare occurance in Lua. --- rc/filetype/lua.kak | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rc/filetype/lua.kak b/rc/filetype/lua.kak index 82dc51cc..6c3f9f52 100644 --- a/rc/filetype/lua.kak +++ b/rc/filetype/lua.kak @@ -105,7 +105,7 @@ define-command -hidden lua-indent-on-new-line %{ # 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]*$^\h*(else|elseif|for|function|if|while)\b } + try %{ execute-keys -draft ^[^\n]*\w+[^\n]*$\b(else|elseif|for|function|if|while)\b\bend\b } } } @@ -116,8 +116,9 @@ define-command -hidden lua-insert-on-new-line %[ # 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(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 + try %[ execute-keys -draft k ^x\b[^\n]*(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 ] ] ]