From 94b35a4071acbce3694cf1a2a05e54da1acb38fb Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Tue, 7 Sep 2021 14:46:50 -0300 Subject: [PATCH 1/6] lua.kak: remove some extra spaces --- rc/filetype/lua.kak | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rc/filetype/lua.kak b/rc/filetype/lua.kak index 090a94f48..40d362810 100644 --- a/rc/filetype/lua.kak +++ b/rc/filetype/lua.kak @@ -40,8 +40,8 @@ provide-module lua %[ add-highlighter shared/lua regions add-highlighter shared/lua/code default-region group -add-highlighter shared/lua/raw_string region -match-capture '\[(=*)\[' '\](=*)\]' fill string -add-highlighter shared/lua/raw_comment region -match-capture '--\[(=*)\[' '\](=*)\]' fill comment +add-highlighter shared/lua/raw_string region -match-capture '\[(=*)\[' '\](=*)\]' fill string +add-highlighter shared/lua/raw_comment region -match-capture '--\[(=*)\[' '\](=*)\]' fill comment add-highlighter shared/lua/double_string region '"' (? Date: Wed, 8 Sep 2021 19:17:17 -0300 Subject: [PATCH 2/6] lua.kak: use %[] instead of %{} We will want to use unbalanced "{" and "}" in some of the rules. To support that, let's use "%[]" instead. --- rc/filetype/lua.kak | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/rc/filetype/lua.kak b/rc/filetype/lua.kak index 40d362810..cb0775b5f 100644 --- a/rc/filetype/lua.kak +++ b/rc/filetype/lua.kak @@ -33,7 +33,7 @@ hook -group lua-highlight global WinSetOption filetype=lua %{ } -provide-module lua %[ +provide-module lua %§ # Highlighters # ‾‾‾‾‾‾‾‾‾‾‾‾ @@ -85,42 +85,42 @@ define-command lua-alternative-file -docstring 'Jump to the alternate file (impl printf %s\\n "edit $altfile" }} -define-command -hidden lua-trim-indent %{ +define-command -hidden lua-trim-indent %[ # remove trailing whitespaces - try %{ execute-keys -draft -itersel s \h+$ d } -} + try %[ execute-keys -draft -itersel s \h+$ d ] +] -define-command -hidden lua-indent-on-char %{ - evaluate-commands -no-hooks -draft -itersel %{ +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) } - } -} + 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) ] + ] +] -define-command -hidden lua-indent-on-new-line %{ - evaluate-commands -no-hooks -draft -itersel %{ +define-command -hidden lua-indent-on-new-line %[ + evaluate-commands -no-hooks -draft -itersel %[ # remove trailing white spaces from previous line - try %{ execute-keys -draft k : lua-trim-indent } + try %[ execute-keys -draft k : lua-trim-indent ] # preserve previous non-empty line indent - try %{ execute-keys -draft ^[^\n]+$s\A|.\z) } + 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 ^[^\n]*\w+[^\n]*$\b(else|elseif|for|function|if|while)\b\bend\b ] + ] +] define-command -hidden lua-insert-on-new-line %[ evaluate-commands -no-hooks -draft -itersel %[ # copy -- comment prefix and following white spaces - try %{ execute-keys -draft ks^\h*\K--\h* y gh j P } + 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]*(for|function|if|while)\bend\b J}iJ ^x(else|end|elseif)$ # Validate previous line and that it is not closed yet + 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 ] ] ] -] +§ From 8755fc679e265727ed5cb858c156dfdba51ae718 Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Fri, 10 Sep 2021 01:43:59 -0300 Subject: [PATCH 3/6] 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" --- rc/filetype/lua.kak | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/rc/filetype/lua.kak b/rc/filetype/lua.kak index cb0775b5f..d0227a7a1 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*[(]) \ + + ] ] ] From aa1a2803093348e4d18244af7c9785c32041abbe Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sat, 11 Sep 2021 00:51:12 -0300 Subject: [PATCH 4/6] 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 d0227a7a1..9660253db 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) + ] ] ] ] From c26fb65ed17ef44854030f2c0afffea357012dce Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sun, 26 Sep 2021 11:19:58 -0300 Subject: [PATCH 5/6] lua.kak: Add some test cases, and also fix a bug --- rc/filetype/lua.kak | 2 +- test/indent/lua/insert-end-only-when-needed/in | 14 +++++++++++++- .../indent/lua/insert-end-only-when-needed/out | 18 +++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/rc/filetype/lua.kak b/rc/filetype/lua.kak index 9660253db..1dd0178af 100644 --- a/rc/filetype/lua.kak +++ b/rc/filetype/lua.kak @@ -136,7 +136,7 @@ define-command -hidden lua-insert-on-new-line %[ ^\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 + 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 diff --git a/test/indent/lua/insert-end-only-when-needed/in b/test/indent/lua/insert-end-only-when-needed/in index 14415ac6c..9c880220f 100644 --- a/test/indent/lua/insert-end-only-when-needed/in +++ b/test/indent/lua/insert-end-only-when-needed/in @@ -3,7 +3,19 @@ end function fun2()%( ) - function fun3() if true then%( ) end + +function fun4() + if true then + return 10 + else%( ) +end + +function fun5() + if true then%( ) + return 10 +end + +local str = "while"%( ) diff --git a/test/indent/lua/insert-end-only-when-needed/out b/test/indent/lua/insert-end-only-when-needed/out index b16440480..f2b9fa25d 100644 --- a/test/indent/lua/insert-end-only-when-needed/out +++ b/test/indent/lua/insert-end-only-when-needed/out @@ -6,9 +6,25 @@ function fun2() foo() end - function fun3() if true then foo() end end + +function fun4() + if true then + return 10 + else + foo() + end +end + +function fun5() + if true then + foo() + return 10 +end + +local str = "while" +foo() From 25a0fe8f58ffba5132dd454fc85ebed21ba5a06d Mon Sep 17 00:00:00 2001 From: Hugo Musso Gualandi Date: Sun, 26 Sep 2021 11:54:02 -0300 Subject: [PATCH 6/6] lua.kak: Add test cases for unindent --- test/indent/lua/unindent-after-end/cmd | 1 + test/indent/lua/unindent-after-end/in | 7 +++++++ test/indent/lua/unindent-after-end/out | 7 +++++++ test/indent/lua/unindent-after-end/rc | 3 +++ 4 files changed, 18 insertions(+) create mode 100644 test/indent/lua/unindent-after-end/cmd create mode 100644 test/indent/lua/unindent-after-end/in create mode 100644 test/indent/lua/unindent-after-end/out create mode 100644 test/indent/lua/unindent-after-end/rc diff --git a/test/indent/lua/unindent-after-end/cmd b/test/indent/lua/unindent-after-end/cmd new file mode 100644 index 000000000..d9f644bd0 --- /dev/null +++ b/test/indent/lua/unindent-after-end/cmd @@ -0,0 +1 @@ +cend diff --git a/test/indent/lua/unindent-after-end/in b/test/indent/lua/unindent-after-end/in new file mode 100644 index 000000000..af777973e --- /dev/null +++ b/test/indent/lua/unindent-after-end/in @@ -0,0 +1,7 @@ +if A then + %( ) + +if A then + while B then + end + %( ) diff --git a/test/indent/lua/unindent-after-end/out b/test/indent/lua/unindent-after-end/out new file mode 100644 index 000000000..2446b3bac --- /dev/null +++ b/test/indent/lua/unindent-after-end/out @@ -0,0 +1,7 @@ +if A then +end + +if A then + while B then + end +end diff --git a/test/indent/lua/unindent-after-end/rc b/test/indent/lua/unindent-after-end/rc new file mode 100644 index 000000000..b5cf999de --- /dev/null +++ b/test/indent/lua/unindent-after-end/rc @@ -0,0 +1,3 @@ +source "%val{runtime}/colors/default.kak" +source "%val{runtime}/rc/filetype/lua.kak" +set buffer filetype lua