diff --git a/rc/filetype/lua.kak b/rc/filetype/lua.kak index 090a94f48..1dd0178af 100644 --- a/rc/filetype/lua.kak +++ b/rc/filetype/lua.kak @@ -33,15 +33,15 @@ hook -group lua-highlight global WinSetOption filetype=lua %{ } -provide-module lua %[ +provide-module lua %§ # Highlighters # ‾‾‾‾‾‾‾‾‾‾‾‾ 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 '"' (? s \h+$ d } -} + try %[ execute-keys -draft -itersel s \h+$ d ] +] -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) } - } -} - -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 } - # 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 } - } -} - -define-command -hidden lua-insert-on-new-line %[ +define-command -hidden lua-indent-on-char %[ 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 } - # 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 - execute-keys -draft oxend # auto insert end - execute-keys -draft k\(function\bjjA) ] # auto insert ) for anonymous function + # unindent middle and end structures + try %[ execute-keys -draft \ + ^\h*(\b(end|else|elseif|until)\b|[)}])$ \ + :lua-indent-on-new-line \ + ] ] ] +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 ] + # preserve previous non-empty line indent + 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*[(]) \ + + ] + ] ] + +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 ] + # wisely add end structure + evaluate-commands -save-regs x %[ + # 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) + ] + ] + ] +] + +§ 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() 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