Improved support for commenting, mainly pulled from c-family.kak

This commit is contained in:
Andrés González 2022-03-16 21:54:05 -05:00
parent ca68f73a36
commit f2c11c0123

View File

@ -43,7 +43,7 @@ provide-module javascript %§
define-command -hidden javascript-trim-indent %{ define-command -hidden javascript-trim-indent %{
# remove trailing white spaces # remove trailing white spaces
try %{ execute-keys -draft -itersel <a-x> s \h+$ <ret> d } try %{ execute-keys -draft <a-x> 1s^(\h+)$<ret> d }
} }
define-command -hidden javascript-indent-on-char %< 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 %< define-command -hidden javascript-insert-on-new-line %<
evaluate-commands -draft -itersel %< evaluate-commands -draft -itersel %<
# copy // comments prefix and following white spaces execute-keys <semicolon>
try %{ execute-keys -draft k <a-x> s ^\h*\K/{2,}\h* <ret> y gh j P } # NOT working, copies single line comment "//"
# try %[
# evaluate-commands -draft -save-regs '/"' %[
# # copy the commenting prefix
# execute-keys -save-regs '' k <a-x>1s^\h*(//+\h*)<ret> y
# try %[
# # if the previous comment isn't empty, create a new one
# execute-keys <a-x><a-K>^\h*//+\h*$<ret> j<a-x>s^\h*<ret>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<a-x> <a-k>^(\h*/\*|\h+\*(?!/))<ret>
# find comment opening, validate it was not closed, and check its using star prefixes
execute-keys -draft <a-?>/\*<ret><a-H> <a-K>\*/<ret> <a-k>\A\h*/\*([^\n]*\n\h*\*)*[^\n]*\n\h*.\z<ret>
try %[
# if the previous line is opening the comment, insert star preceeded by space
execute-keys -draft k<a-x><a-k>^\h*/\*<ret>
execute-keys -draft i*<space><esc>
] catch %[
try %[
# if the next line is a comment line insert a star
execute-keys -draft j<a-x><a-k>^\h+\*<ret>
execute-keys -draft i*<space><esc>
] catch %[
try %[
# if the previous line is an empty comment line, close the comment scope
execute-keys -draft k<a-x><a-k>^\h+\*\h+$<ret> <a-x>1s\*(\h*)<ret>c/<esc>
] catch %[
# if the previous line is a non-empty comment line, add a star
execute-keys -draft i*<space><esc>
]
]
]
# trim trailing whitespace on the previous line
try %[ execute-keys -draft s\h+$<ret> d ]
# align the new star with the previous one
execute-keys K<a-x>1s^[^*]*(\*)<ret>&
]
# # copy // comments prefix and following white spaces
# try %{ execute-keys -draft k <a-x> s ^\h*\K/{2,}\h* <ret> y gh j P }
> >
> >
define-command -hidden javascript-indent-on-new-line %< define-command -hidden javascript-indent-on-new-line %<
evaluate-commands -draft -itersel %< evaluate-commands -draft -itersel %<
# preserve previous line indent execute-keys <semicolon>
try %{ execute-keys -draft <semicolon> K <a-&> } try %<
# filter previous line # if previous line is part of a comment, do nothing
try %{ execute-keys -draft k : javascript-trim-indent <ret> } execute-keys -draft <a-?>/\*<ret> <a-K>^\h*[^/*\h]<ret>
# indent after lines beginning / ending with opener token > catch %<
try %_ execute-keys -draft k <a-x> s [[({] <ret> <space> <a-l> <a-K> [\])}] <ret> j <a-gt> _ # else if previous line closed a paren (possibly followed by words and a comment),
# deindent closing token(s) when after cursor # copy indent of the opening paren line
try %_ execute-keys -draft <a-x> <a-k> ^\h*[})\]] <ret> gh / [})\]] <ret> m <a-S> 1<a-&> _ execute-keys -draft k<a-x> 1s(\))(\h+\w+)*\h*(\;\h*)?(?://[^\n]+)?\n\z<ret> m<a-semicolon>J <a-S> 1<a-&>
> catch %<
# else indent new lines with the same level as the previous one
execute-keys -draft K <a-&>
>
# remove previous empty lines resulting from the automatic indent
try %< execute-keys -draft k <a-x> <a-k>^\h+$<ret> Hd >
# indent after an opening brace or parenthesis at end of line
try %< execute-keys -draft k <a-x> <a-k>[{(]\h*$<ret> j <a-gt> >
# indent after a label (works for case statements)
try %< execute-keys -draft k <a-x> s[a-zA-Z0-9_-]+:\h*$<ret> j <a-gt> >
# indent after a statement not followed by an opening brace
try %< execute-keys -draft k <a-x> s\)\h*(?://[^\n]+)?\n\z<ret> \
<a-semicolon>mB <a-k>\A\b(if|for|while)\b<ret> <a-semicolon>j <a-gt> >
try %< execute-keys -draft k <a-x> s \belse\b\h*(?://[^\n]+)?\n\z<ret> \
j <a-gt> >
# deindent after a single line statement end
try %< execute-keys -draft K <a-x> <a-k>\;\h*(//[^\n]+)?$<ret> \
K <a-x> s\)(\h+\w+)*\h*(//[^\n]+)?\n([^\n]*\n){2}\z<ret> \
MB <a-k>\A\b(if|for|while)\b<ret> <a-S>1<a-&> >
try %< execute-keys -draft K <a-x> <a-k>\;\h*(//[^\n]+)?$<ret> \
K <a-x> s \belse\b\h*(?://[^\n]+)?\n([^\n]*\n){2}\z<ret> \
<a-S>1<a-&> >
# deindent closing brace(s) when after cursor
try %< execute-keys -draft <a-x> <a-k> ^\h*[})] <ret> gh / [})] <esc> m <a-S> 1<a-&> >
# 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 [({],[)}] <ret> >
# Validate selection and get first and last char
execute-keys <a-k>\A[{(](\h*\S+)+\n<ret> <a-K>"(([^"]*"){2})*<ret> <a-K>'(([^']*'){2})*<ret> <a-:><a-semicolon>L <a-S>
# Remove possibly incorrect indent from new line which was copied from previous line
try %< execute-keys -draft <space> <a-h> s\h+<ret> d >
# Now indent and align that new line with the opening parenthesis/brace
execute-keys 1<a-&> &
> >
# # preserve previous line indent
# try %{ execute-keys -draft <semicolon> K <a-&> }
# # filter previous line
# try %{ execute-keys -draft k : javascript-trim-indent <ret> }
# # indent after lines beginning / ending with opener token
# try %_ execute-keys -draft k <a-x> s [[({] <ret> <space> <a-l> <a-K> [\])}] <ret> j <a-gt> _
# # deindent closing token(s) when after cursor
# try %_ execute-keys -draft <a-x> <a-k> ^\h*[})\]] <ret> gh / [})\]] <ret> m <a-S> 1<a-&> _
> >
> >