2014-07-03 15:16:59 +02:00
# http://ruby-lang.org
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
# Detection
# ‾‾‾‾‾‾‾‾‾
hook global BufCreate .*(([.](rb))|(irbrc)|(pryrc)|(Capfile|[.]cap)|(Gemfile)|(Guardfile)|(Rakefile|[.]rake)|(Thorfile|[.]thor)|(Vagrantfile)) %{
set buffer filetype ruby
}
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
2017-01-04 01:07:45 +01:00
add-highlighter -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]\[' \] \[ \
2016-03-17 11:48:27 +01:00
literal '%[iqrswxIQRSWX]<' > < \
division '[\w\)\]](/|(\h+/\h+))' '\w' '' # Help Kakoune to better detect /…/ literals
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
2017-01-04 01:07:45 +01:00
add-highlighter -group /ruby/double_string fill string
add-highlighter -group /ruby/double_string regions regions interpolation \Q#{ \} \{
add-highlighter -group /ruby/double_string/regions/interpolation fill meta
2014-07-14 22:42:19 +02:00
2017-01-04 01:07:45 +01:00
add-highlighter -group /ruby/single_string fill string
2014-07-14 22:42:19 +02:00
2017-01-04 01:07:45 +01:00
add-highlighter -group /ruby/backtick fill meta
add-highlighter -group /ruby/backtick regions regions interpolation \Q#{ \} \{
add-highlighter -group /ruby/backtick/regions/interpolation fill meta
2014-07-14 22:42:19 +02:00
2017-01-04 01:07:45 +01:00
add-highlighter -group /ruby/regex fill meta
add-highlighter -group /ruby/regex regions regions interpolation \Q#{ \} \{
add-highlighter -group /ruby/regex/regions/interpolation fill meta
2014-07-14 22:42:19 +02:00
2017-01-04 01:07:45 +01:00
add-highlighter -group /ruby/comment fill comment
2014-07-14 22:42:19 +02:00
2017-01-04 01:07:45 +01:00
add-highlighter -group /ruby/literal fill meta
2014-07-03 15:16:59 +02:00
2017-02-28 09:48:17 +01:00
add-highlighter -group /ruby/code regex \b([A-Za-z]\w*:(?=[^:]))|([$@][A-Za-z]\w*)|((?<=[^:]):(([A-Za-z]\w*[=?!]?)|(\[\]=?)))|([A-Z]\w*|^|\h)\K::(?=[A-Z]) 0:variable
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"
2016-12-25 20:01:56 +01:00
meta="require|include|extend"
2016-03-14 13:08:51 +01:00
# Add the language's grammar to the static completion list
2016-04-23 07:47:01 +02:00
printf %s\\n "hook global WinSetOption filetype=ruby %{
2016-04-09 11:20:13 +02:00
set window static_words '${keywords}:${attributes}:${values}:${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 "
2017-01-04 01:07:45 +01:00
add-highlighter -group /ruby/code regex \b(${keywords})\b 0:keyword
add-highlighter -group /ruby/code regex \b(${attributes})\b 0:attribute
add-highlighter -group /ruby/code regex \b(${values})\b 0:value
add-highlighter -group /ruby/code regex \b(${meta})\b 0:meta
2016-03-14 13:08:51 +01:00
"
}
2014-07-03 15:16:59 +02:00
# Commands
# ‾‾‾‾‾‾‾‾
2016-03-16 15:05:13 +01:00
def ruby-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ %sh{
case $kak_buffile in
*spec/*_spec.rb)
altfile=$(eval echo $(echo $kak_buffile | sed s+spec/+'*'/+';'s/_spec//))
2017-07-19 17:18:52 +02:00
[ ! -f $altfile ] && echo "echo -markup '{Error}implementation file not found'" && exit
2016-03-16 15:05:13 +01:00
;;
*.rb)
path=$kak_buffile
dirs=$(while [ $path ]; do echo $path; path=${path%/*}; done | tail -n +2)
for dir in $dirs; do
altdir=$dir/spec
if [ -d $altdir ]; then
altfile=$altdir/$(realpath $kak_buffile --relative-to $dir | sed s+[^/]'*'/++';'s/.rb$/_spec.rb/)
break
fi
done
2017-07-19 17:18:52 +02:00
[ ! -d $altdir ] && echo "echo -markup '{Error}spec/ not found'" && exit
2016-03-16 15:05:13 +01:00
;;
*)
2017-07-19 17:18:52 +02:00
echo "echo -markup '{Error}alternative file not found'" && exit
2016-03-16 15:05:13 +01:00
;;
esac
echo "edit $altfile"
}}
2017-01-13 01:56:30 +01:00
def -hidden ruby-filter-around-selections %{
2016-09-09 15:23:06 +02:00
eval -no-hooks -draft -itersel %{
2016-03-10 14:48:12 +01:00
exec <a-x>
# remove trailing white spaces
try %{ exec -draft s \h + $ <ret> d }
}
}
2017-01-13 01:56:30 +01:00
def -hidden ruby-indent-on-char %{
2016-09-09 15:23:06 +02:00
eval -no-hooks -draft -itersel %{
2016-01-21 19:19:17 +01:00
# align middle and end structures to start
2017-09-29 04:39:59 +02:00
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-&> }
2016-01-21 19:19:17 +01:00
}
}
2017-01-13 01:56:30 +01:00
def -hidden ruby-indent-on-new-line %{
2016-09-09 15:23:06 +02:00
eval -no-hooks -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
2017-01-13 01:56:30 +01:00
try %{ exec -draft k : ruby-filter-around-selections <ret> }
2016-01-21 19:19:17 +01:00
# indent after start structure
2017-01-13 01:50:03 +01:00
try %{ exec -draft k <a-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
}
}
2017-01-13 01:56:30 +01:00
def -hidden ruby-insert-on-new-line %{
2016-09-09 15:23:06 +02:00
eval -no-hooks -draft -itersel %{
2016-03-10 14:12:08 +01:00
# copy _#_ comment prefix and following white spaces
2017-09-30 15:39:57 +02:00
try %{ exec -draft k <a-x> s '^\h*\K#\h*' <ret> y gh j P }
2016-01-21 19:19:17 +01:00
# wisely add end structure
eval -save-regs x %{
2017-01-13 01:50:03 +01:00
try %{ exec -draft k <a-x> s ^ \h + <ret> \" x y } catch %{ reg x '' }
try %{ exec -draft k <a-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 (begin|case|class|def|do|for|if|module|unless|until|while) . * \n <c-r> x end $ <ret> j x y p j a end <esc> <a-lt> }
2016-01-21 19:19:17 +01:00
}
}
}
2014-07-03 15:16:59 +02:00
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
2017-01-04 01:07:45 +01:00
hook -group ruby-highlight global WinSetOption filetype=ruby %{ add-highlighter ref ruby }
2014-07-03 15:16:59 +02:00
2016-09-25 15:15:07 +02:00
hook global WinSetOption filetype=ruby %{
2017-01-13 01:56:30 +01:00
hook window InsertChar .* -group ruby-indent ruby-indent-on-char
hook window InsertChar \n -group ruby-insert ruby-insert-on-new-line
hook window InsertChar \n -group ruby-indent ruby-indent-on-new-line
2016-01-21 18:33:03 +01:00
2016-03-16 15:05:13 +01:00
alias window alt ruby-alternative-file
2014-07-03 15:16:59 +02:00
}
2017-01-04 01:07:45 +01:00
hook -group ruby-highlight global WinSetOption filetype=(?!ruby).* %{ remove-highlighter ruby }
2016-09-28 08:45:01 +02:00
2014-07-03 15:16:59 +02:00
hook global WinSetOption filetype=(?!ruby).* %{
2017-01-04 01:07:45 +01:00
remove-hooks window ruby-indent
remove-hooks window ruby-insert
2016-03-16 15:05:13 +01:00
unalias window alt ruby-alternative-file
2014-07-03 15:16:59 +02:00
}