kakoune

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

rust.kak (13483B)


      1 # http://rust-lang.org
      2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
      3 
      4 # Detection
      5 # ‾‾‾‾‾‾‾‾‾
      6 
      7 hook global BufCreate .*[.](rust|rs) %{
      8     set-option buffer filetype rust
      9 }
     10 
     11 # Initialization
     12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
     13 
     14 hook global WinSetOption filetype=rust %<
     15     require-module rust
     16     hook window ModeChange pop:insert:.* -group rust-trim-indent rust-trim-indent
     17     hook window InsertChar \n -group rust-indent rust-indent-on-new-line
     18     hook window InsertChar \{ -group rust-indent rust-indent-on-opening-curly-brace
     19     hook window InsertChar [)}\]] -group rust-indent rust-indent-on-closing
     20     hook -once -always window WinSetOption filetype=.* %{ remove-hooks window rust-.+ }
     21 >
     22 
     23 hook -group rust-highlight global WinSetOption filetype=rust %{
     24     add-highlighter window/rust ref rust
     25     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/rust }
     26 }
     27 
     28 provide-module rust %§
     29 
     30 # Highlighters
     31 # ‾‾‾‾‾‾‾‾‾‾‾‾
     32 
     33 add-highlighter shared/rust regions
     34 add-highlighter shared/rust/code default-region group
     35 add-highlighter shared/rust/string           region %{(?<!')(?<!'\\)"} (?<!\\)(\\\\)*"      fill string
     36 add-highlighter shared/rust/raw_string       region -match-capture %{(?<!')r(#*)"} %{"(#*)} fill string
     37 
     38 add-highlighter shared/rust/line_doctest region ^\h*//[!/]\h*```($|should_panic|no_run|ignore|allow_fail|rust|test_harness|compile_fail|E\d{4}|edition201[58]) ^\h*//[!/]\h*```$ regions
     39 add-highlighter shared/rust/line_doctest/marker region ``` $ group
     40 add-highlighter shared/rust/line_doctest/marker/fence regex ``` 0:meta
     41 add-highlighter shared/rust/line_doctest/marker/keywords regex [\d\w] 0:meta # already matched above, just ignore comma
     42 add-highlighter shared/rust/line_doctest/inner region '^\h*//[!/]( #(?= ))?' '$| ' group
     43 add-highlighter shared/rust/line_doctest/inner/comment regex //[!/] 0:documentation
     44 add-highlighter shared/rust/line_doctest/inner/hidden regex '#' 0:meta
     45 add-highlighter shared/rust/line_doctest/code default-region ref rust
     46 add-highlighter shared/rust/line_code_rest   region ^\h*//[!/]\h*``` ^\h*//[!/]\h*```$      fill documentation # reset invalid doctest
     47 add-highlighter shared/rust/line_comment2    region //[!/]{2} $                             fill comment
     48 add-highlighter shared/rust/line_doc         region //[!/] $                                fill documentation
     49 add-highlighter shared/rust/line_comment1    region // $                                    group
     50 add-highlighter shared/rust/line_comment1/comment fill comment
     51 add-highlighter shared/rust/line_comment1/todo regex (TODO|NOTE|FIXME): 1:meta
     52 
     53 add-highlighter shared/rust/block_comment2   region -recurse /\*\*\* /\*\*\* \*/            fill comment
     54 add-highlighter shared/rust/block_doc        region -recurse /\*\* /\*\* \*/ regions
     55 add-highlighter shared/rust/block_doc/doctest region ```($|should_panic|no_run|ignore|allow_fail|rust|test_harness|compile_fail|E\d{4}|edition201[58]) ```$ regions
     56 add-highlighter shared/rust/block_doc/doctest/marker region ``` $ group
     57 add-highlighter shared/rust/block_doc/doctest/marker/fence regex ``` 0:meta
     58 add-highlighter shared/rust/block_doc/doctest/marker/keywords regex [\d\w] 0:meta # already matched above, just ignore comma
     59 add-highlighter shared/rust/block_doc/doctest/inner default-region group
     60 add-highlighter shared/rust/block_doc/doctest/inner/hidden regex '^\h*\**\h*#' 0:meta
     61 add-highlighter shared/rust/block_doc/doctest/inner/comment regex ^\h*\* 0:documentation
     62 add-highlighter shared/rust/block_doc/doctest/inner/code ref rust
     63 add-highlighter shared/rust/block_doc/code_rest region ``` ``` fill documentation
     64 add-highlighter shared/rust/block_doc/doc    default-region fill documentation
     65 add-highlighter shared/rust/block_comment1   region -recurse /\* /\* \*/ group
     66 add-highlighter shared/rust/block_comment1/comment fill comment
     67 add-highlighter shared/rust/block_comment1/todo regex (TODO|NOTE|FIXME): 1:meta
     68 
     69 add-highlighter shared/rust/macro_attributes region -recurse "\[" "#!?\[" "\]" regions
     70 add-highlighter shared/rust/macro_attributes/ default-region fill meta
     71 add-highlighter shared/rust/macro_attributes/string region %{(?<!')"} (?<!\\)(\\\\)*" fill string
     72 add-highlighter shared/rust/macro_attributes/raw_string region -match-capture %{(?<!')r(#*)"} %{"(#*)} fill string
     73 
     74 add-highlighter shared/rust/code/operators_arithmetic   regex (\+|-|/|\*|=|\^|&|\||!|>|<|%)=? 0:operator
     75 add-highlighter shared/rust/code/operators_as           regex \bas\b 0:operator
     76 add-highlighter shared/rust/code/ref_ref                regex (&\h+[&~@*])[^)=\s\t\r\n] 1:type
     77 add-highlighter shared/rust/code/ref                    regex ([&~@*])[^)=\s\t\r\n] 1:type
     78 add-highlighter shared/rust/code/operators_logic        regex &&|\|\| 0:operator
     79 
     80 add-highlighter shared/rust/code/lifetime_or_loop_label regex ('([a-zA-Z]\w+|_\w+))\b 1:meta
     81 add-highlighter shared/rust/code/namespace              regex \b[a-zA-Z](\w+)?(\h+)?(?=::) 0:module
     82 add-highlighter shared/rust/code/mod_path_sep           regex :: 0:meta
     83 add-highlighter shared/rust/code/question_mark          regex \? 0:meta
     84 # the language keywords are defined here, but many of   them are reserved and unused yet:
     85 # https://doc.rust-lang.org/reference/keywords.html
     86 add-highlighter shared/rust/code/function_call          regex _?[a-zA-Z]\w*\s*(?=\() 0:function
     87 add-highlighter shared/rust/code/generic_function_call  regex _?[a-zA-Z]\w*\s*(?=::<) 0:function
     88 add-highlighter shared/rust/code/function_declaration   regex (?:fn\h+)(_?\w+)(?:<[^>]+?>)?\( 1:function
     89 add-highlighter shared/rust/code/keywords               regex \b(?:as|break|continue|crate|else|enum|extern|false|fn|for|if|impl|in|let|loop|match|mod|pub|return|self|Self|struct|super|trait|true|type|union|unsafe|use|where|while|async|await|dyn|abstract|become|box|do|try)\b 0:keyword
     90 add-highlighter shared/rust/code/storage                regex \b(move|mut|ref|static|const)\b 0:type
     91 add-highlighter shared/rust/code/pub_with_scope         regex \b(pub)\h*(\()\h*(crate|super|self|in\h+[\w:]+)\h*(\)) 1:keyword 2:meta 4:meta
     92 # after let can be an arbitrary pattern match
     93 add-highlighter shared/rust/code/macro                  regex \b\w+! 0:meta
     94 # the number literals syntax is defined here:
     95 # https://doc.rust-lang.org/reference/tokens.html#numb  ers
     96 add-highlighter shared/rust/code/values                 regex \b(?:self|true|false|[0-9][_0-9]*(?:\.[0-9][_0-9]*|(?:\.[0-9][_0-9]*)?E[\+\-][_0-9]+)(?:f(?:32|64))?|(?:0x[_0-9a-fA-F]+|0o[_0-7]+|0b[_01]+|[0-9][_0-9]*)(?:(?:i|u|f)(?:8|16|32|64|128|size))?)\b 0:value
     97 add-highlighter shared/rust/code/char_character         regex %{'([^\\]|\\(.|x[0-9a-fA-F]{2}|u\{[0-9a-fA-F]{1,6}\}))'} 0:value
     98 # TODO highlight error for unicode or single escape by  te character
     99 add-highlighter shared/rust/code/byte_character         regex %{b'([\x00-\x5B\x5D-\x7F]|\\(.|x[0-9a-fA-F]{2}))'} 0:value
    100 add-highlighter shared/rust/code/builtin_types          regex \b(?:u8|u16|u32|u64|u128|usize|i8|i16|i32|i64|i128|isize|f32|f64|bool|char|str|Self)\b 0:type
    101 
    102 add-highlighter shared/rust/code/enum                   regex \b(Option|Result)\b 0:type
    103 add-highlighter shared/rust/code/enum_variant           regex \b(Some|None|Ok|Err)\b 0:value
    104 add-highlighter shared/rust/code/std_traits             regex \b(Copy|Send|Sized|Sync|Drop|Fn|FnMut|FnOnce|Box|ToOwned|Clone|PartialEq|PartialOrd|Eq|Ord|AsRef|AsMut|Into|From|Default|Iterator|Extend|IntoIterator|DoubleEndedIterator|ExactSizeIterator|SliceConcatExt|String|ToString|Vec)\b 0:type
    105  
    106 # Commands
    107 # ‾‾‾‾‾‾‾‾
    108 
    109 define-command -hidden rust-trim-indent %{
    110     # remove trailing white spaces
    111     try %{ execute-keys -draft -itersel x s \h+$ <ret> d }
    112 }
    113 
    114 define-command -hidden rust-indent-on-new-line %~
    115     evaluate-commands -draft -itersel %@
    116         try %{
    117             try %[ # line comment
    118                 evaluate-commands -draft -save-regs '/"' %[
    119                     # copy the commenting prefix
    120                     execute-keys -save-regs '' k x s ^\h*//[!/]{0,2}\h* <ret> y
    121                     try %[
    122                         # if the previous comment isn't empty, create a new one
    123                         execute-keys x<a-K>^\h*//[!/]{0,2}$<ret> jxs^\h*<ret>P
    124                     ] catch %[
    125                         # TODO figure out a way to not delete empty comment in current line
    126                         # if there is no space and text in the previous comment, remove it completely
    127                         execute-keys s //.*<ret> d
    128                     ]
    129                 ]
    130             ] catch %[ # block comment
    131                 # if the previous line isn't within a comment scope, break
    132                 execute-keys -draft kx <a-k>^(\h*/\*|\h+\*(?!/))<ret>
    133 
    134                 # find comment opening, validate it was not closed, and check its using star prefixes
    135                 execute-keys -draft <a-?>/\*<ret><a-H> <a-K>\*/<ret> <a-k>\A\h*/\*(\N*\n\h*\*)*\N*\n\h*.\z<ret>
    136 
    137                 try %[
    138                     # if the previous line is opening the comment, insert star preceeded by space
    139                     execute-keys -draft kx<a-k>^\h*/\*<ret>
    140                     execute-keys -draft i*<space><esc>
    141                 ] catch %[
    142                     try %[
    143                         # if the next line is a comment line insert a star
    144                         execute-keys -draft jx<a-k>^\h+\*<ret>
    145                         execute-keys -draft i*<space><esc>
    146                     ] catch %[
    147                         try %[
    148                             # if the previous line is an empty comment line, close the comment scope
    149                             execute-keys -draft kx<a-k>^\h+\*\h+$<ret> x1s\*(\h*)<ret>c/<esc>
    150                         ] catch %[
    151                             # if the previous line is a non-empty comment line, add a star
    152                             execute-keys -draft i*<space><esc>
    153                         ]
    154                     ]
    155                 ]
    156 
    157                 # trim trailing whitespace on the previous line
    158                 try %[ execute-keys -draft s\h+$<ret> d ]
    159                 # align the new star with the previous one
    160                 execute-keys Kx1s^[^*]*(\*)<ret>&
    161             ]
    162         } catch %`
    163             # re-indent previous line if it starts with where to match previous block
    164             # string literal parsing within extern does not handle escape
    165             try %% execute-keys -draft k x <a-k> ^\h*where\b <ret> hh <a-?> ^\h*\b(impl|((|pub\ |pub\((crate|self|super|in\ (::)?([a-zA-Z][a-zA-Z0-9_]*|_[a-zA-Z0-9_]+)(::[a-zA-Z][a-zA-Z0-9_]*|_[a-zA-Z0-9_]+)*)\)\ )((async\ |const\ )?(unsafe\ )?(extern\ ("[^"]*"\ )?)?fn|struct|enum|union)))\b <ret> <a-S> 1<a-&> %
    166             # preserve previous line indent
    167             try %{ execute-keys -draft <semicolon> K <a-&> }
    168             # indent after lines ending with [{([].+ and move first parameter to own line
    169             try %< execute-keys -draft [c[({[],[)}\]] <ret> <a-k> \A[({[]\N+\n\N*\n?\z <ret> L i<ret><esc> <gt> <a-S> <a-&> >
    170             # indent after non-empty lines not starting with operator and not ending with , or ; or {
    171             # XXX simplify this into a single <a-k> without s
    172             try %< execute-keys -draft k x s [^\h].+ <ret> <a-K> \A[-+*/&|^})<gt><lt>#] <ret> <a-K> [,<semicolon>{](\h*/[/*].*|)$ <ret> j <a-gt> >
    173             # indent after lines ending with {
    174             try %+ execute-keys -draft k x <a-k> \{$ <ret> j <a-gt> +
    175             # dedent after lines starting with . and ending with } or ) or , or ; or .await (} or ) or .await maybe with ?)
    176             try %_ execute-keys -draft k x <a-k> ^\h*\. <ret> <a-k> ([,<semicolon>]|(([})]|\.await)\?*))\h*$ <ret> j <a-lt> _
    177             # dedent after lines ending with " => {}" - part of empty match
    178             try %# execute-keys -draft k x <a-k> \ =>\ \{\}\h*$ <ret> j <a-lt> #
    179             # align to opening curly brace or paren when newline is inserted before a single closing
    180             try %< execute-keys -draft <a-h> <a-k> ^\h*[)}] <ret> h m <a-S> 1<a-&> >
    181             # todo dedent additional unmatched parenthesis
    182             # try %& execute-keys -draft k x s \((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\) l Gl s\) %sh{
    183                 # count previous selections length
    184                 # printf "j $(echo $kak_selections_length | wc -w) <a-lt>"
    185             # } &
    186         `
    187         # filter previous line
    188         try %{ execute-keys -draft k : rust-trim-indent <ret> }
    189     @
    190 ~
    191 
    192 define-command -hidden rust-indent-on-opening-curly-brace %[
    193     evaluate-commands -draft -itersel %~
    194         # align indent with opening paren when { is entered on a new line after the closing paren
    195         try %[ execute-keys -draft h <a-F> ) M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ]
    196         # dedent standalone { after impl and related block without any { in between
    197         try %@ execute-keys -draft hh <a-?> ^\h*\b(impl|((|pub\ |pub\((crate|self|super|in\ (::)?([a-zA-Z][a-zA-Z0-9_]*|_[a-zA-Z0-9_]+)(::[a-zA-Z][a-zA-Z0-9_]*|_[a-zA-Z0-9_]+)*)\)\ )((async\ |const\ )?(unsafe\ )?(extern\ ("[^"]*"\ )?)?fn|struct|enum|union))|if|for)\b <ret> <a-K> \{ <ret> <a-semicolon> <semicolon> ll x <a-k> ^\h*\{$ <ret> <a-lt> @
    198     ~
    199 ]
    200 
    201 define-command -hidden rust-indent-on-closing %~
    202     evaluate-commands -draft -itersel %_
    203         # align to opening curly brace or paren when alone on a line
    204         try %< execute-keys -draft <a-h> <a-k> ^\h*[)}\]]$ <ret> h m <a-S> 1<a-&> >
    205     _
    206 ~
    207 
    208 §