kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

javascript.kak (11215B)


      1 # Detection
      2 # ‾‾‾‾‾‾‾‾‾
      3 
      4 hook global BufCreate .*[.][cm]?(js)x? %{
      5     set-option buffer filetype javascript
      6 }
      7 
      8 hook global BufCreate .*[.][cm]?(ts)x? %{
      9     set-option buffer filetype typescript
     10 }
     11 
     12 # Initialization
     13 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     14 
     15 hook global WinSetOption filetype=(javascript|typescript) %{
     16     require-module javascript
     17 
     18     hook window ModeChange pop:insert:.* -group "%val{hook_param_capture_1}-trim-indent" javascript-trim-indent
     19     hook window InsertChar .* -group "%val{hook_param_capture_1}-indent" javascript-indent-on-char
     20     hook window InsertChar \n -group "%val{hook_param_capture_1}-insert" javascript-insert-on-new-line
     21     hook window InsertChar \n -group "%val{hook_param_capture_1}-indent" javascript-indent-on-new-line
     22 
     23     hook -once -always window WinSetOption filetype=.* "
     24         remove-hooks window %val{hook_param_capture_1}-.+
     25     "
     26 }
     27 
     28 hook -group javascript-highlight global WinSetOption filetype=javascript %{
     29     add-highlighter window/javascript ref javascript
     30     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/javascript }
     31 }
     32 
     33 hook -group typescript-highlight global WinSetOption filetype=typescript %{
     34     add-highlighter window/typescript ref typescript
     35     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/typescript }
     36 }
     37 
     38 
     39 provide-module javascript %§
     40 
     41 # Commands
     42 # ‾‾‾‾‾‾‾‾
     43 
     44 define-command -hidden javascript-trim-indent %{
     45     # remove trailing white spaces
     46     try %{ execute-keys -draft x 1s^(\h+)$<ret> d }
     47 }
     48 
     49 define-command -hidden javascript-indent-on-char %<
     50     evaluate-commands -draft -itersel %<
     51         # align closer token to its opener when alone on a line
     52         try %/ execute-keys -draft <a-h> <a-k> ^\h+[\]}]$ <ret> m s \A|.\z <ret> 1<a-&> /
     53     >
     54 >
     55 
     56 define-command -hidden javascript-insert-on-new-line %<
     57     evaluate-commands -draft -itersel %<
     58     execute-keys <semicolon>
     59     try %[
     60         evaluate-commands -draft -save-regs '/"' %[
     61             # copy the commenting prefix
     62             execute-keys -save-regs '' k x1s^\h*(//+\h*)<ret> y
     63             try %[
     64                 # if the previous comment isn't empty, create a new one
     65                 execute-keys x<a-K>^\h*//+\h*$<ret> jxs^\h*<ret>P
     66             ] catch %[
     67                 # if there is no text in the previous comment, remove it completely
     68                 execute-keys d
     69             ]
     70         ]
     71     ]
     72     try %[
     73         # if the previous line isn't within a comment scope, break
     74         execute-keys -draft kx <a-k>^(\h*/\*|\h+\*(?!/))<ret>
     75 
     76         # find comment opening, validate it was not closed, and check its using star prefixes
     77         execute-keys -draft <a-?>/\*<ret><a-H> <a-K>\*/<ret> <a-k>\A\h*/\*(\N*\n\h*\*)*\N*\n\h*.\z<ret>
     78 
     79         try %[
     80             # if the previous line is opening the comment, insert star preceeded by space
     81             execute-keys -draft kx<a-k>^\h*/\*<ret>
     82             execute-keys -draft i*<space><esc>
     83         ] catch %[
     84            try %[
     85                 # if the next line is a comment line insert a star
     86                 execute-keys -draft jx<a-k>^\h+\*<ret>
     87                 execute-keys -draft i*<space><esc>
     88             ] catch %[
     89                 try %[
     90                     # if the previous line is an empty comment line, close the comment scope
     91                     execute-keys -draft kx<a-k>^\h+\*\h+$<ret> x1s\*(\h*)<ret>c/<esc>
     92                 ] catch %[
     93                     # if the previous line is a non-empty comment line, add a star
     94                     execute-keys -draft i*<space><esc>
     95                 ]
     96             ]
     97         ]
     98 
     99         # trim trailing whitespace on the previous line
    100         try %[ execute-keys -draft s\h+$<ret> d ]
    101         # align the new star with the previous one
    102         execute-keys Kx1s^[^*]*(\*)<ret><a-(><a-&>
    103     ]
    104     >
    105 >
    106 
    107 define-command -hidden javascript-indent-on-new-line %<
    108     evaluate-commands -draft -itersel %<
    109     execute-keys <semicolon>
    110     try %<
    111         # if previous line is part of a comment, do nothing
    112         execute-keys -draft <a-?>/\*<ret> <a-K>^\h*[^/*\h]<ret>
    113     > catch %<
    114         # else if previous line closed a paren (possibly followed by words and a comment),
    115         # copy indent of the opening paren line
    116         execute-keys -draft kx 1s(\))(\h+\w+)*\h*(\;\h*)?(?://\N+)?\n\z<ret> m<a-semicolon>J <a-S> 1<a-&>
    117     > catch %<
    118         # else indent new lines with the same level as the previous one
    119         execute-keys -draft K <a-&>
    120     >
    121     # remove previous empty lines resulting from the automatic indent
    122     try %< execute-keys -draft k x <a-k>^\h+$<ret> Hd >
    123     # indent after an opening brace or parenthesis at end of line
    124     try %< execute-keys -draft k x <a-k>[{(]\h*$<ret> j <a-gt> >
    125     # indent after a label (works for case statements)
    126     try %< execute-keys -draft k x s[a-zA-Z0-9_-]+:\h*$<ret> j <a-gt> >
    127     # indent after a statement not followed by an opening brace
    128     try %< execute-keys -draft k x s\)\h*(?://\N+)?\n\z<ret> \
    129                                <a-semicolon>mB <a-k>\A\b(if|for|while)\b<ret> <a-semicolon>j <a-gt> >
    130     try %< execute-keys -draft k x s \belse\b\h*(?://\N+)?\n\z<ret> \
    131                                j <a-gt> >
    132     # deindent after a single line statement end
    133     try %< execute-keys -draft K x <a-k>\;\h*(//\N+)?$<ret> \
    134                                K x s\)(\h+\w+)*\h*(//\N+)?\n(\N*\n){2}\z<ret> \
    135                                MB <a-k>\A\b(if|for|while)\b<ret> <a-S>1<a-&> >
    136     try %< execute-keys -draft K x <a-k>\;\h*(//\N+)?$<ret> \
    137                                K x s \belse\b\h*(?://\N+)?\n(\N*\n){2}\z<ret> \
    138                                <a-S>1<a-&> >
    139     # deindent closing brace(s) when after cursor
    140     try %< execute-keys -draft x <a-k> ^\h*[})] <ret> gh / [})] <esc> m <a-S> 1<a-&> >
    141     # align to the opening parenthesis or opening brace (whichever is first)
    142     # on a previous line if its followed by text on the same line
    143     try %< evaluate-commands -draft %<
    144         # Go to opening parenthesis and opening brace, then select the most nested one
    145         try %< execute-keys [c [({],[)}] <ret> >
    146         # Validate selection and get first and last char
    147         execute-keys <a-k>\A[{(](\h*\S+)+\n<ret> <a-K>"(([^"]*"){2})*<ret> <a-K>'(([^']*'){2})*<ret> <a-:><a-semicolon>L <a-S>
    148         # Remove possibly incorrect indent from new line which was copied from previous line
    149         try %< execute-keys -draft , <a-h> s\h+<ret> d >
    150         # Now indent and align that new line with the opening parenthesis/brace
    151         execute-keys 1<a-&> &
    152      > >
    153     >
    154 >
    155 
    156 # Highlighting and hooks bulder for JavaScript and TypeScript
    157 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
    158 define-command -hidden init-javascript-filetype -params 1 %~
    159     # Highlighters
    160     # ‾‾‾‾‾‾‾‾‾‾‾‾
    161 
    162     add-highlighter "shared/%arg{1}" regions
    163     add-highlighter "shared/%arg{1}/code" default-region group
    164     add-highlighter "shared/%arg{1}/double_string" region '"'  (?<!\\)(\\\\)*"         fill string
    165     add-highlighter "shared/%arg{1}/single_string" region "'"  (?<!\\)(\\\\)*'         fill string
    166     add-highlighter "shared/%arg{1}/literal"       region "`"  (?<!\\)(\\\\)*`         group
    167     add-highlighter "shared/%arg{1}/comment_line"  region //   '$'                     fill comment
    168     add-highlighter "shared/%arg{1}/comment"       region /\*  \*/                     fill comment
    169     add-highlighter "shared/%arg{1}/shebang"       region ^#!  $                       fill meta
    170     add-highlighter "shared/%arg{1}/division" region '[\w\)\]]\K(/|(\h+/\s+))' '(?=\w)' group # Help Kakoune to better detect /…/ literals
    171     add-highlighter "shared/%arg{1}/regex"         region /    (?<!\\)(\\\\)*/[gimuy]* fill meta
    172     add-highlighter "shared/%arg{1}/jsx"           region -recurse (?<![\w<])<[\w>][\w:.-]* (?<![\w<])<[\w>][\w:.-]*(?!\hextends)(?=[\s/>])(?!>\()) (</.*?>|/>) regions
    173 
    174     # Regular expression flags are: g → global match, i → ignore case, m → multi-lines, u → unicode, y → sticky
    175     # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
    176 
    177     add-highlighter "shared/%arg{1}/literal/"       fill string
    178     add-highlighter "shared/%arg{1}/literal/"       regex \$\{.*?\} 0:value
    179 
    180     add-highlighter "shared/%arg{1}/code/" regex (?:^|[^$_])\b(document|false|null|parent|self|this|true|undefined|window)\b 1:value
    181     add-highlighter "shared/%arg{1}/code/" regex "-?\b[0-9]*\.?[0-9]+" 0:value
    182     add-highlighter "shared/%arg{1}/code/" regex \b(Array|Boolean|Date|Function|Number|Object|RegExp|String|Symbol)\b 0:type
    183 
    184     # jsx: In well-formed xml the number of opening and closing tags match up regardless of tag name.
    185     #
    186     # We inline a small XML highlighter here since it anyway need to recurse back up to the starting highlighter.
    187     # To make things simple we assume that jsx is always enabled.
    188 
    189     add-highlighter "shared/%arg{1}/jsx/tag"  region -recurse <  <(?=[/\w>]) (?<!=)> regions
    190     add-highlighter "shared/%arg{1}/jsx/expr" region -recurse \{ \{             \}      ref %arg{1}
    191 
    192     add-highlighter "shared/%arg{1}/jsx/tag/base" default-region group
    193     add-highlighter "shared/%arg{1}/jsx/tag/double_string" region =\K" (?<!\\)(\\\\)*" fill string
    194     add-highlighter "shared/%arg{1}/jsx/tag/single_string" region =\K' (?<!\\)(\\\\)*' fill string
    195     add-highlighter "shared/%arg{1}/jsx/tag/expr" region -recurse \{ \{   \}           group
    196 
    197     add-highlighter "shared/%arg{1}/jsx/tag/base/" regex (\w+) 1:attribute
    198     add-highlighter "shared/%arg{1}/jsx/tag/base/" regex </?([\w-$]+) 1:keyword
    199     add-highlighter "shared/%arg{1}/jsx/tag/base/" regex (</?|/?>) 0:meta
    200 
    201     add-highlighter "shared/%arg{1}/jsx/tag/expr/"   ref %arg{1}
    202 
    203     # Keywords are collected at
    204     # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
    205     # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
    206     # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set
    207     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
    208 ~
    209 
    210 init-javascript-filetype javascript
    211 init-javascript-filetype typescript
    212 
    213 # Highlighting specific to TypeScript
    214 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
    215 add-highlighter shared/typescript/code/ regex \b(array|boolean|date|number|object|regexp|string|symbol)\b 0:type
    216 
    217 # Keywords grabbed from https://github.com/Microsoft/TypeScript/issues/2536
    218 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
    219 
    220 §
    221 
    222 # Aliases
    223 # ‾‾‾‾‾‾‾
    224 provide-module typescript %{ require-module javascript }