ron.kak (8286B)
1 # http://doc.rs/ron 2 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 3 4 # Detection 5 # ‾‾‾‾‾‾‾‾‾ 6 7 hook global BufCreate .*[.](ron) %{ 8 set-option buffer filetype ron 9 } 10 11 # Initialization 12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 13 14 hook global WinSetOption filetype=ron %{ 15 require-module ron 16 17 hook window ModeChange pop:insert:.* -group ron-trim-indent ron-trim-indent 18 hook window InsertChar \n -group ron-indent ron-indent-on-new-line 19 hook window InsertChar \{ -group ron-indent ron-indent-on-opening-curly-brace 20 hook window InsertChar [)}\]] -group ron-indent ron-indent-on-closing 21 22 hook -once -always window WinSetOption filetype=.* %{ remove-hooks window ron-.+ } 23 } 24 25 hook -group ron-highlight global WinSetOption filetype=ron %{ 26 add-highlighter window/ron ref ron 27 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/ron } 28 } 29 30 provide-module ron %§ 31 32 # Highlighters 33 # ‾‾‾‾‾‾‾‾‾‾‾‾ 34 35 add-highlighter shared/ron regions 36 add-highlighter shared/ron/code default-region group 37 add-highlighter shared/ron/string region %{(?<!')"} (?<!\\)(\\\\)*" fill string 38 add-highlighter shared/ron/raw_string region -match-capture %{(?<!')r(#*)"} %{"(#*)} fill string 39 40 add-highlighter shared/ron/line_comment1 region // $ group 41 add-highlighter shared/ron/line_comment1/comment fill comment 42 add-highlighter shared/ron/line_comment1/todo regex (TODO|NOTE|FIXME): 1:meta 43 add-highlighter shared/ron/line_comment2 region //[!/]{2} $ fill comment 44 45 add-highlighter shared/ron/block_comment1 region -recurse /\* /\* \*/ group 46 add-highlighter shared/ron/block_comment1/comment fill comment 47 add-highlighter shared/ron/block_comment1/todo regex (TODO|NOTE|FIXME): 1:meta 48 49 add-highlighter shared/ron/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 50 51 add-highlighter shared/ron/code/enum_variant regex \b(Some|None|Ok|Err)\b 0:value 52 53 add-highlighter shared/ron/code/ regex ^(\s*[^,:\[\]\{\}\s]+): 1:variable 54 add-highlighter shared/ron/code/ regex ,(\s*[^,:\[\]\{\}\s]+): 1:variable 55 add-highlighter shared/ron/code/ regex \{(\s*[^,:\[\]\{\}\s]+): 1:variable 56 add-highlighter shared/ron/code/ regex \((\s*[^,:\[\]\{\}\s]+): 1:variable 57 58 # Commands 59 # ‾‾‾‾‾‾‾‾ 60 61 define-command -hidden ron-trim-indent %{ 62 # remove trailing white spaces 63 try %{ execute-keys -draft -itersel x s \h+$ <ret> d } 64 } 65 66 define-command -hidden ron-indent-on-new-line %~ 67 evaluate-commands -draft -itersel %@ 68 try %{ 69 try %[ # line comment 70 evaluate-commands -draft -save-regs '/"' %[ 71 # copy the commenting prefix 72 execute-keys -save-regs '' k x s ^\h*//[!/]{0,2}\h* <ret> y 73 try %[ 74 # if the previous comment isn't empty, create a new one 75 execute-keys x<a-K>^\h*//[!/]{0,2}$<ret> jxs^\h*<ret>P 76 ] catch %[ 77 # TODO figure out a way to not delete empty comment in current line 78 # if there is no space and text in the previous comment, remove it completely 79 execute-keys s //.*<ret> d 80 ] 81 ] 82 ] catch %[ # block comment 83 # if the previous line isn't within a comment scope, break 84 execute-keys -draft kx <a-k>^(\h*/\*|\h+\*(?!/))<ret> 85 86 # find comment opening, validate it was not closed, and check its using star prefixes 87 execute-keys -draft <a-?>/\*<ret><a-H> <a-K>\*/<ret> <a-k>\A\h*/\*(\N*\n\h*\*)*\N*\n\h*.\z<ret> 88 89 try %[ 90 # if the previous line is opening the comment, insert star preceeded by space 91 execute-keys -draft kx<a-k>^\h*/\*<ret> 92 execute-keys -draft i*<space><esc> 93 ] catch %[ 94 try %[ 95 # if the next line is a comment line insert a star 96 execute-keys -draft jx<a-k>^\h+\*<ret> 97 execute-keys -draft i*<space><esc> 98 ] catch %[ 99 try %[ 100 # if the previous line is an empty comment line, close the comment scope 101 execute-keys -draft kx<a-k>^\h+\*\h+$<ret> x1s\*(\h*)<ret>c/<esc> 102 ] catch %[ 103 # if the previous line is a non-empty comment line, add a star 104 execute-keys -draft i*<space><esc> 105 ] 106 ] 107 ] 108 109 # trim trailing whitespace on the previous line 110 try %[ execute-keys -draft s\h+$<ret> d ] 111 # align the new star with the previous one 112 execute-keys Kx1s^[^*]*(\*)<ret>& 113 ] 114 } catch %` 115 # re-indent previous line if it starts with where to match previous block 116 # string literal parsing within extern does not handle escape 117 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-&> % 118 # preserve previous line indent 119 try %{ execute-keys -draft <semicolon> K <a-&> } 120 # indent after lines ending with [{([].+ and move first parameter to own line 121 try %< execute-keys -draft [c[({[],[)}\]] <ret> <a-k> \A[({[]\N+\n\N*\n?\z <ret> L i<ret><esc> <gt> <a-S> <a-&> > 122 # indent after non-empty lines not starting with operator and not ending with , or ; or { 123 # XXX simplify this into a single <a-k> without s 124 try %< execute-keys -draft k x s [^\h].+ <ret> <a-K> \A[-+*/&|^})<gt><lt>#] <ret> <a-K> [,<semicolon>{](\h*/[/*].*|)$ <ret> j <a-gt> > 125 # indent after lines ending with { 126 try %+ execute-keys -draft k x <a-k> \{$ <ret> j <a-gt> + 127 # dedent after lines starting with . and ending with } or ) or , or ; or .await (} or ) or .await maybe with ?) 128 try %_ execute-keys -draft k x <a-k> ^\h*\. <ret> <a-k> ([,<semicolon>]|(([})]|\.await)\?*))\h*$ <ret> j <a-lt> _ 129 # dedent after lines ending with " => {}" - part of empty match 130 try %# execute-keys -draft k x <a-k> \ =>\ \{\}\h*$ <ret> j <a-lt> # 131 # align to opening curly brace or paren when newline is inserted before a single closing 132 try %< execute-keys -draft <a-h> <a-k> ^\h*[)}] <ret> h m <a-S> 1<a-&> > 133 # todo dedent additional unmatched parenthesis 134 # try %& execute-keys -draft k x s \((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\) l Gl s\) %sh{ 135 # count previous selections length 136 # printf "j $(echo $kak_selections_length | wc -w) <a-lt>" 137 # } & 138 ` 139 # filter previous line 140 try %{ execute-keys -draft k : ron-trim-indent <ret> } 141 @ 142 ~ 143 144 define-command -hidden ron-indent-on-opening-curly-brace %[ 145 evaluate-commands -draft -itersel %~ 146 # align indent with opening paren when { is entered on a new line after the closing paren 147 try %[ execute-keys -draft h <a-F> ) M <a-k> \A\(.*\)\h*\n\h*\{\z <ret> s \A|.\z <ret> 1<a-&> ] 148 # dedent standalone { after impl and related block without any { in between 149 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> @ 150 ~ 151 ] 152 153 define-command -hidden ron-indent-on-closing %~ 154 evaluate-commands -draft -itersel %_ 155 # align to opening curly brace or paren when alone on a line 156 try %< execute-keys -draft <a-h> <a-k> ^\h*[)}\]]$ <ret> h m <a-S> 1<a-&> > 157 _ 158 ~ 159 §