Created
September 23, 2013 18:25
-
-
Save bashcoder/6674761 to your computer and use it in GitHub Desktop.
From my .vimrc, switch between test and production code with `<leader>.`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
" SWITCH BETWEEN TEST AND PRODUCTION CODE | |
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | |
function! OpenTestAlternate() | |
let new_file = AlternateForCurrentFile() | |
exec ':e ' . new_file | |
endfunction | |
function! AlternateForCurrentFile() | |
let current_file = expand("%") | |
let new_file = current_file | |
let in_spec = match(current_file, '^spec/') != -1 | |
let going_to_spec = !in_spec | |
let in_app = match(current_file, '\<controllers\>') != -1 || match(current_file, '\<models\>') != -1 || match(current_file, '\<views\>') != -1 || match(current_file, '\<helpers\>') != -1 | |
if going_to_spec | |
if in_app | |
let new_file = substitute(new_file, '^app/', '', '') | |
end | |
let new_file = substitute(new_file, '\.rb$', '_spec.rb', '') | |
let new_file = 'spec/' . new_file | |
else | |
let new_file = substitute(new_file, '_spec\.rb$', '.rb', '') | |
let new_file = substitute(new_file, '^spec/', '', '') | |
if in_app | |
let new_file = 'app/' . new_file | |
end | |
endif | |
return new_file | |
endfunction | |
nnoremap <leader>. :call OpenTestAlternate()<cr> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment