Merge remote-tracking branch 'johnisom/crystal-filetype'

This commit is contained in:
Maxime Coste 2020-08-03 18:57:45 +10:00
commit ba7831035f

View File

@ -1,28 +1,43 @@
# Crystal
# https://crystal-lang.org
# Detection
# ‾‾‾‾‾‾‾‾‾
hook global BufCreate '.*\.cr' %{
set-option buffer filetype crystal
}
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook global WinSetOption filetype=crystal %{
require-module crystal
evaluate-commands set-option window static_words %opt{crystal_keywords} %opt{crystal_attributes} %opt{crystal_objects}
add-highlighter window/crystal ref crystal
hook -group crystal window InsertChar '\n' crystal-new-line-inserted
evaluate-commands set-option window static_words %opt{crystal_keywords} %opt{crystal_attributes} %opt{crystal_objects}
hook window InsertChar .* -group crystal-indent crystal-indent-on-char
hook window InsertChar '\n' -group crystal-indent crystal-indent-on-new-line
hook window InsertChar '\n' -group crystal-insert crystal-insert-on-new-line
hook -always -once window WinSetOption filetype=.* %{
remove-highlighter window/crystal
remove-hooks window crystal
remove-hooks window crystal-.+
}
}
provide-module crystal %🐈
provide-module crystal %§
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'
# https://crystal-lang.org/reference/syntax_and_semantics/methods_and_instance_variables.html#getters-and-setters
declare-option -hidden str-list crystal_attributes 'getter' 'setter' 'property'
declare-option -hidden str-list crystal_operators '+' '-' '*' '/' '//' '%' '|' '&' '^' '~' '**' '<<' '<' '<=' '==' '!=' '=~' '!~' '>>' '>' '>=' '<=>' '===' '[]' '[]=' '[]?' '[' '&+' '&-' '&*' '&**'
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'
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
add-highlighter shared/crystal regions
add-highlighter shared/crystal/code default-region group
@ -42,6 +57,7 @@ provide-module crystal %🐈
add-highlighter shared/crystal/brace-string region -recurse '\{' '%Q?\{' '\}' regions
add-highlighter shared/crystal/angle-string region -recurse '<' '%Q?<' '>' regions
add-highlighter shared/crystal/pipe-string region '%Q?\|' '\|' regions
# Raw
# https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html#percent-string-literals
# https://crystal-lang.org/reference/syntax_and_semantics/literals/string.html#percent-string-array-literal
@ -159,17 +175,63 @@ provide-module crystal %🐈
done
]
define-command -hidden crystal-new-line-inserted %{
# Commands
# ‾‾‾‾‾‾‾‾
define-command -hidden crystal-trim-indent %{
evaluate-commands -no-hooks -draft -itersel %{
execute-keys <a-x>
# remove trailing white spaces
try %{ execute-keys -draft s \h+$ <ret> d }
}
}
define-command -hidden crystal-indent-on-char %{
evaluate-commands -no-hooks -draft -itersel %{
# align 'else' to 'if/case'
try %{ execute-keys -draft <a-x> <a-k> ^\h*else$ <ret> <a-a>i <a-semicolon> <a-?> ^\h*(?:if|case) <ret> <a-S> 1<a-&> }
# align 'elsif' to 'if'
try %{ execute-keys -draft <a-x> <a-k> ^\h*elsif$ <ret> <a-a>i <a-semicolon> <a-?> ^\h*(?:if) <ret> <a-S> 1<a-&> }
# align 'when' to 'case'
try %{ execute-keys -draft <a-x> <a-k> ^\h*when$ <ret> <a-a>i <a-semicolon> <a-?> ^\h*(?:case) <ret> <a-S> 1<a-&> }
# align 'rescue' to 'begin/def'
try %{ execute-keys -draft <a-x> <a-k> ^\h*rescue$ <ret> <a-a>i <a-semicolon> <a-?> ^\h*(?:begin|def) <ret> <a-S> 1<a-&> }
# align 'end' to opening structure
try %{ execute-keys -draft <a-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-&> }
}
}
define-command -hidden crystal-indent-on-new-line %{
evaluate-commands -no-hooks -draft -itersel %{
# Copy previous line indent
try %{
execute-keys -draft 'K<a-&>'
}
# Remove empty line indent
try %{
execute-keys -draft 'k<a-x>s^\h+$<ret>d'
try %{ execute-keys -draft K <a-&> }
# Remove previous line's trailing spaces
try %{ execute-keys -draft k :ruby-trim-indent <ret> }
# Indent after start structure/opening statement
try %{ execute-keys -draft k <a-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> }
}
}
define-command -hidden crystal-insert-on-new-line %[
evaluate-commands -no-hooks -draft -itersel %[
# copy _#_ comment prefix and following white spaces
try %{ execute-keys -draft k <a-x> s '^\h*\K#\h*' <ret> y j <a-x><semicolon> P }
# wisely add end structure
evaluate-commands -save-regs x %[
try %{ execute-keys -draft k <a-x> s ^ \h + <ret> \" x y } catch %{ reg x '' }
try %[
evaluate-commands -draft %[
# Check if previous line opens a block
execute-keys -draft k<a-x> <a-k>^<c-r>x(?:begin|case|class|def|for|if|module|unless|until|while|.+\bdo$|.+\bdo\h\|.+(?=\|))[^0-9A-Za-z_!?]<ret>
# Check that we do not already have an end for this indent level which is first set via `crystal-indent-on-new-line` hook
execute-keys -draft }i J <a-x> <a-K> ^<c-r>x(?:end|else|elsif|rescue|when)[^0-9A-Za-z_!?]<ret>
]
execute-keys -draft o<c-r>xend<esc> # insert a new line with containing end
]
]
]
]
define-command -hidden crystal-fetch-keywords %{
set-register dquote %sh{
curl --location https://github.com/crystal-lang/crystal/raw/master/src/compiler/crystal/syntax/lexer.cr |
@ -191,4 +253,5 @@ provide-module crystal %🐈
kak -f '%1sdata-id="github.com/crystal-lang/crystal/(\w+)"<ret>)<a-space>y%<a-R>a<ret><esc><a-_>a<del><esc>'
}
}
🐈
§