2014-07-03 15:16:59 +02:00
# http://ruby-lang.org
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
2016-01-21 18:33:03 +01:00
# require commenting.kak
2014-07-03 15:16:59 +02:00
# Detection
# ‾‾‾‾‾‾‾‾‾
hook global BufSetOption mimetype=text/x-ruby %{
set buffer filetype ruby
}
hook global BufCreate .*(([.](rb))|(irbrc)|(pryrc)|(Capfile|[.]cap)|(Gemfile)|(Guardfile)|(Rakefile|[.]rake)|(Thorfile|[.]thor)|(Vagrantfile)) %{
set buffer filetype ruby
}
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
2014-07-14 22:42:19 +02:00
addhl -group / regions -default code ruby \
2014-07-10 16:23:15 +02:00
double_string '"' (?<!\\)(\\\\)*" '' \
2016-02-27 03:34:47 +01:00
single_string "'" (?<!\\)(\\\\)*' '' \
2014-07-10 16:23:15 +02:00
backtick '`' (?<!\\)(\\\\)*` '' \
regex '/' (?<!\\)(\\\\)*/[imox]* '' \
comment '#' '$' '' \
comment ^begin= ^=end '' \
literal '%[iqrswxIQRSWX]\(' \) \( \
literal '%[iqrswxIQRSWX]\{' \} \{ \
literal '%[iqrswxIQRSWX]\[' \] \[ \
literal '%[iqrswxIQRSWX]<' > <
2014-07-03 15:16:59 +02:00
# Regular expression flags are: i → ignore case, m → multi-lines, o → only interpolate #{} blocks once, x → extended mode (ignore white spaces)
# Literals are: i → array of symbols, q → string, r → regular expression, s → symbol, w → array of words, x → capture shell result
addhl -group /ruby/double_string fill string
2014-07-14 22:42:19 +02:00
addhl -group /ruby/double_string regions regions interpolation \Q#{ \} \{
addhl -group /ruby/double_string/regions/interpolation fill meta
2014-07-03 15:16:59 +02:00
addhl -group /ruby/single_string fill string
2014-07-14 22:42:19 +02:00
2014-07-13 17:57:49 +02:00
addhl -group /ruby/backtick fill meta
2014-07-14 22:42:19 +02:00
addhl -group /ruby/backtick regions regions interpolation \Q#{ \} \{
addhl -group /ruby/backtick/regions/interpolation fill meta
2014-07-13 17:57:49 +02:00
addhl -group /ruby/regex fill meta
2014-07-14 22:42:19 +02:00
addhl -group /ruby/regex regions regions interpolation \Q#{ \} \{
addhl -group /ruby/regex/regions/interpolation fill meta
2014-07-03 15:16:59 +02:00
addhl -group /ruby/comment fill comment
2014-07-14 22:42:19 +02:00
2014-07-13 17:57:49 +02:00
addhl -group /ruby/literal fill meta
2014-07-03 15:16:59 +02:00
2016-02-27 17:53:16 +01:00
addhl -group /ruby/code regex \<([A-Za-z]\w*:)|([$@][A-Za-z]\w*)|(\W\K:[A-Za-z]\w*[=?!]?) 0:identifier
2014-07-03 15:16:59 +02:00
2016-03-14 13:08:51 +01:00
%sh{
# Grammar
2016-03-14 17:29:11 +01:00
# Keywords are collected searching for keywords at
2016-03-14 13:08:51 +01:00
# https://github.com/ruby/ruby/blob/trunk/parse.y
2016-03-14 17:29:11 +01:00
keywords="alias|and|begin|break|case|class|def|defined|do|else|elsif|end"
keywords="${keywords}|ensure|false|for|if|in|module|next|nil|not|or|redo"
keywords="${keywords}|rescue|retry|return|self|super|then|true|undef|unless|until|when|while|yield"
attributes="attr_reader|attr_writer|attr_accessor"
values="false|true|nil"
meta="require|include"
2016-03-14 13:08:51 +01:00
# Add the language's grammar to the static completion list
2016-03-17 14:30:14 +01:00
printf %s "hook global WinSetOption filetype=ruby %{
2016-03-14 13:08:51 +01:00
set window static_words '${keywords}'
set -add window static_words '${attributes}'
set -add window static_words '${values}'
set -add window static_words '${meta}'
2016-03-17 14:30:14 +01:00
}" | sed 's,|,:,g'
2016-03-14 13:08:51 +01:00
# Highlight keywords
2016-03-17 14:30:14 +01:00
printf %s "
2016-03-14 17:29:11 +01:00
addhl -group /ruby/code regex \<(${keywords})\> 0:keyword
addhl -group /ruby/code regex \<(${attributes})\> 0:attribute
addhl -group /ruby/code regex \<(${values})\> 0:value
addhl -group /ruby/code regex \<(${meta})\> 0:meta
2016-03-14 13:08:51 +01:00
"
}
2014-07-03 15:16:59 +02:00
# Commands
# ‾‾‾‾‾‾‾‾
2016-03-10 14:48:12 +01:00
def -hidden _ruby_filter_around_selections %{
eval -draft -itersel %{
exec <a-x>
# remove trailing white spaces
try %{ exec -draft s \h + $ <ret> d }
}
}
2016-01-21 19:19:17 +01:00
def -hidden _ruby_indent_on_char %{
eval -draft -itersel %{
# align middle and end structures to start
try %{ exec -draft <a-x> <a-k> ^ \h * (else|elsif) $ <ret> <a-\;> <a-?> ^ \h * (if) <ret> s \A | \Z <ret> \' <a-&> }
try %{ exec -draft <a-x> <a-k> ^ \h * (when) $ <ret> <a-\;> <a-?> ^ \h * (case) <ret> s \A | \Z <ret> \' <a-&> }
try %{ exec -draft <a-x> <a-k> ^ \h * (rescue) $ <ret> <a-\;> <a-?> ^ \h * (begin) <ret> s \A | \Z <ret> \' <a-&> }
try %{ exec -draft <a-x> <a-k> ^ \h * (end) $ <ret> <a-\;> <a-?> ^ \h * (begin|case|class|def|do|for|if|module|unless|until|while) <ret> s \A | \Z <ret> \' <a-&> }
}
}
def -hidden _ruby_indent_on_new_line %{
eval -draft -itersel %{
2014-07-03 15:16:59 +02:00
# preserve previous line indent
2016-01-21 19:19:17 +01:00
try %{ exec -draft K <a-&> }
2016-03-10 14:48:12 +01:00
# filter previous line
try %{ exec -draft k : _ruby_filter_around_selections <ret> }
2016-01-21 19:19:17 +01:00
# indent after start structure
try %{ exec -draft k x <a-k> ^ \h * (begin|case|class|def|do|else|elsif|ensure|for|if|module|rescue|unless|until|when|while) \b <ret> j <a-gt> }
2016-03-10 14:12:08 +01:00
}
}
def -hidden _ruby_insert_on_new_line %{
eval -draft -itersel %{
# copy _#_ comment prefix and following white spaces
try %{ exec -draft k x s ^ \h * \K \# \h * <ret> y j p }
2016-01-21 19:19:17 +01:00
# wisely add end structure
eval -save-regs x %{
try %{ exec -draft k x s ^ \h + <ret> \" x y } catch %{ reg x '' }
try %{ exec -draft k x <a-k> ^ <c-r> x (begin|case|class|def|do|for|if|module|unless|until|while) <ret> j <a-a> i X <a-\;> K <a-K> ^ <c-r> x (for|function|if|while) . * \n <c-r> x end $ <ret> j x y p j a end <esc> <a-lt> }
}
}
}
2014-07-03 15:16:59 +02:00
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook global WinSetOption filetype=ruby %{
addhl ref ruby
hook window InsertChar .* -group ruby-indent _ruby_indent_on_char
hook window InsertChar \n -group ruby-indent _ruby_indent_on_new_line
2016-03-10 14:12:08 +01:00
hook window InsertChar \n -group ruby-insert _ruby_insert_on_new_line
2016-01-21 18:33:03 +01:00
set window comment_line_chars '#'
set window comment_selection_chars '^begin=:^=end'
2016-03-10 14:07:08 +01:00
# Rubocop requires a filepath that will be used when generating the errors summary,
# even though it's reading anonymous data on stdin
# It also leaves an ugly separator on the first line on the output
set window formatcmd 'rubocop --auto-correct --stdin - -o /dev/null | sed 1d'
2014-07-03 15:16:59 +02:00
}
hook global WinSetOption filetype=(?!ruby).* %{
rmhl ruby
rmhooks window ruby-indent
rmhooks window ruby-hooks
}