# http://ruby-lang.org # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ # require commenting.kak # 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 # ‾‾‾‾‾‾‾‾‾‾‾‾ addhl -group / regions -default code ruby \ double_string '"' (? < # 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 addhl -group /ruby/double_string regions regions interpolation \Q#{ \} \{ addhl -group /ruby/double_string/regions/interpolation fill meta addhl -group /ruby/single_string fill string addhl -group /ruby/backtick fill meta addhl -group /ruby/backtick regions regions interpolation \Q#{ \} \{ addhl -group /ruby/backtick/regions/interpolation fill meta addhl -group /ruby/regex fill meta addhl -group /ruby/regex regions regions interpolation \Q#{ \} \{ addhl -group /ruby/regex/regions/interpolation fill meta addhl -group /ruby/comment fill comment addhl -group /ruby/literal fill meta addhl -group /ruby/code regex \<([A-Za-z]\w*:)|([$@][A-Za-z]\w*)|(\W\K:[A-Za-z]\w*[=?!]?) 0:identifier addhl -group /ruby/code regex \<(require|include)\> 0:meta addhl -group /ruby/code regex \<(attr_(reader|writer|accessor))\> 0:attribute # Keywords are collected searching for keyword_ at # https://github.com/ruby/ruby/blob/trunk/parse.y addhl -group /ruby/code regex \<(alias|and|begin|break|case|class|def|defined|do|else|elsif|end|ensure|false|for|if|in|module|next|nil|not|or|redo|rescue|retry|return|self|super|then|true|undef|unless|until|when|while|yield)\> 0:keyword # Commands # ‾‾‾‾‾‾‾‾ 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//)) [ ! -f $altfile ] && echo "echo -color Error 'implementation file not found'" && exit ;; *.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 [ ! -d $altdir ] && echo "echo -color Error 'spec/ not found'" && exit ;; *) echo "echo -color Error 'alternative file not found'" && exit ;; esac echo "edit $altfile" }} def -hidden _ruby_filter_around_selections %{ eval -draft -itersel %{ exec # remove trailing white spaces try %{ exec -draft s \h + $ d } } } def -hidden _ruby_indent_on_char %{ eval -draft -itersel %{ # align middle and end structures to start try %{ exec -draft ^ \h * (else|elsif) $ ^ \h * (if) s \A | \Z \' } try %{ exec -draft ^ \h * (when) $ ^ \h * (case) s \A | \Z \' } try %{ exec -draft ^ \h * (rescue) $ ^ \h * (begin) s \A | \Z \' } try %{ exec -draft ^ \h * (end) $ ^ \h * (begin|case|class|def|do|for|if|module|unless|until|while) s \A | \Z \' } } } def -hidden _ruby_indent_on_new_line %{ eval -draft -itersel %{ # preserve previous line indent try %{ exec -draft K } # filter previous line try %{ exec -draft k : _ruby_filter_around_selections } # indent after start structure try %{ exec -draft k x ^ \h * (begin|case|class|def|do|else|elsif|ensure|for|if|module|rescue|unless|until|when|while) \b j } } } 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 * y j p } # wisely add end structure eval -save-regs x %{ try %{ exec -draft k x s ^ \h + \" x y } catch %{ reg x '' } try %{ exec -draft k x ^ x (begin|case|class|def|do|for|if|module|unless|until|while) j i X K ^ x (for|function|if|while) . * \n x end $ j x y p j a end } } } } # 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 hook window InsertChar \n -group ruby-insert _ruby_insert_on_new_line alias window alt ruby-alternative-file set window comment_line_chars '#' set window comment_selection_chars '^begin=:^=end' # 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' } hook global WinSetOption filetype=(?!ruby).* %{ rmhl ruby rmhooks window ruby-indent rmhooks window ruby-hooks unalias window alt ruby-alternative-file }