From f2c11c01231cdbcd428b60d67af950a039a7fe03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Gonz=C3=A1lez?= Date: Wed, 16 Mar 2022 21:54:05 -0500 Subject: [PATCH] Improved support for commenting, mainly pulled from c-family.kak --- rc/filetype/javascript.kak | 113 +++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 11 deletions(-) diff --git a/rc/filetype/javascript.kak b/rc/filetype/javascript.kak index 089061ab..5d9059b1 100644 --- a/rc/filetype/javascript.kak +++ b/rc/filetype/javascript.kak @@ -43,7 +43,7 @@ provide-module javascript %ยง define-command -hidden javascript-trim-indent %{ # remove trailing white spaces - try %{ execute-keys -draft -itersel s \h+$ d } + try %{ execute-keys -draft 1s^(\h+)$ d } } define-command -hidden javascript-indent-on-char %< @@ -55,21 +55,112 @@ define-command -hidden javascript-indent-on-char %< define-command -hidden javascript-insert-on-new-line %< evaluate-commands -draft -itersel %< - # copy // comments prefix and following white spaces - try %{ execute-keys -draft k s ^\h*\K/{2,}\h* y gh j P } + execute-keys + # NOT working, copies single line comment "//" + # try %[ + # evaluate-commands -draft -save-regs '/"' %[ + # # copy the commenting prefix + # execute-keys -save-regs '' k 1s^\h*(//+\h*) y + # try %[ + # # if the previous comment isn't empty, create a new one + # execute-keys ^\h*//+\h*$ js^\h*P + # ] catch %[ + # # if there is no text in the previous comment, remove it completely + # execute-keys d + # ] + # ] + # ] + try %[ + # if the previous line isn't within a comment scope, break + execute-keys -draft k ^(\h*/\*|\h+\*(?!/)) + + # find comment opening, validate it was not closed, and check its using star prefixes + execute-keys -draft /\* \*/ \A\h*/\*([^\n]*\n\h*\*)*[^\n]*\n\h*.\z + + try %[ + # if the previous line is opening the comment, insert star preceeded by space + execute-keys -draft k^\h*/\* + execute-keys -draft i* + ] catch %[ + try %[ + # if the next line is a comment line insert a star + execute-keys -draft j^\h+\* + execute-keys -draft i* + ] catch %[ + try %[ + # if the previous line is an empty comment line, close the comment scope + execute-keys -draft k^\h+\*\h+$ 1s\*(\h*)c/ + ] catch %[ + # if the previous line is a non-empty comment line, add a star + execute-keys -draft i* + ] + ] + ] + + # trim trailing whitespace on the previous line + try %[ execute-keys -draft s\h+$ d ] + # align the new star with the previous one + execute-keys K1s^[^*]*(\*)& + ] + # # copy // comments prefix and following white spaces + # try %{ execute-keys -draft k s ^\h*\K/{2,}\h* y gh j P } > > define-command -hidden javascript-indent-on-new-line %< evaluate-commands -draft -itersel %< - # preserve previous line indent - try %{ execute-keys -draft K } - # filter previous line - try %{ execute-keys -draft k : javascript-trim-indent } - # indent after lines beginning / ending with opener token - try %_ execute-keys -draft k s [[({] [\])}] j _ - # deindent closing token(s) when after cursor - try %_ execute-keys -draft ^\h*[})\]] gh / [})\]] m 1 _ + execute-keys + try %< + # if previous line is part of a comment, do nothing + execute-keys -draft /\* ^\h*[^/*\h] + > catch %< + # else if previous line closed a paren (possibly followed by words and a comment), + # copy indent of the opening paren line + execute-keys -draft k 1s(\))(\h+\w+)*\h*(\;\h*)?(?://[^\n]+)?\n\z mJ 1 + > catch %< + # else indent new lines with the same level as the previous one + execute-keys -draft K + > + # remove previous empty lines resulting from the automatic indent + try %< execute-keys -draft k ^\h+$ Hd > + # indent after an opening brace or parenthesis at end of line + try %< execute-keys -draft k [{(]\h*$ j > + # indent after a label (works for case statements) + try %< execute-keys -draft k s[a-zA-Z0-9_-]+:\h*$ j > + # indent after a statement not followed by an opening brace + try %< execute-keys -draft k s\)\h*(?://[^\n]+)?\n\z \ + mB \A\b(if|for|while)\b j > + try %< execute-keys -draft k s \belse\b\h*(?://[^\n]+)?\n\z \ + j > + # deindent after a single line statement end + try %< execute-keys -draft K \;\h*(//[^\n]+)?$ \ + K s\)(\h+\w+)*\h*(//[^\n]+)?\n([^\n]*\n){2}\z \ + MB \A\b(if|for|while)\b 1 > + try %< execute-keys -draft K \;\h*(//[^\n]+)?$ \ + K s \belse\b\h*(?://[^\n]+)?\n([^\n]*\n){2}\z \ + 1 > + # deindent closing brace(s) when after cursor + try %< execute-keys -draft ^\h*[})] gh / [})] m 1 > + # align to the opening parenthesis or opening brace (whichever is first) + # on a previous line if its followed by text on the same line + try %< evaluate-commands -draft %< + # Go to opening parenthesis and opening brace, then select the most nested one + try %< execute-keys [c [({],[)}] > + # Validate selection and get first and last char + execute-keys \A[{(](\h*\S+)+\n "(([^"]*"){2})* '(([^']*'){2})* L + # Remove possibly incorrect indent from new line which was copied from previous line + try %< execute-keys -draft s\h+ d > + # Now indent and align that new line with the opening parenthesis/brace + execute-keys 1 & + > > + # # preserve previous line indent + # try %{ execute-keys -draft K } + # # filter previous line + # try %{ execute-keys -draft k : javascript-trim-indent } + # # indent after lines beginning / ending with opener token + # try %_ execute-keys -draft k s [[({] [\])}] j _ + # # deindent closing token(s) when after cursor + # try %_ execute-keys -draft ^\h*[})\]] gh / [})\]] m 1 _ > >