add :alt command to jump to the alternate file (implementation ↔ test)

This commit is contained in:
Alex Leferry 2 2016-03-16 15:05:13 +01:00
parent 5fe2872904
commit 13e6c34c35

View File

@ -61,6 +61,31 @@ addhl -group /ruby/code regex \<(alias|and|begin|break|case|class|def|defined|do
# 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 <a-x>
@ -112,6 +137,8 @@ hook global WinSetOption filetype=ruby %{
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'
@ -125,4 +152,6 @@ hook global WinSetOption filetype=(?!ruby).* %{
rmhl ruby
rmhooks window ruby-indent
rmhooks window ruby-hooks
unalias window alt ruby-alternative-file
}