crystal.kak (13894B)
1 # Crystal 2 # https://crystal-lang.org 3 4 # Detection 5 # ‾‾‾‾‾‾‾‾‾ 6 7 hook global BufCreate '.*\.cr' %{ 8 set-option buffer filetype crystal 9 } 10 11 # Initialization 12 # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ 13 14 hook global WinSetOption filetype=crystal %{ 15 require-module crystal 16 17 add-highlighter window/crystal ref crystal 18 evaluate-commands set-option window static_words %opt{crystal_keywords} %opt{crystal_attributes} %opt{crystal_objects} 19 20 hook window ModeChange pop:insert:.* -group crystal-trim-indent crystal-trim-indent 21 hook window InsertChar .* -group crystal-indent crystal-indent-on-char 22 hook window InsertChar '\n' -group crystal-indent crystal-indent-on-new-line 23 hook window InsertChar '\n' -group crystal-insert crystal-insert-on-new-line 24 25 hook -always -once window WinSetOption filetype=.* %{ 26 remove-highlighter window/crystal 27 remove-hooks window crystal-.+ 28 } 29 } 30 31 provide-module crystal %§ 32 33 declare-option -hidden str-list crystal_keywords 'abstract' 'alias' 'annotation' 'as' 'asm' 'begin' 'break' 'case' 'class' 'def' 'do' 'else' 'elsif' 'end' 'ensure' 'enum' 'extend' 'false' 'for' 'fun' 'if' 'include' 'instance_sizeof' 'is_a?' 'lib' 'macro' 'module' 'next' 'nil' 'nil?' 'of' 'offsetof' 'out' 'pointerof' 'private' 'protected' 'require' 'rescue' 'responds_to?' 'return' 'select' 'self' 'sizeof' 'struct' 'super' 'then' 'true' 'type' 'typeof' 'uninitialized' 'union' 'unless' 'until' 'verbatim' 'when' 'while' 'with' 'yield' 34 # https://crystal-lang.org/reference/syntax_and_semantics/methods_and_instance_variables.html#getters-and-setters 35 declare-option -hidden str-list crystal_attributes 'getter' 'setter' 'property' 36 declare-option -hidden str-list crystal_operators '+' '-' '*' '/' '//' '%' '|' '&' '^' '~' '**' '<<' '<' '<=' '==' '!=' '=~' '!~' '>>' '>' '>=' '<=>' '===' '[]' '[]=' '[]?' '[' '&+' '&-' '&*' '&**' 37 declare-option -hidden str-list crystal_objects 'Adler32' 'ArgumentError' 'Array' 'Atomic' 'Base64' 'Benchmark' 'BigDecimal' 'BigFloat' 'BigInt' 'BigRational' 'BitArray' 'Bool' 'Box' 'Bytes' 'Channel' 'Char' 'Class' 'Colorize' 'Comparable' 'Complex' 'Concurrent' 'ConcurrentExecutionException' 'CRC32' 'Crypto' 'Crystal' 'CSV' 'Debug' 'Deprecated' 'Deque' 'Digest' 'Dir' 'DivisionByZeroError' 'DL' 'ECR' 'Enum' 'Enumerable' 'ENV' 'Errno' 'Exception' 'Fiber' 'File' 'FileUtils' 'Flags' 'Flate' 'Float' 'Float32' 'Float64' 'GC' 'Gzip' 'Hash' 'HTML' 'HTTP' 'Indexable' 'IndexError' 'INI' 'Int' 'Int128' 'Int16' 'Int32' 'Int64' 'Int8' 'InvalidBigDecimalException' 'InvalidByteSequenceError' 'IO' 'IPSocket' 'Iterable' 'Iterator' 'JSON' 'KeyError' 'Levenshtein' 'Link' 'LLVM' 'Logger' 'Markdown' 'Math' 'MIME' 'Mutex' 'NamedTuple' 'Nil' 'NilAssertionError' 'NotImplementedError' 'Number' 'OAuth' 'OAuth2' 'Object' 'OpenSSL' 'OptionParser' 'OverflowError' 'PartialComparable' 'Path' 'Pointer' 'PrettyPrint' 'Proc' 'Process' 'Random' 'Range' 'Readline' 'Reference' 'Reflect' 'Regex' 'SemanticVersion' 'Set' 'Signal' 'Slice' 'Socket' 'Spec' 'StaticArray' 'String' 'StringPool' 'StringScanner' 'Struct' 'Symbol' 'System' 'TCPServer' 'TCPSocket' 'Termios' 'Time' 'Tuple' 'TypeCastError' 'UDPSocket' 'UInt128' 'UInt16' 'UInt32' 'UInt64' 'UInt8' 'Unicode' 'Union' 'UNIXServer' 'UNIXSocket' 'URI' 'UUID' 'VaList' 'Value' 'WeakRef' 'XML' 'YAML' 'Zip' 'Zlib' 38 39 # Highlighters 40 # ‾‾‾‾‾‾‾‾‾‾‾‾ 41 42 add-highlighter shared/crystal regions 43 add-highlighter shared/crystal/code default-region group 44 45 # Comments 46 # https://crystal-lang.org/reference/syntax_and_semantics/comments.html 47 # Avoid string literals with interpolation 48 add-highlighter shared/crystal/comment region '#(?!\{)' '$' fill comment 49 50 # String 51 # https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html 52 add-highlighter shared/crystal/string region '"' '(?<!\\)(\\\\)*"' regions 53 54 # Percent string literals 55 # https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html#percent-string-literals 56 add-highlighter shared/crystal/parenthesis-string region -recurse '\(' '%Q?\(' '\)' regions 57 add-highlighter shared/crystal/bracket-string region -recurse '\[' '%Q?\[' '\]' regions 58 add-highlighter shared/crystal/brace-string region -recurse '\{' '%Q?\{' '\}' regions 59 add-highlighter shared/crystal/angle-string region -recurse '<' '%Q?<' '>' regions 60 add-highlighter shared/crystal/pipe-string region '%Q?\|' '\|' regions 61 62 # Raw 63 # https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html#percent-string-literals 64 # https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html#percent-string-array-literal 65 # https://crystal-lang.org/reference/syntax_and_semantics/literals/symbol.html#percent-symbol-array-literal 66 add-highlighter shared/crystal/raw-parenthesis-string region -recurse '\(' '%[qwi]\(' '\)' fill string 67 add-highlighter shared/crystal/raw-bracket-string region -recurse '\[' '%[qwi]\[' '\]' fill string 68 add-highlighter shared/crystal/raw-brace-string region -recurse '\{' '%[qwi]\{' '\}' fill string 69 add-highlighter shared/crystal/raw-angle-string region -recurse '<' '%[qwi]<' '>' fill string 70 add-highlighter shared/crystal/raw-pipe-string region '%[qwi]\|' '\|' fill string 71 72 # Here document 73 # https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html#heredoc 74 add-highlighter shared/crystal/heredoc region -match-capture '<<-(\w+)' '^\h*(\w+)$' regions 75 # Raw 76 add-highlighter shared/crystal/raw-heredoc region -match-capture "<<-'(\w+)'" '^\h*(\w+)$' regions 77 add-highlighter shared/crystal/raw-heredoc/fill default-region fill string 78 add-highlighter shared/crystal/raw-heredoc/interpolation region -recurse '\{' '#\{' '\}' fill meta 79 80 # Symbol 81 # https://crystal-lang.org/reference/syntax_and_semantics/literals/symbol.html 82 add-highlighter shared/crystal/quoted-symbol region ':"' '(?<!\\)(\\\\)*"' fill value 83 84 # Regular expressions 85 # https://crystal-lang.org/reference/syntax_and_semantics/literals/regex.html 86 # https://crystal-lang.org/reference/syntax_and_semantics/literals/regex.html#modifiers 87 add-highlighter shared/crystal/regex region '/' '(?<!\\)(\\\\)*/[imx]*' regions 88 # Avoid unterminated regular expression 89 add-highlighter shared/crystal/division region ' / ' '.\K' group 90 91 # Percent regex literals 92 # https://crystal-lang.org/reference/syntax_and_semantics/literals/regex.html#percent-regex-literals 93 add-highlighter shared/crystal/parenthesis-regex region -recurse '\(' '%r?\(' '\)[imx]*' regions 94 add-highlighter shared/crystal/bracket-regex region -recurse '\[' '%r?\[' '\][imx]*' regions 95 add-highlighter shared/crystal/brace-regex region -recurse '\{' '%r?\{' '\}[imx]*' regions 96 add-highlighter shared/crystal/angle-regex region -recurse '<' '%r?<' '>[imx]*' regions 97 add-highlighter shared/crystal/pipe-regex region '%r?\|' '\|[imx]*' regions 98 99 # Command 100 # https://crystal-lang.org/reference/syntax_and_semantics/literals/command.html 101 add-highlighter shared/crystal/command region '`' '(?<!\\)(\\\\)*`' regions 102 103 # Percent command literals 104 add-highlighter shared/crystal/parenthesis-command region -recurse '\(' '%x?\(' '\)' regions 105 add-highlighter shared/crystal/bracket-command region -recurse '\[' '%x?\[' '\]' regions 106 add-highlighter shared/crystal/brace-command region -recurse '\{' '%x?\{' '\}' regions 107 add-highlighter shared/crystal/angle-command region -recurse '<' '%x?<' '>' regions 108 add-highlighter shared/crystal/pipe-command region '%x?\|' '\|' regions 109 110 evaluate-commands %sh[ 111 # Keywords 112 eval "set -- $kak_quoted_opt_crystal_keywords" 113 regex="\\b(?:\\Q$1\\E" 114 shift 115 for keyword do 116 regex="$regex|\\Q$keyword\\E" 117 done 118 regex="$regex)\\b" 119 printf 'add-highlighter shared/crystal/code/keywords regex %s 0:keyword\n' "$regex" 120 121 # Attributes 122 eval "set -- $kak_quoted_opt_crystal_attributes" 123 regex="\\b(?:\\Q$1\\E" 124 shift 125 for attribute do 126 regex="$regex|\\Q$attribute\\E" 127 done 128 regex="$regex)\\b" 129 printf 'add-highlighter shared/crystal/code/attributes regex %s 0:attribute\n' "$regex" 130 131 # Symbols 132 eval "set -- $kak_quoted_opt_crystal_operators" 133 # Avoid to match modules 134 regex="(?<!:):(?:\\w+[?!]?" 135 for operator do 136 regex="$regex|\\Q$operator\\E" 137 done 138 regex="$regex)" 139 printf 'add-highlighter shared/crystal/code/symbols regex %%(%s) 0:value\n' "$regex" 140 141 # Objects 142 eval "set -- $kak_quoted_opt_crystal_objects" 143 regex="\\b(?:\\Q$1\\E" 144 shift 145 for object do 146 regex="$regex|\\Q$object\\E" 147 done 148 regex="$regex)\\b" 149 printf 'add-highlighter shared/crystal/code/objects regex %s 0:builtin\n' "$regex" 150 151 # Interpolation 152 # String 153 # https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html#interpolation 154 for id in string parenthesis-string bracket-string brace-string angle-string pipe-string heredoc; do 155 printf " 156 add-highlighter shared/crystal/$id/fill default-region fill string 157 add-highlighter shared/crystal/$id/interpolation region -recurse '\\{' '#\\{' '\\}' ref crystal 158 " 159 done 160 161 # Regular expressions 162 # https://crystal-lang.org/reference/syntax_and_semantics/literals/regex.html#interpolation 163 for id in regex parenthesis-regex bracket-regex brace-regex angle-regex pipe-regex; do 164 printf " 165 add-highlighter shared/crystal/$id/fill default-region fill meta 166 add-highlighter shared/crystal/$id/interpolation region -recurse '\\{' '#\\{' '\\}' ref crystal 167 " 168 done 169 170 # Command 171 for id in command parenthesis-command bracket-command brace-command angle-command pipe-command; do 172 printf " 173 add-highlighter shared/crystal/$id/fill default-region fill meta 174 add-highlighter shared/crystal/$id/interpolation region -recurse '\\{' '#\\{' '\\}' ref crystal 175 " 176 done 177 ] 178 179 # Commands 180 # ‾‾‾‾‾‾‾‾ 181 182 define-command -hidden crystal-trim-indent %{ 183 evaluate-commands -no-hooks -draft -itersel %{ 184 execute-keys x 185 # remove trailing white spaces 186 try %{ execute-keys -draft s \h+$ <ret> d } 187 } 188 } 189 190 define-command -hidden crystal-indent-on-char %{ 191 evaluate-commands -no-hooks -draft -itersel %{ 192 # align 'else' to 'if/case' 193 try %{ execute-keys -draft x <a-k> ^\h*else$ <ret> <a-a>i <a-semicolon> <a-?> ^\h*(?:if|case) <ret> <a-S> 1<a-&> } 194 # align 'elsif' to 'if' 195 try %{ execute-keys -draft x <a-k> ^\h*elsif$ <ret> <a-a>i <a-semicolon> <a-?> ^\h*(?:if) <ret> <a-S> 1<a-&> } 196 # align 'when' to 'case' 197 try %{ execute-keys -draft x <a-k> ^\h*when$ <ret> <a-a>i <a-semicolon> <a-?> ^\h*(?:case) <ret> <a-S> 1<a-&> } 198 # align 'rescue' to 'begin/def' 199 try %{ execute-keys -draft x <a-k> ^\h*rescue$ <ret> <a-a>i <a-semicolon> <a-?> ^\h*(?:begin|def) <ret> <a-S> 1<a-&> } 200 # align 'end' to opening structure 201 try %{ execute-keys -draft x <a-k> ^\h*end$ <ret> <a-a>i <a-semicolon> <a-?> ^\h*(?:begin|case|class|def|for|if|module|unless|until|while) <ret> <a-S> 1<a-&> } 202 } 203 } 204 205 define-command -hidden crystal-indent-on-new-line %{ 206 evaluate-commands -no-hooks -draft -itersel %{ 207 # Copy previous line indent 208 try %{ execute-keys -draft K <a-&> } 209 # Remove previous line's trailing spaces 210 try %{ execute-keys -draft k :crystal-trim-indent <ret> } 211 # Indent after start structure/opening statement 212 try %{ execute-keys -draft k x <a-k> ^\h*(?:begin|case|class|def|else|elsif|ensure|for|if|module|rescue|unless|until|when|while|.+\bdo$|.+\bdo\h\|.+(?=\|))[^0-9A-Za-z_!?] <ret> j <a-gt> } 213 } 214 } 215 216 define-command -hidden crystal-insert-on-new-line %[ 217 evaluate-commands -no-hooks -draft -itersel %[ 218 # copy _#_ comment prefix and following white spaces 219 try %{ execute-keys -draft k x s '^\h*\K#\h*' <ret> y j x<semicolon> P } 220 # wisely add end structure 221 evaluate-commands -save-regs x %[ 222 try %{ execute-keys -draft k x s ^ \h + <ret> \" x y } catch %{ reg x '' } 223 try %[ 224 evaluate-commands -draft %[ 225 # Check if previous line opens a block 226 execute-keys -draft kx <a-k>^<c-r>x(?:begin|case|class|def|for|if|module|unless|until|while|.+\bdo$|.+\bdo\h\|.+(?=\|))[^0-9A-Za-z_!?]<ret> 227 # Check that we do not already have an end for this indent level which is first set via `crystal-indent-on-new-line` hook 228 execute-keys -draft }i J x <a-K> ^<c-r>x(?:end|else|elsif|rescue|when)[^0-9A-Za-z_!?]<ret> 229 ] 230 execute-keys -draft o<c-r>xend<esc> # insert a new line with containing end 231 ] 232 ] 233 ] 234 ] 235 236 define-command -hidden crystal-fetch-keywords %{ 237 set-register dquote %sh{ 238 curl --location https://github.com/crystal-lang/crystal/raw/master/src/compiler/crystal/syntax/lexer.cr | 239 kak -f '%1scheck_ident_or_keyword\(:(\w+\??), \w+\)<ret>y%<a-R>a<ret><esc><a-_>a<del><esc>|sort<ret>' 240 } 241 } 242 243 define-command -hidden crystal-fetch-operators %{ 244 set-register dquote %sh{ 245 curl --location https://github.com/crystal-lang/crystal/raw/master/src/compiler/crystal/syntax/parser.cr | 246 kak -f '/AtomicWithMethodCheck =<ret>x1s:"([^"]+)"<ret>y%<a-R>i''<esc>a''<ret><esc><a-_>a<del><esc>' 247 } 248 } 249 250 define-command -hidden crystal-fetch-objects %{ 251 set-register dquote %sh{ 252 curl --location https://crystal-lang.org/api/ | 253 # Remove Top Level Namespace 254 kak -f '%1sdata-id="github.com/crystal-lang/crystal/(\w+)"<ret>)<a-,>y%<a-R>a<ret><esc><a-_>a<del><esc>' 255 } 256 } 257 258 §