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

This commit is contained in:
Alex Leferry 2 2016-03-03 18:56:50 +01:00
parent 11153adcd8
commit 936bfcf0c8

View File

@ -32,6 +32,31 @@ addhl -group /lua/code regex \<(and|break|do|else|elseif|end|false|for|function|
# Commands # Commands
# ‾‾‾‾‾‾‾‾ # ‾‾‾‾‾‾‾‾
def lua-alternative-file -docstring 'Jump to the alternate file (implementation ↔ test)' %{ %sh{
case $kak_buffile in
*spec/*_spec.lua)
altfile=$(eval echo $(echo $kak_buffile | sed -e s+spec/+'*'/+';'s/_spec//))
[ ! -f $altfile ] && echo "echo -color Error 'implementation file not found'" && exit
;;
*.lua)
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 -e s+[^/]'*'/++';'s/.lua$/_spec.lua/)
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 _lua_filter_around_selections %{ def -hidden _lua_filter_around_selections %{
eval -draft -itersel %{ eval -draft -itersel %{
exec <a-x> exec <a-x>
@ -77,6 +102,8 @@ hook global WinSetOption filetype=lua %{
hook window InsertChar .* -group lua-indent _lua_indent_on_char hook window InsertChar .* -group lua-indent _lua_indent_on_char
hook window InsertChar \n -group lua-indent _lua_indent_on_new_line hook window InsertChar \n -group lua-indent _lua_indent_on_new_line
alias window alt lua-alternative-file
set window comment_line_chars '--' set window comment_line_chars '--'
set window comment_selection_chars '\Q--[[:]]' set window comment_selection_chars '\Q--[[:]]'
} }
@ -85,4 +112,6 @@ hook global WinSetOption filetype=(?!lua).* %{
rmhl lua rmhl lua
rmhooks window lua-indent rmhooks window lua-indent
rmhooks window lua-hooks rmhooks window lua-hooks
unalias window alt lua-alternative-file
} }