2014-07-07 18:22:16 +02:00
# Detection
# ‾‾‾‾‾‾‾‾‾
2019-12-03 07:05:40 +01:00
hook global BufCreate .*[.]m?(js)x? %{
2017-11-03 08:34:41 +01:00
set-option buffer filetype javascript
2014-07-07 18:22:16 +02:00
}
2018-01-07 23:17:37 +01:00
hook global BufCreate .*[.](ts)x? %{
set-option buffer filetype typescript
}
2014-07-07 18:22:16 +02:00
2019-04-10 05:54:19 +02:00
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook global WinSetOption filetype=(javascript|typescript) %{
require-module javascript
2019-10-16 11:19:43 +02:00
hook window ModeChange pop:insert:.* -group "%val{hook_param_capture_1}-trim-indent" javascript-trim-indent
2019-04-11 02:08:05 +02:00
hook window InsertChar .* -group "%val{hook_param_capture_1}-indent" javascript-indent-on-char
hook window InsertChar \n -group "%val{hook_param_capture_1}-indent" javascript-indent-on-new-line
2019-04-10 05:54:19 +02: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 21:57:57 +02:00
provide-module javascript %§
2019-03-13 06:24:33 +01:00
2014-07-07 18:22:16 +02:00
# Commands
# ‾‾‾‾‾‾‾‾
2018-12-19 10:10:26 +01:00
define-command -hidden javascript-trim-indent %{
2015-11-04 10:48:47 +01:00
# remove trailing white spaces
2017-11-03 09:09:45 +01:00
try %{ execute-keys -draft -itersel <a-x> s \h+$ <ret> d }
2014-07-07 18:22:16 +02:00
}
2017-11-03 08:34:41 +01:00
define-command -hidden javascript-indent-on-char %<
2017-11-03 09:09:45 +01:00
evaluate-commands -draft -itersel %<
2014-07-16 13:56:40 +02:00
# align closer token to its opener when alone on a line
2018-03-22 17:31:43 +01:00
try %/ execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m s \A|.\z <ret> 1<a-&> /
2015-09-02 14:30:34 +02:00
>
>
2014-07-07 18:22:16 +02:00
2017-11-03 08:34:41 +01:00
define-command -hidden javascript-indent-on-new-line %<
2017-11-03 09:09:45 +01:00
evaluate-commands -draft -itersel %<
2017-01-11 14:56:48 +01:00
# copy // comments prefix and following white spaces
2017-11-03 09:09:45 +01:00
try %{ execute-keys -draft k <a-x> s ^\h*\K#\h* <ret> y gh j P }
2014-07-07 18:22:16 +02:00
# preserve previous line indent
2019-10-22 11:02:06 +02:00
try %{ execute-keys -draft <semicolon> K <a-&> }
2014-07-07 18:22:16 +02:00
# filter previous line
2018-12-19 10:10:26 +01:00
try %{ execute-keys -draft k : javascript-trim-indent <ret> }
2014-07-07 18:22:16 +02:00
# indent after lines beginning / ending with opener token
2020-07-29 23:04:14 +02:00
try %_ execute-keys -draft k <a-x> s [[({] <ret> <space> <a-l> <a-K> [\])}] <ret> j <a-gt> _
2020-07-28 04:39:44 +02:00
# deindent closing token(s) when after cursor
2020-07-29 17:34:29 +02:00
try %_ execute-keys -draft <a-x> <a-k> ^\h*[})\]] <ret> gh / [})\]] <ret> m <a-S> 1<a-&> _
2015-09-02 14:30:34 +02:00
>
>
2014-07-07 18:22:16 +02:00
2018-01-07 23:17:37 +01:00
# Highlighting and hooks bulder for JavaScript and TypeScript
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
define-command -hidden init-javascript-filetype -params 1 %~
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
2014-07-07 18:22:16 +02:00
2018-07-01 11:53:35 +02:00
add-highlighter "shared/%arg{1}" regions
add-highlighter "shared/%arg{1}/code" default-region group
2018-07-02 12:59:12 +02: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
2020-11-04 16:01:56 +01:00
add-highlighter "shared/%arg{1}/division" region '[\w\)\]]\K(/|(\h+/\h+))' '(?=\w)' group # Help Kakoune to better detect /…/ literals
2018-07-02 12:59:12 +02:00
add-highlighter "shared/%arg{1}/regex" region / (?<!\\)(\\\\)*/[gimuy]* fill meta
2020-01-11 12:40:21 +01: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 18:22:16 +02:00
2018-01-07 23:17:37 +01: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 18:22:16 +02:00
2018-07-01 11:53:35 +02:00
add-highlighter "shared/%arg{1}/literal/" fill string
add-highlighter "shared/%arg{1}/literal/" regex \$\{.*?\} 0:value
2016-09-28 08:45:01 +02:00
2018-07-01 11:53:35 +02:00
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
2018-01-07 23:17:37 +01: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 12:40:21 +01:00
add-highlighter "shared/%arg{1}/jsx/tag" region -recurse < <(?=[/a-zA-Z>]) (?<!=)> regions
2018-07-02 12:59:12 +02:00
add-highlighter "shared/%arg{1}/jsx/expr" region -recurse \{ \{ \} ref %arg{1}
2018-01-07 23:17:37 +01:00
2018-07-01 11:53:35 +02:00
add-highlighter "shared/%arg{1}/jsx/tag/base" default-region group
2018-07-02 12:59:12 +02: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-07 23:17:37 +01:00
2018-07-01 11:53:35 +02: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-07 23:17:37 +01:00
2018-07-01 11:53:35 +02:00
add-highlighter "shared/%arg{1}/jsx/tag/expr/" ref %arg{1}
2018-01-07 23:17:37 +01:00
# Keywords are collected at
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
2019-04-24 03:33:54 +02: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 12:31:39 +02: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-07 23:17:37 +01:00
~
init-javascript-filetype javascript
init-javascript-filetype typescript
# Highlighting specific to TypeScript
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
2018-07-01 11:53:35 +02:00
add-highlighter shared/typescript/code/ regex \b(array|boolean|date|number|object|regexp|string|symbol)\b 0:type
2018-01-07 23:17:37 +01:00
# Keywords grabbed from https://github.com/Microsoft/TypeScript/issues/2536
2018-07-01 11:53:35 +02: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 06:24:33 +01:00
2019-04-13 21:57:57 +02:00
§
2019-07-23 08:48:30 +02:00
# Aliases
# ‾‾‾‾‾‾‾
provide-module typescript %{ require-module javascript }