Permalink
Browse files

add vim shortcut for switching between test and production code (via

garybernhardt)
  • Loading branch information...
1 parent c0c3a73 commit 8ba1127aa8ea7aafdbd58c646ef2fd2ed50c7825 @molawson committed Oct 9, 2012
Showing with 31 additions and 0 deletions.
  1. +31 −0 vimrc
View
31 vimrc
@@ -126,6 +126,37 @@ map <leader>gg :topleft 100 :split Gemfile<cr>
let g:CommandTMaxHeight=20
+""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+" 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>
+
+
"""""""""""""""""
" RUNNING TESTS
"""""""""""""""""

0 comments on commit 8ba1127

Please sign in to comment.