kakoune

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

dart.kak (5330B)


      1 # https://dartlang.org/
      2 #
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*\.dart %{
      8     set-option buffer filetype dart
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=dart %{
     15     require-module dart
     16 
     17     set-option window static_words %opt{dart_static_words}
     18 
     19     # cleanup trailing whitespaces when exiting insert mode
     20     hook window ModeChange pop:insert:.* -group dart-trim-indent %{ try %{ execute-keys -draft xs^\h+$<ret>d } }
     21     hook window InsertChar \n -group dart-insert dart-insert-on-new-line
     22     hook window InsertChar \n -group dart-indent dart-indent-on-new-line
     23     hook window InsertChar \{ -group dart-indent dart-indent-on-opening-curly-brace
     24     hook window InsertChar \} -group dart-indent dart-indent-on-closing-curly-brace
     25 
     26     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window dart-.+ }
     27 }
     28 
     29 hook -group dart-highlight global WinSetOption filetype=dart %{
     30     add-highlighter window/dart ref dart
     31     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/dart }
     32 }
     33 
     34 
     35 provide-module dart %§
     36 
     37 # Highlighters
     38 # ‾‾‾‾‾‾‾‾‾‾‾‾
     39 
     40 add-highlighter shared/dart regions
     41 add-highlighter shared/dart/code default-region group
     42 add-highlighter shared/dart/double_string region '"' (?<!\\)(\\\\)*" group
     43 add-highlighter shared/dart/single_string region "'" (?<!\\)(\\\\)*' group
     44 add-highlighter shared/dart/comment region /\* \*/ fill comment
     45 add-highlighter shared/dart/comment_line region '//' $ fill comment
     46 
     47 add-highlighter shared/dart/code/ regex %{-?([0-9]*\.(?!0[xX]))?\b([0-9]+|0[xX][0-9a-fA-F]+)\.?([eE][+-]?[0-9]+)?i?\b} 0:value
     48 
     49 # String interpolation
     50 add-highlighter shared/dart/double_string/ fill string
     51 add-highlighter shared/dart/double_string/ regex \$\{.*?\} 0:value
     52 add-highlighter shared/dart/single_string/ fill string
     53 add-highlighter shared/dart/single_string/ regex \$\{.*?\} 0:value
     54 
     55 evaluate-commands %sh{
     56     # Grammar
     57     keywords="abstract|do|import|super|as|in|switch|assert|else|interface|async"
     58     keywords="${keywords}|enum|is|this|export|library|throw|await|external|mixin|break|extends"
     59     keywords="${keywords}|new|try|case|factory|typedef|catch|operator|class|final|part|extension"
     60     keywords="${keywords}|const|finally|rethrow|while|continue|for|return|with|covariant"
     61     keywords="${keywords}|get|set|yield|default|if|static|deferred|implements"
     62     generator_keywords="async\*|sync\*|yield\*"
     63 
     64     types="void|bool|num|int|double|dynamic|var"
     65     values="false|true|null"
     66 
     67     annotations="@[a-zA-Z]+"
     68     functions="(_?[a-z][a-zA-Z0-9]*)(\(|\w+=>)"
     69     classes="_?[A-Z][a-zA-Z0-9]*"
     70 
     71     # Add the language's grammar to the static completion list
     72     printf %s\\n "declare-option str-list dart_static_words ${keywords} ${attributes} ${types} ${values}" | tr '|' ' '
     73 
     74     # Highlight keywords
     75     printf %s "
     76         add-highlighter shared/dart/code/ regex \b(${keywords})\b 0:keyword
     77         add-highlighter shared/dart/code/ regex \b(${generator_keywords}) 0:keyword
     78         add-highlighter shared/dart/code/ regex \b(${attributes})\b 0:attribute
     79         add-highlighter shared/dart/code/ regex \b(${types})\b 0:type
     80         add-highlighter shared/dart/code/ regex \b(${values})\b 0:value
     81         add-highlighter shared/dart/code/ regex \b(${functions}) 2:function
     82         add-highlighter shared/dart/code/ regex (${annotations})\b 0:meta
     83         add-highlighter shared/dart/code/ regex \b(${classes})\b 0:module
     84     "
     85 }
     86 
     87 # Commands
     88 # ‾‾‾‾‾‾‾‾
     89 
     90 define-command -hidden dart-insert-on-new-line %~
     91     evaluate-commands -draft -itersel %=
     92         # copy // comments prefix and following white spaces
     93         try %{ execute-keys -draft <semicolon><c-s>kx s ^\h*\K/{2,}\h* <ret> y<c-o>P<esc> }
     94     =
     95 ~
     96 
     97 define-command -hidden dart-indent-on-new-line %~
     98     evaluate-commands -draft -itersel %=
     99         # preserve previous line indent
    100         try %{ execute-keys -draft <semicolon>K<a-&> }
    101         # indent after lines ending with { or (
    102         try %[ execute-keys -draft kx <a-k> [{(]\h*$ <ret> j<a-gt> ]
    103         # cleanup trailing white spaces on the previous line
    104         try %{ execute-keys -draft kx s \h+$ <ret>d }
    105         # align to opening paren of previous line
    106         try %{ execute-keys -draft [( <a-k> \A\(\N+\n\N*\n?\z <ret> s \A\(\h*.|.\z <ret> '<a-;>' & }
    107         # indent after a switch's case/default statements
    108         try %[ execute-keys -draft kx <a-k> ^\h*(case|default).*:$ <ret> j<a-gt> ]
    109         # indent after if|else|while|for
    110         try %[ execute-keys -draft <semicolon><a-F>)MB <a-k> \A(if|else|while|for)\h*\(.*\)\h*\n\h*\n?\z <ret> s \A|.\z <ret> 1<a-&>1<a-,><a-gt> ]
    111         # deindent closing brace when after cursor
    112         try %[ execute-keys -draft x <a-k> ^\h*\} <ret> gh / \} <ret> m <a-S> 1<a-&> ]
    113     =
    114 ~
    115 
    116 define-command -hidden dart-indent-on-opening-curly-brace %[
    117     # align indent with opening paren when { is entered on a new line after the closing paren
    118     try %[ execute-keys -draft -itersel h<a-F>)M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ]
    119 ]
    120 
    121 define-command -hidden dart-indent-on-closing-curly-brace %[
    122     # align to opening curly brace when alone on a line
    123     try %[ execute-keys -itersel -draft <a-h><a-k>^\h+\}$<ret>hms\A|.\z<ret>1<a-&> ]
    124 ]
    125 
    126 §