From aa1a2803093348e4d18244af7c9785c32041abbe Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sat, 11 Sep 2021 00:51:12 -0300 Subject: [PATCH] lua.kak: more accurate "end" insertion - Also insert "end" after "do", "else" and "elseif" - Do not insert "end" after strings or comments containing keywords - Only insert "end" if the block is empty An example of the last item is if we want to add a new line to the start of an unclosed block that already contains statements. @ is the cursor. -- before if a then@ x = 1 y = 2 -- after if a then @ end x = 1 y = 2 In this case, inserting the "end" before the statements is probably not what the programmer wants. It might make more sense to insert the "end" after the statements, but that is potentially confusing due to spooky action at a distance. I think the least confusing thing to do in this situation is to not insert the "end". --- rc/filetype/lua.kak | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) 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) + ] ] ] ]