From 904cec3c4a329cf89fc3219d359239910d61f3f6 Mon Sep 17 00:00:00 2001 From: thing1 Date: Tue, 28 Jan 2025 09:14:32 +0000 Subject: init commit --- autoload/filetype/javascript.kak | 224 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 autoload/filetype/javascript.kak (limited to 'autoload/filetype/javascript.kak') diff --git a/autoload/filetype/javascript.kak b/autoload/filetype/javascript.kak new file mode 100644 index 0000000..05e0bc6 --- /dev/null +++ b/autoload/filetype/javascript.kak @@ -0,0 +1,224 @@ +# Detection +# ‾‾‾‾‾‾‾‾‾ + +hook global BufCreate .*[.][cm]?(js)x? %{ + set-option buffer filetype javascript +} + +hook global BufCreate .*[.][cm]?(ts)x? %{ + set-option buffer filetype typescript +} + +# Initialization +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +hook global WinSetOption filetype=(javascript|typescript) %{ + require-module javascript + + hook window ModeChange pop:insert:.* -group "%val{hook_param_capture_1}-trim-indent" javascript-trim-indent + hook window InsertChar .* -group "%val{hook_param_capture_1}-indent" javascript-indent-on-char + hook window InsertChar \n -group "%val{hook_param_capture_1}-insert" javascript-insert-on-new-line + hook window InsertChar \n -group "%val{hook_param_capture_1}-indent" javascript-indent-on-new-line + + hook -once -always window WinSetOption filetype=.* " + remove-hooks window %val{hook_param_capture_1}-.+ + " +} + +hook -group javascript-highlight global WinSetOption filetype=javascript %{ + add-highlighter window/javascript ref javascript + hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/javascript } +} + +hook -group typescript-highlight global WinSetOption filetype=typescript %{ + add-highlighter window/typescript ref typescript + hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/typescript } +} + + +provide-module javascript %§ + +# Commands +# ‾‾‾‾‾‾‾‾ + +define-command -hidden javascript-trim-indent %{ + # remove trailing white spaces + try %{ execute-keys -draft x 1s^(\h+)$ d } +} + +define-command -hidden javascript-indent-on-char %< + evaluate-commands -draft -itersel %< + # align closer token to its opener when alone on a line + try %/ execute-keys -draft ^\h+[\]}]$ m s \A|.\z 1 / + > +> + +define-command -hidden javascript-insert-on-new-line %< + evaluate-commands -draft -itersel %< + execute-keys + try %[ + evaluate-commands -draft -save-regs '/"' %[ + # copy the commenting prefix + execute-keys -save-regs '' k x1s^\h*(//+\h*) y + try %[ + # if the previous comment isn't empty, create a new one + execute-keys x^\h*//+\h*$ jxs^\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 kx ^(\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 kx^\h*/\* + execute-keys -draft i* + ] catch %[ + try %[ + # if the next line is a comment line insert a star + execute-keys -draft jx^\h+\* + execute-keys -draft i* + ] catch %[ + try %[ + # if the previous line is an empty comment line, close the comment scope + execute-keys -draft kx^\h+\*\h+$ x1s\*(\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 Kx1s^[^*]*(\*) + ] + > +> + +define-command -hidden javascript-indent-on-new-line %< + evaluate-commands -draft -itersel %< + 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 kx 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 x ^\h+$ Hd > + # indent after an opening brace or parenthesis at end of line + try %< execute-keys -draft k x [{(]\h*$ j > + # indent after a label (works for case statements) + try %< execute-keys -draft k x s[a-zA-Z0-9_-]+:\h*$ j > + # indent after a statement not followed by an opening brace + try %< execute-keys -draft k x s\)\h*(?://[^\n]+)?\n\z \ + mB \A\b(if|for|while)\b j > + try %< execute-keys -draft k x s \belse\b\h*(?://[^\n]+)?\n\z \ + j > + # deindent after a single line statement end + try %< execute-keys -draft K x \;\h*(//[^\n]+)?$ \ + K x s\)(\h+\w+)*\h*(//[^\n]+)?\n([^\n]*\n){2}\z \ + MB \A\b(if|for|while)\b 1 > + try %< execute-keys -draft K x \;\h*(//[^\n]+)?$ \ + K x s \belse\b\h*(?://[^\n]+)?\n([^\n]*\n){2}\z \ + 1 > + # deindent closing brace(s) when after cursor + try %< execute-keys -draft x ^\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 & + > > + > +> + +# Highlighting and hooks bulder for JavaScript and TypeScript +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +define-command -hidden init-javascript-filetype -params 1 %~ + # Highlighters + # ‾‾‾‾‾‾‾‾‾‾‾‾ + + add-highlighter "shared/%arg{1}" regions + add-highlighter "shared/%arg{1}/code" default-region group + add-highlighter "shared/%arg{1}/double_string" region '"' (?][\w:.-]* (?][\w:.-]*(?!\hextends)(?=[\s/>])(?!>\()) (|/>) regions + + # Regular expression flags are: g → global match, i → ignore case, m → multi-lines, u → unicode, y → sticky + # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp + + add-highlighter "shared/%arg{1}/literal/" fill string + add-highlighter "shared/%arg{1}/literal/" regex \$\{.*?\} 0:value + + add-highlighter "shared/%arg{1}/code/" regex (?:^|[^$_])\b(document|false|null|parent|self|this|true|undefined|window)\b 1:value + add-highlighter "shared/%arg{1}/code/" regex "-?\b[0-9]*\.?[0-9]+" 0:value + add-highlighter "shared/%arg{1}/code/" regex \b(Array|Boolean|Date|Function|Number|Object|RegExp|String|Symbol)\b 0:type + + # jsx: In well-formed xml the number of opening and closing tags match up regardless of tag name. + # + # We inline a small XML highlighter here since it anyway need to recurse back up to the starting highlighter. + # To make things simple we assume that jsx is always enabled. + + add-highlighter "shared/%arg{1}/jsx/tag" region -recurse < <(?=[/a-zA-Z>]) (? regions + add-highlighter "shared/%arg{1}/jsx/expr" region -recurse \{ \{ \} ref %arg{1} + + add-highlighter "shared/%arg{1}/jsx/tag/base" default-region group + add-highlighter "shared/%arg{1}/jsx/tag/double_string" region =\K" (?) 0:meta + + add-highlighter "shared/%arg{1}/jsx/tag/expr/" ref %arg{1} + + # Keywords are collected at + # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords + # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get + # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set + add-highlighter "shared/%arg{1}/code/" regex \b(async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|export|extends|finally|for|function|get|if|import|in|instanceof|let|new|of|return|set|static|super|switch|throw|try|typeof|var|void|while|with|yield)\b 0:keyword +~ + +init-javascript-filetype javascript +init-javascript-filetype typescript + +# Highlighting specific to TypeScript +# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ +add-highlighter shared/typescript/code/ regex \b(array|boolean|date|number|object|regexp|string|symbol)\b 0:type + +# Keywords grabbed from https://github.com/Microsoft/TypeScript/issues/2536 +add-highlighter shared/typescript/code/ regex \b(as|constructor|declare|enum|from|implements|interface|module|namespace|package|private|protected|public|readonly|static|type)\b 0:keyword + +§ + +# Aliases +# ‾‾‾‾‾‾‾ +provide-module typescript %{ require-module javascript } -- cgit v1.2.3