Skip to content

Instantly share code, notes, and snippets.

@davidmfoley
Created July 26, 2011 17:44
Show Gist options
  • Save davidmfoley/1107331 to your computer and use it in GitHub Desktop.
Save davidmfoley/1107331 to your computer and use it in GitHub Desktop.
VIM: flip between test and production files (rails/rspec)
function! FindOrCreateAlternate()
let buflist = []
let bufcount = bufnr("$")
let currbufnr = 1
let altname = AlternateFileName()
while currbufnr <= bufcount
if(buflisted(currbufnr))
let currbufname = bufname(currbufnr)
let curmatch = tolower(currbufname)
if(altname == currbufname)
call add(buflist, currbufnr)
endif
endif
let currbufnr = currbufnr + 1
endwhile
if(len(buflist) > 1)
exec ":b " . get(buflist,0)
else
:w
exec ":e " . altname
endif
endfunction
function! AlternateFileName()
"todo: deal with projects that have /spec/app
let current = fnamemodify(expand("%"), ':p')
if current =~ '/spec/'
if current =~ '/spec/javascripts/'
let altname = substitute(current, "/spec/","/public/", 'g')
let altname = substitute(altname, "\Spec\.js", ".js",'g')
else
let altname = substitute(current, "/spec/lib/","/lib/", 'g')
let altname = substitute(altname, "/spec/","/app/", 'g')
let altname = substitute(altname, "\.erb_spec\.rb", ".erb",'g')
let altname = substitute(altname, "_spec\.rb", ".rb",'g')
endif
elseif current =~ '/test/'
" todo: deal with this
else
if current =~ '/javascripts/'
let altname = substitute(current, "/public/","/spec/", 'g')
let altname = substitute(altname, ".js", "Spec.js", 'g')
else
let altname = substitute(current, "/app/","/spec/", 'g')
let altname = substitute(altname, "/lib/", "/spec/lib/", "g")
let altname = substitute(altname, "\.rb", "_spec.rb", 'g')
endif
endif
return altname
endfunction
function! TestToggleNewTabVertical()
:vsplit
call FindOrCreateAlternate()
endfunction
function! TestToggleNewTabHorizontal()
:split
call FindOrCreateAlternate()
endfunction
" flip between test/subject (same window, vsp, sp)
nnoremap <leader>tt :call FindOrCreateAlternate()<CR>
nnoremap <leader>tv :call TestToggleNewTabVertical()<CR>
nnoremap <leader>th :call TestToggleNewTabHorizontal()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment