2014-07-07 20:22:16 +04:00
# Detection
# ‾‾‾‾‾‾‾‾‾
2022-05-09 20:20:27 +03:00
hook global BufCreate .*[.][cm]?(js)x? %{
2017-11-03 10:34:41 +03:00
set-option buffer filetype javascript
2014-07-07 20:22:16 +04:00
}
2018-01-08 01:17:37 +03:00
hook global BufCreate .*[.](ts)x? %{
set-option buffer filetype typescript
}
2014-07-07 20:22:16 +04:00
2019-04-10 06:54:19 +03:00
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook global WinSetOption filetype=(javascript|typescript) %{
require-module javascript
2019-10-16 12:19:43 +03:00
hook window ModeChange pop:insert:.* -group "%val{hook_param_capture_1}-trim-indent" javascript-trim-indent
2019-04-11 03:08:05 +03:00
hook window InsertChar .* -group "%val{hook_param_capture_1}-indent" javascript-indent-on-char
2021-04-17 11:17:01 +03:00
hook window InsertChar \n -group "%val{hook_param_capture_1}-insert" javascript-insert-on-new-line
2019-04-11 03:08:05 +03:00
hook window InsertChar \n -group "%val{hook_param_capture_1}-indent" javascript-indent-on-new-line
2019-04-10 06:54:19 +03:00
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 }
}
2019-04-13 22:57:57 +03:00
provide-module javascript %§
2019-03-13 08:24:33 +03:00
2014-07-07 20:22:16 +04:00
# Commands
# ‾‾‾‾‾‾‾‾
2018-12-19 12:10:26 +03:00
define-command -hidden javascript-trim-indent %{
2015-11-04 12:48:47 +03:00
# remove trailing white spaces
2022-03-17 01:20:07 +03:00
try %{ execute-keys -draft x 1s^(\h+)$<ret> d }
2014-07-07 20:22:16 +04:00
}
2017-11-03 10:34:41 +03:00
define-command -hidden javascript-indent-on-char %<
2017-11-03 11:09:45 +03:00
evaluate-commands -draft -itersel %<
2014-07-16 15:56:40 +04:00
# align closer token to its opener when alone on a line
2018-03-22 19:31:43 +03:00
try %/ execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m s \A|.\z <ret> 1<a-&> /
2015-09-02 15:30:34 +03:00
>
>
2014-07-07 20:22:16 +04:00
2021-04-17 11:17:01 +03:00
define-command -hidden javascript-insert-on-new-line %<
2017-11-03 11:09:45 +03:00
evaluate-commands -draft -itersel %<
2022-03-17 05:54:05 +03:00
execute-keys <semicolon>
2022-05-04 05:11:28 +03:00
try %[
evaluate-commands -draft -save-regs '/"' %[
# copy the commenting prefix
2022-03-17 01:20:07 +03:00
execute-keys -save-regs '' k x1s^\h*(//+\h*)<ret> y
2022-05-04 05:11:28 +03:00
try %[
# if the previous comment isn't empty, create a new one
2022-03-17 01:20:07 +03:00
execute-keys x<a-K>^\h*//+\h*$<ret> jxs^\h*<ret>P
2022-05-04 05:11:28 +03:00
] catch %[
# if there is no text in the previous comment, remove it completely
execute-keys d
]
]
]
2022-03-17 05:54:05 +03:00
try %[
# if the previous line isn't within a comment scope, break
2022-03-17 01:20:07 +03:00
execute-keys -draft kx <a-k>^(\h*/\*|\h+\*(?!/))<ret>
2022-03-17 05:54:05 +03:00
# 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
2022-03-17 01:20:07 +03:00
execute-keys -draft kx<a-k>^\h*/\*<ret>
2022-03-17 05:54:05 +03:00
execute-keys -draft i*<space><esc>
] catch %[
try %[
# if the next line is a comment line insert a star
2022-03-17 01:20:07 +03:00
execute-keys -draft jx<a-k>^\h+\*<ret>
2022-03-17 05:54:05 +03:00
execute-keys -draft i*<space><esc>
] catch %[
try %[
# if the previous line is an empty comment line, close the comment scope
2022-03-17 01:20:07 +03:00
execute-keys -draft kx<a-k>^\h+\*\h+$<ret> x1s\*(\h*)<ret>c/<esc>
2022-03-17 05:54:05 +03:00
] 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
2022-03-17 01:20:07 +03:00
execute-keys Kx1s^[^*]*(\*)<ret>&
2022-03-17 05:54:05 +03:00
]
2021-04-17 11:17:01 +03:00
>
>
define-command -hidden javascript-indent-on-new-line %<
evaluate-commands -draft -itersel %<
2022-03-17 05:54:05 +03:00
execute-keys <semicolon>
try %<
# if previous line is part of a comment, do nothing
execute-keys -draft <a-?>/\*<ret> <a-K>^\h*[^/*\h]<ret>
> catch %<
# else if previous line closed a paren (possibly followed by words and a comment),
# copy indent of the opening paren line
2022-03-17 01:20:07 +03:00
execute-keys -draft kx 1s(\))(\h+\w+)*\h*(\;\h*)?(?://[^\n]+)?\n\z<ret> m<a-semicolon>J <a-S> 1<a-&>
2022-03-17 05:54:05 +03:00
> 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
2022-03-17 01:20:07 +03:00
try %< execute-keys -draft k x <a-k>^\h+$<ret> Hd >
2022-03-17 05:54:05 +03:00
# indent after an opening brace or parenthesis at end of line
2022-03-17 01:20:07 +03:00
try %< execute-keys -draft k x <a-k>[{(]\h*$<ret> j <a-gt> >
2022-03-17 05:54:05 +03:00
# indent after a label (works for case statements)
2022-03-17 01:20:07 +03:00
try %< execute-keys -draft k x s[a-zA-Z0-9_-]+:\h*$<ret> j <a-gt> >
2022-03-17 05:54:05 +03:00
# indent after a statement not followed by an opening brace
2022-03-17 01:20:07 +03:00
try %< execute-keys -draft k x s\)\h*(?://[^\n]+)?\n\z<ret> \
2022-03-17 05:54:05 +03:00
<a-semicolon>mB <a-k>\A\b(if|for|while)\b<ret> <a-semicolon>j <a-gt> >
2022-03-17 01:20:07 +03:00
try %< execute-keys -draft k x s \belse\b\h*(?://[^\n]+)?\n\z<ret> \
2022-03-17 05:54:05 +03:00
j <a-gt> >
# deindent after a single line statement end
2022-03-17 01:20:07 +03:00
try %< execute-keys -draft K x <a-k>\;\h*(//[^\n]+)?$<ret> \
K x s\)(\h+\w+)*\h*(//[^\n]+)?\n([^\n]*\n){2}\z<ret> \
2022-03-17 05:54:05 +03:00
MB <a-k>\A\b(if|for|while)\b<ret> <a-S>1<a-&> >
2022-03-17 01:20:07 +03:00
try %< execute-keys -draft K x <a-k>\;\h*(//[^\n]+)?$<ret> \
K x s \belse\b\h*(?://[^\n]+)?\n([^\n]*\n){2}\z<ret> \
2022-03-17 05:54:05 +03:00
<a-S>1<a-&> >
# deindent closing brace(s) when after cursor
2022-03-17 01:20:07 +03:00
try %< execute-keys -draft x <a-k> ^\h*[})] <ret> gh / [})] <esc> m <a-S> 1<a-&> >
2022-03-17 05:54:05 +03:00
# 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
2022-04-15 01:14:17 +03:00
try %< execute-keys -draft , <a-h> s\h+<ret> d >
2022-03-17 05:54:05 +03:00
# Now indent and align that new line with the opening parenthesis/brace
execute-keys 1<a-&> &
> >
2015-09-02 15:30:34 +03:00
>
>
2014-07-07 20:22:16 +04:00
2018-01-08 01:17:37 +03:00
# Highlighting and hooks bulder for JavaScript and TypeScript
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
define-command -hidden init-javascript-filetype -params 1 %~
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
2014-07-07 20:22:16 +04:00
2018-07-01 12:53:35 +03:00
add-highlighter "shared/%arg{1}" regions
add-highlighter "shared/%arg{1}/code" default-region group
2018-07-02 13:59:12 +03:00
add-highlighter "shared/%arg{1}/double_string" region '"' (?<!\\)(\\\\)*" fill string
add-highlighter "shared/%arg{1}/single_string" region "'" (?<!\\)(\\\\)*' fill string
add-highlighter "shared/%arg{1}/literal" region "`" (?<!\\)(\\\\)*` group
add-highlighter "shared/%arg{1}/comment_line" region // '$' fill comment
add-highlighter "shared/%arg{1}/comment" region /\* \*/ fill comment
add-highlighter "shared/%arg{1}/shebang" region ^#! $ fill meta
2021-01-11 19:06:38 +03:00
add-highlighter "shared/%arg{1}/division" region '[\w\)\]]\K(/|(\h+/\s+))' '(?=\w)' group # Help Kakoune to better detect /…/ literals
2018-07-02 13:59:12 +03:00
add-highlighter "shared/%arg{1}/regex" region / (?<!\\)(\\\\)*/[gimuy]* fill meta
2020-01-11 14:40:21 +03:00
add-highlighter "shared/%arg{1}/jsx" region -recurse (?<![\w<])<[a-zA-Z>][\w:.-]* (?<![\w<])<[a-zA-Z>][\w:.-]*(?!\hextends)(?=[\s/>])(?!>\()) (</.*?>|/>) regions
2014-07-07 20:22:16 +04:00
2018-01-08 01:17:37 +03:00
# 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
2014-07-07 20:22:16 +04:00
2018-07-01 12:53:35 +03:00
add-highlighter "shared/%arg{1}/literal/" fill string
add-highlighter "shared/%arg{1}/literal/" regex \$\{.*?\} 0:value
2016-09-28 09:45:01 +03:00
2021-03-18 05:59:05 +03:00
add-highlighter "shared/%arg{1}/code/" regex (?:^|[^$_])\b(document|false|null|parent|self|this|true|undefined|window)\b 1:value
2018-07-01 12:53:35 +03:00
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
2018-01-08 01:17:37 +03:00
# 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.
2020-01-11 14:40:21 +03:00
add-highlighter "shared/%arg{1}/jsx/tag" region -recurse < <(?=[/a-zA-Z>]) (?<!=)> regions
2018-07-02 13:59:12 +03:00
add-highlighter "shared/%arg{1}/jsx/expr" region -recurse \{ \{ \} ref %arg{1}
2018-01-08 01:17:37 +03:00
2018-07-01 12:53:35 +03:00
add-highlighter "shared/%arg{1}/jsx/tag/base" default-region group
2018-07-02 13:59:12 +03:00
add-highlighter "shared/%arg{1}/jsx/tag/double_string" region =\K" (?<!\\)(\\\\)*" fill string
add-highlighter "shared/%arg{1}/jsx/tag/single_string" region =\K' (?<!\\)(\\\\)*' fill string
add-highlighter "shared/%arg{1}/jsx/tag/expr" region -recurse \{ \{ \} group
2018-01-08 01:17:37 +03:00
2018-07-01 12:53:35 +03:00
add-highlighter "shared/%arg{1}/jsx/tag/base/" regex (\w+) 1:attribute
add-highlighter "shared/%arg{1}/jsx/tag/base/" regex </?([\w-$]+) 1:keyword
add-highlighter "shared/%arg{1}/jsx/tag/base/" regex (</?|/?>) 0:meta
2018-01-08 01:17:37 +03:00
2018-07-01 12:53:35 +03:00
add-highlighter "shared/%arg{1}/jsx/tag/expr/" ref %arg{1}
2018-01-08 01:17:37 +03:00
# Keywords are collected at
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
2019-04-24 04:33:54 +03:00
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set
2019-04-19 13:31:39 +03:00
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
2018-01-08 01:17:37 +03:00
~
init-javascript-filetype javascript
init-javascript-filetype typescript
# Highlighting specific to TypeScript
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
2018-07-01 12:53:35 +03:00
add-highlighter shared/typescript/code/ regex \b(array|boolean|date|number|object|regexp|string|symbol)\b 0:type
2018-01-08 01:17:37 +03:00
# Keywords grabbed from https://github.com/Microsoft/TypeScript/issues/2536
2018-07-01 12:53:35 +03:00
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
2019-03-13 08:24:33 +03:00
2019-04-13 22:57:57 +03:00
§
2019-07-23 09:48:30 +03:00
# Aliases
# ‾‾‾‾‾‾‾
provide-module typescript %{ require-module javascript }