2014-07-03 17:16:59 +04:00
# http://ruby-lang.org
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
# Detection
# ‾‾‾‾‾‾‾‾‾
2018-02-18 02:49:09 +03:00
hook global BufCreate .*(([.](rb))|(irbrc)|(pryrc)|(Capfile|[.]cap)|(Gemfile|[.]gemspec)|(Guardfile)|(Rakefile|[.]rake)|(Thorfile|[.]thor)|(Vagrantfile)) %{
2017-11-03 10:34:41 +03:00
set-option buffer filetype ruby
2014-07-03 17:16:59 +04:00
}
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
2018-01-24 22:19:52 +03:00
add-highlighter shared/ regions -default code -match-capture ruby \
2014-07-10 18:23:15 +04:00
double_string '"' (?<!\\)(\\\\)*" '' \
2016-02-27 05:34:47 +03:00
single_string "'" (?<!\\)(\\\\)*' '' \
2014-07-10 18:23:15 +04:00
backtick '`' (?<!\\)(\\\\)*` '' \
regex '/' (?<!\\)(\\\\)*/[imox]* '' \
comment '#' '$' '' \
comment ^begin= ^=end '' \
literal '%[iqrswxIQRSWX]\(' \) \( \
literal '%[iqrswxIQRSWX]\{' \} \{ \
literal '%[iqrswxIQRSWX]\[' \] \[ \
2016-03-17 13:48:27 +03:00
literal '%[iqrswxIQRSWX]<' > < \
2018-03-23 16:42:48 +03:00
heredoc '<<[-~]?(\w+)' '^\h*(\w+)$' '' \
2016-03-17 13:48:27 +03:00
division '[\w\)\]](/|(\h+/\h+))' '\w' '' # Help Kakoune to better detect /…/ literals
2014-07-03 17:16:59 +04: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-10-28 06:00:51 +03:00
add-highlighter shared/ruby/double_string fill string
add-highlighter shared/ruby/double_string regions regions interpolation \Q#{ \} \{
add-highlighter shared/ruby/double_string/regions/interpolation fill meta
2014-07-15 00:42:19 +04:00
2017-10-28 06:00:51 +03:00
add-highlighter shared/ruby/single_string fill string
2018-01-24 22:19:52 +03:00
add-highlighter shared/ruby/heredoc fill string
2014-07-15 00:42:19 +04:00
2017-10-28 06:00:51 +03:00
add-highlighter shared/ruby/backtick fill meta
add-highlighter shared/ruby/backtick regions regions interpolation \Q#{ \} \{
add-highlighter shared/ruby/backtick/regions/interpolation fill meta
2014-07-15 00:42:19 +04:00
2017-10-28 06:00:51 +03:00
add-highlighter shared/ruby/regex fill meta
add-highlighter shared/ruby/regex regions regions interpolation \Q#{ \} \{
add-highlighter shared/ruby/regex/regions/interpolation fill meta
2014-07-15 00:42:19 +04:00
2017-10-28 06:00:51 +03:00
add-highlighter shared/ruby/comment fill comment
2014-07-15 00:42:19 +04:00
2017-10-28 06:00:51 +03:00
add-highlighter shared/ruby/literal fill meta
2014-07-03 17:16:59 +04:00
2017-10-28 06:00:51 +03:00
add-highlighter shared/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 17:16:59 +04:00
2016-03-14 15:08:51 +03:00
%sh{
# Grammar
2016-03-14 19:29:11 +03:00
# Keywords are collected searching for keywords at
2016-03-14 15:08:51 +03:00
# https://github.com/ruby/ruby/blob/trunk/parse.y
2016-03-14 19:29:11 +03:00
keywords="alias|and|begin|break|case|class|def|defined|do|else|elsif|end"
2018-02-08 15:34:40 +03:00
keywords="${keywords}|ensure|false|for|if|in|module|next|nil|not|or|private|protected|public|redo"
2016-03-14 19:29:11 +03:00
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 22:01:56 +03:00
meta="require|include|extend"
2016-03-14 15:08:51 +03:00
# Add the language's grammar to the static completion list
2016-04-23 08:47:01 +03:00
printf %s\\n "hook global WinSetOption filetype=ruby %{
2017-11-03 10:34:41 +03:00
set-option window static_words '${keywords}:${attributes}:${values}:${meta}'
2018-02-18 07:00:30 +03:00
}" | tr '|' ':'
2016-03-14 15:08:51 +03:00
# Highlight keywords
2016-03-17 16:30:14 +03:00
printf %s "
2017-10-28 06:00:51 +03:00
add-highlighter shared/ruby/code regex \b(${keywords})\b 0:keyword
add-highlighter shared/ruby/code regex \b(${attributes})\b 0:attribute
add-highlighter shared/ruby/code regex \b(${values})\b 0:value
add-highlighter shared/ruby/code regex \b(${meta})\b 0:meta
2016-03-14 15:08:51 +03:00
"
}
2014-07-03 17:16:59 +04:00
# Commands
# ‾‾‾‾‾‾‾‾
2017-11-03 10:34:41 +03:00
define-command ruby-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ %sh{
2016-03-16 17:05:13 +03:00
case $kak_buffile in
*spec/*_spec.rb)
altfile=$(eval echo $(echo $kak_buffile | sed s+spec/+'*'/+';'s/_spec//))
2017-07-19 18:18:52 +03:00
[ ! -f $altfile ] && echo "echo -markup '{Error}implementation file not found'" && exit
2016-03-16 17:05:13 +03: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 18:18:52 +03:00
[ ! -d $altdir ] && echo "echo -markup '{Error}spec/ not found'" && exit
2016-03-16 17:05:13 +03:00
;;
*)
2017-07-19 18:18:52 +03:00
echo "echo -markup '{Error}alternative file not found'" && exit
2016-03-16 17:05:13 +03:00
;;
esac
echo "edit $altfile"
}}
2017-11-03 10:34:41 +03:00
define-command -hidden ruby-filter-around-selections %{
2017-11-03 11:09:45 +03:00
evaluate-commands -no-hooks -draft -itersel %{
execute-keys <a-x>
2016-03-10 16:48:12 +03:00
# remove trailing white spaces
2017-11-03 11:09:45 +03:00
try %{ execute-keys -draft s \h + $ <ret> d }
2016-03-10 16:48:12 +03:00
}
}
2017-11-03 10:34:41 +03:00
define-command -hidden ruby-indent-on-char %{
2017-11-03 11:09:45 +03:00
evaluate-commands -no-hooks -draft -itersel %{
2016-01-21 21:19:17 +03:00
# align middle and end structures to start
2018-03-25 03:45:27 +03:00
try %{ execute-keys -draft <a-x> <a-k> ^ \h * (else|elsif) $ <ret> <a-\;> <a-?> ^ \h * (if) <ret> s \A | \z <ret> ) <a-&> }
try %{ execute-keys -draft <a-x> <a-k> ^ \h * (when) $ <ret> <a-\;> <a-?> ^ \h * (case) <ret> s \A | \z <ret> ) <a-&> }
try %{ execute-keys -draft <a-x> <a-k> ^ \h * (rescue) $ <ret> <a-\;> <a-?> ^ \h * (begin) <ret> s \A | \z <ret> ) <a-&> }
try %{ execute-keys -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 21:19:17 +03:00
}
}
2017-11-03 10:34:41 +03:00
define-command -hidden ruby-indent-on-new-line %{
2017-11-03 11:09:45 +03:00
evaluate-commands -no-hooks -draft -itersel %{
2014-07-03 17:16:59 +04:00
# preserve previous line indent
2017-11-03 11:09:45 +03:00
try %{ execute-keys -draft K <a-&> }
2016-03-10 16:48:12 +03:00
# filter previous line
2017-11-03 11:09:45 +03:00
try %{ execute-keys -draft k : ruby-filter-around-selections <ret> }
2016-01-21 21:19:17 +03:00
# indent after start structure
2017-11-03 11:09:45 +03:00
try %{ execute-keys -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 16:12:08 +03:00
}
}
2018-04-14 05:47:42 +03:00
define-command -hidden ruby-insert-on-new-line %[
evaluate-commands -no-hooks -draft -itersel %[
2016-03-10 16:12:08 +03:00
# copy _#_ comment prefix and following white spaces
2017-11-03 11:09:45 +03:00
try %{ execute-keys -draft k <a-x> s '^\h*\K#\h*' <ret> y gh j P }
2016-01-21 21:19:17 +03:00
# wisely add end structure
2018-04-14 05:47:42 +03:00
evaluate-commands -save-regs x %[
2017-11-03 11:09:45 +03:00
try %{ execute-keys -draft k <a-x> s ^ \h + <ret> \" x y } catch %{ reg x '' }
2018-04-14 05:47:42 +03:00
try %[
evaluate-commands -draft %[
execute-keys -draft k<a-x> <a-k>^<c-r>x(begin|case|class|def|do|for|if|module|unless|until|while)<ret> # Check if previous line opens a block
# Check that we do not already have an end for this indent level, or that we have another block opening at that indent level first
execute-keys -draft Ge <a-K>\A(^\n|^<c-r>x(?!begin)(?!case)(?!class)(?!def)(?!do)(?!for)(?!if)(?!module)(?!unless)(?!until)(?!while)[^\n]*\n)+<c-r>xend$<ret>
]
execute-keys -draft o<c-r>xend<esc> # insert a new line with containing end
]
]
]
]
2014-07-03 17:16:59 +04:00
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
2017-10-28 06:00:51 +03:00
hook -group ruby-highlight global WinSetOption filetype=ruby %{ add-highlighter window ref ruby }
2014-07-03 17:16:59 +04:00
2016-09-25 16:15:07 +03:00
hook global WinSetOption filetype=ruby %{
2017-01-13 03:56:30 +03: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 20:33:03 +03:00
2016-03-16 17:05:13 +03:00
alias window alt ruby-alternative-file
2014-07-03 17:16:59 +04:00
}
2017-10-28 06:00:51 +03:00
hook -group ruby-highlight global WinSetOption filetype=(?!ruby).* %{ remove-highlighter window/ruby }
2016-09-28 09:45:01 +03:00
2014-07-03 17:16:59 +04:00
hook global WinSetOption filetype=(?!ruby).* %{
2017-01-04 03:07:45 +03:00
remove-hooks window ruby-indent
remove-hooks window ruby-insert
2016-03-16 17:05:13 +03:00
unalias window alt ruby-alternative-file
2014-07-03 17:16:59 +04:00
}