2014-07-03 17:16:59 +04:00
# http://ruby-lang.org
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
# Detection
# ‾‾‾‾‾‾‾‾‾
2018-09-17 17:29:05 +03:00
hook global BufCreate .*(([.](rb))|(irbrc)|(pryrc)|(Brewfile)|(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
}
2019-04-10 06:54:19 +03:00
# Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook global WinSetOption filetype=ruby %{
2019-03-13 08:24:33 +03:00
require-module ruby
2019-04-10 06:54:19 +03:00
set-option window static_words %opt{ruby_static_words}
hook window InsertChar .* -group ruby-indent ruby-indent-on-char
hook window InsertChar \n -group ruby-indent ruby-indent-on-new-line
2019-06-08 00:25:33 +03:00
hook window InsertChar \n -group ruby-insert ruby-insert-on-new-line
2019-04-10 06:54:19 +03:00
alias window alt ruby-alternative-file
hook -once -always window WinSetOption filetype=.* %{
remove-hooks window ruby-.+
unalias window alt ruby-alternative-file
}
}
hook -group ruby-highlight global WinSetOption filetype=ruby %{
add-highlighter window/ruby ref ruby
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/ruby }
2019-03-13 08:24:33 +03:00
}
2020-08-01 22:37:40 +03:00
provide-module ruby %§
2019-03-13 08:24:33 +03:00
2014-07-03 17:16:59 +04:00
# Highlighters
# ‾‾‾‾‾‾‾‾‾‾‾‾
2018-06-28 15:10:22 +03:00
add-highlighter shared/ruby regions
add-highlighter shared/ruby/code default-region group
2019-03-15 20:32:49 +03:00
add-highlighter shared/ruby/double_symbol region ':"' (?<!\\)(\\\\)*" regions
add-highlighter shared/ruby/single_symbol region ":'" (?<!\\)(\\\\)*' fill variable
add-highlighter shared/ruby/double_string region '"' (?<!\\)(\\\\)*" regions
add-highlighter shared/ruby/single_string region "'" (?<!\\)(\\\\)*' fill string
add-highlighter shared/ruby/backtick region '(?<![$:])`' (?<!\\)(\\\\)*` regions
add-highlighter shared/ruby/regex region '(?<![$:])/' (?<!\\)(\\\\)*/[imox]* regions
add-highlighter shared/ruby/ region '#' '$' fill comment
add-highlighter shared/ruby/ region ^=begin ^=end fill comment
2020-08-01 22:54:16 +03:00
add-highlighter shared/ruby/ region -recurse \( '%[qwQW]?\(' \) fill string
add-highlighter shared/ruby/ region -recurse \{ '%[qwQW]?\{' \} fill string
add-highlighter shared/ruby/ region -recurse \[ '%[qwQW]?\[' \] fill string
add-highlighter shared/ruby/ region -recurse < '%[qwQW]?<' > fill string
add-highlighter shared/ruby/ region -recurse \( '%[isIS]\(' \) fill variable
add-highlighter shared/ruby/ region -recurse \{ '%[isIS]\{' \} fill variable
add-highlighter shared/ruby/ region -recurse \[ '%[isIS]\[' \] fill variable
add-highlighter shared/ruby/ region -recurse < '%[isIS]<' > fill variable
add-highlighter shared/ruby/ region -recurse \( '%[rxRX]\(' \) fill meta
add-highlighter shared/ruby/ region -recurse \{ '%[rxRX]\{' \} fill meta
add-highlighter shared/ruby/ region -recurse \[ '%[rxRX]\[' \] fill meta
add-highlighter shared/ruby/ region -recurse < '%[rxRX]<' > fill meta
2020-08-01 22:37:40 +03:00
add-highlighter shared/ruby/ region -match-capture '%[qwQW]?([^0-9A-Za-z\(\{\[<>\]\}\)])' ([^0-9A-Za-z\(\{\[<>\]\}\)]) fill string
add-highlighter shared/ruby/ region -match-capture '%[isIS]([^0-9A-Za-z\(\{\[<>\]\}\)])' ([^0-9A-Za-z\(\{\[<>\]\}\)]) fill variable
add-highlighter shared/ruby/ region -match-capture '%[rxRX]([^0-9A-Za-z\(\{\[<>\]\}\)])' ([^0-9A-Za-z\(\{\[<>\]\}\)]) fill meta
2020-10-13 19:16:44 +03:00
add-highlighter shared/ruby/heredoc region -match-capture '<<[-~]?(?!self)(\w+)' '^\h*(\w+)$' fill string
2020-07-20 22:53:45 +03:00
add-highlighter shared/ruby/division region '[\w\)\]]\K(/|(\h+/\h+))' '\w' group # 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
2018-06-28 15:10:22 +03:00
add-highlighter shared/ruby/double_string/ default-region fill string
2018-07-02 13:59:12 +03:00
add-highlighter shared/ruby/double_string/interpolation region -recurse \{ \Q#{ \} fill meta
2014-07-15 00:42:19 +04:00
2019-03-15 20:32:49 +03:00
add-highlighter shared/ruby/double_symbol/ default-region fill variable
add-highlighter shared/ruby/double_symbol/interpolation region -recurse \{ \Q#{ \} fill meta
2018-06-28 15:10:22 +03:00
add-highlighter shared/ruby/backtick/ default-region fill meta
2018-07-02 13:59:12 +03:00
add-highlighter shared/ruby/backtick/interpolation region -recurse \{ \Q#{ \} fill meta
2014-07-15 00:42:19 +04:00
2018-06-28 15:10:22 +03:00
add-highlighter shared/ruby/regex/ default-region fill meta
2018-07-02 13:59:12 +03:00
add-highlighter shared/ruby/regex/interpolation region -recurse \{ \Q#{ \} fill meta
2014-07-15 00:42:19 +04:00
2018-05-07 00:29:52 +03:00
evaluate-commands %sh{
2016-03-14 15:08:51 +03:00
# 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
2019-04-10 06:54:19 +03:00
printf %s\\n "declare-option str-list ruby_static_words ${keywords} ${attributes} ${values} ${meta}" | tr '|' ' '
2016-03-14 15:08:51 +03:00
# Highlight keywords
2016-03-17 16:30:14 +03:00
printf %s "
2020-03-23 10:20:02 +03:00
add-highlighter shared/ruby/code/ regex \b(${keywords})[^0-9A-Za-z_!?] 1:keyword
2018-06-28 15:10:22 +03:00
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
2019-03-15 20:32:49 +03:00
add-highlighter shared/ruby/code/ regex \b(\w+:(?!:))|(:?(\$(-[0FIKWadilpvw]|["'`/~&+=!$*,:.\;<>?@\\])|(\$|@@?)\w+))|((?<!:):(![~=]|=~|>[=>]?|<((=>?)|<)?|[+\-]@?|\*\*?|===?|[/`%&!^|~]|(\w+[=?!]?)|(\[\]=?)))|([A-Z]\w*|^|\h)\K::(?=[A-Z]) 0:variable
2014-07-03 17:16:59 +04:00
# Commands
# ‾‾‾‾‾‾‾‾
2018-05-07 00:29:52 +03:00
define-command ruby-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ evaluate-commands %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//))
2019-10-08 19:06:54 +03:00
[ ! -f $altfile ] && echo "fail 'implementation file not found'" && exit
2016-03-16 17:05:13 +03:00
;;
2019-05-25 12:39:52 +03:00
*test/*_test.rb)
altfile=$(eval echo $(echo $kak_buffile | sed s+test/+'*'/+';'s/_test//))
2019-10-08 19:06:54 +03:00
[ ! -f $altfile ] && echo "fail 'implementation file not found'" && exit
2019-05-25 12:39:52 +03:00
;;
2016-03-16 17:05:13 +03:00
*.rb)
2020-08-23 19:55:50 +03:00
altfile=""
altdir=""
2016-03-16 17:05:13 +03:00
path=$kak_buffile
dirs=$(while [ $path ]; do echo $path; path=${path%/*}; done | tail -n +2)
for dir in $dirs; do
2019-05-25 12:39:52 +03:00
altdir=$dir/spec && suffix=spec
[ ! -d $altdir ] && altdir=$dir/test && suffix=test
2016-03-16 17:05:13 +03:00
if [ -d $altdir ]; then
2019-05-25 12:39:52 +03:00
altfile=$altdir/$(realpath $kak_buffile --relative-to $dir | sed s+[^/]'*'/++';'s/.rb$/_${suffix}.rb/)
2016-03-16 17:05:13 +03:00
break
fi
done
2020-08-23 19:55:50 +03:00
[ ! -d "$altdir" ] && echo "fail 'spec/ and test/ not found'" && exit
2016-03-16 17:05:13 +03:00
;;
*)
2019-10-08 19:06:54 +03:00
echo "fail 'alternative file not found'" && exit
2016-03-16 17:05:13 +03:00
;;
esac
echo "edit $altfile"
}}
2018-12-19 12:10:26 +03:00
define-command -hidden ruby-trim-indent %{
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
2020-08-02 20:28:27 +03:00
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-&> }
try %{ execute-keys -draft <a-x> <a-k> ^ \h * (elsif) $ <ret> <a-a> i <a-semicolon> <a-?> ^ \h * (if) <ret> <a-S> 1<a-&> }
try %{ execute-keys -draft <a-x> <a-k> ^ \h * (when) $ <ret> <a-a> i <a-semicolon> <a-?> ^ \h * (case) <ret> <a-S> 1<a-&> }
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-&> }
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-&> }
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
2018-12-19 12:10:26 +03:00
try %{ execute-keys -draft k : ruby-trim-indent <ret> }
2016-01-21 21:19:17 +03:00
# indent after start structure
2020-03-23 10:20:02 +03:00
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> }
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
2019-10-22 12:02:06 +03:00
try %{ execute-keys -draft k <a-x> s '^\h*\K#\h*' <ret> y j <a-x><semicolon> 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 %[
2019-06-08 00:25:33 +03:00
# Check if previous line opens a block
2020-03-23 10:20:02 +03:00
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>
2019-06-08 00:25:33 +03:00
# Check that we do not already have an end for this indent level which is first set via `ruby-indent-on-new-line` hook
2020-08-01 20:54:57 +03:00
execute-keys -draft }i J <a-x> <a-K> ^<c-r>x(end|else|elsif|rescue|when)[^0-9A-Za-z_!?]<ret>
2018-04-14 05:47:42 +03:00
]
execute-keys -draft o<c-r>xend<esc> # insert a new line with containing end
]
]
]
]
2014-07-03 17:16:59 +04:00
2020-08-01 22:37:40 +03:00
§