From ff1e84bf89cfd214303d9815f8688bc892cc648d Mon Sep 17 00:00:00 2001 From: yan Date: Wed, 30 May 2012 01:33:24 -0700 Subject: [PATCH] Integrated spec-finder plugin with vim-ruby-conque --- README.md | 24 +++++++++++++++++++++-- doc/spec-finder.txt | 31 +++++++++++++++++++++++++++++ plugin/spec-finder.vim | 40 ++++++++++++++++++++++++++++++++++++++ plugin/vim-ruby-conque.vim | 6 ++++++ 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 doc/spec-finder.txt create mode 100644 plugin/spec-finder.vim diff --git a/README.md b/README.md index d256bd0..7e09094 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ -vim-ruby-conque.vim +Ruby Conque & Fast-spec Aware RSpec Finder ============ * Colorized Ruby, Rake, RSpec, and Cucumber output in vim using ConqueTerm + * Spec Finder - use `Ctrl-s` to automatically find the related spec and open in a split, fast_spec aware! * Sensible keybindings (feel free to change), all prefixed with 'rc': ```vim @@ -12,6 +13,9 @@ nmap rccc :call RunCucumberCurrentFileConque() nmap rccl :call RunCucumberCurrentLineConque() nmap rcRR :call RunRakeConque() nmap rcrl :call RunLastConqueCommand() + +nnoremap :call RelatedSpecVOpen() +nnoremap , :call RelatedSpecOpen() ``` * Requires: http://code.google.com/p/conque/ @@ -34,7 +38,7 @@ nmap :call RunRspecCurrentLineConque() nmap , :call RunLastConqueCommand() ``` -Usage +Using the Spec Runner ------------- Try some of the keybindings listed above. @@ -45,6 +49,22 @@ Upon running a test your cursor will be in the Conque buffer, you can use these p goes to the previous Failure message. f goes to the Finished At section for an overview of the test. +Using the Spec Finder +-------------- + +While inside a ruby file, invoke the shortcut (supplied as `Ctrl-s`), and +the corresponding spec will be open. The assumptions are: + + * Your specs live in spec/ or fast_spec/ + * Your pwd (current dir) is the project root + * You use the same dir structure in your code and specs so that + code living at lib/foo/bar.rb has a spec at spec/lib/foo/bar_spec.rb + +For more information, please see doc/spec-finder.txt. There are included +functions for opening the spec in the current window, and in a split, as +well as just getting its full path. + + Configuration -------------- diff --git a/doc/spec-finder.txt b/doc/spec-finder.txt new file mode 100644 index 0000000..7e02acf --- /dev/null +++ b/doc/spec-finder.txt @@ -0,0 +1,31 @@ +*spec-finder.txt* Finds and opens the spec related to the file you're working on. + +Author: Yan Pritzker + +USAGE *spec-finder* + +While inside a ruby file, invoke the shortcut (supplied as Ctrl-s), and +the corresponding spec will be open. The assumptions are: + + * Your specs live in spec/ or fast_spec/ + * Your pwd (current dir) is the project root + * You use the same dir structure in your code and specs so that + code living at lib/foo/bar.rb has a spec at spec/lib/foo/bar_spec.rb + +This method handles files in fast_spec unlike the :A and :AV functions +that ship with rails.vim + +FUNCTIONS *spec-finder-functions* + +RelatedSpec() - gives you the name of the related spec, can be integrated +into other scripts, such as something that saves your file and runsj the +related spec. +RelatedSpecOpen() - Opens the related spec in current window +RelatedSpecVOpen() - Opens the related spec in vertical split + +ABOUT *spec-finder-author* + +Find the project, and contribute at: +https://github.com/skwp/vim-spec-finder + +Copyright (c) Yan Pritzker. Distributed under the same terms as Vim itself. diff --git a/plugin/spec-finder.vim b/plugin/spec-finder.vim new file mode 100644 index 0000000..eb1d41c --- /dev/null +++ b/plugin/spec-finder.vim @@ -0,0 +1,40 @@ +" Find the related spec for any file you open. +function! RelatedSpec() + let l:fullpath = expand("%:p") + let l:filepath = expand("%:h") + let l:fname = expand("%:t") + let l:filepath_without_app = substitute(l:filepath, "app/", "", "") + + " Possible names for the spec/test for the file we're looking at + let l:test_names = [substitute(l:fname, ".rb$", "_spec.rb", ""), substitute(l:fname, ".rb$", "_test.rb", "")] + + " Possible paths + let l:test_paths = ["spec", "fast_spec", "test"] + + for test_name in l:test_names + for path in l:test_paths + let l:spec_path = path . "/" . l:filepath_without_app . "/" . test_name + let l:full_spec_path = substitute(l:fullpath, l:filepath . "/" . l:fname, l:spec_path, "") + if filereadable(l:spec_path) + return l:full_spec_path + end + endfor + endfor +endfunction + +function! RelatedSpecOpen() + let l:spec_path = RelatedSpec() + if filereadable(l:spec_path) + execute ":e " . l:spec_path + endif +endfunction + +function! RelatedSpecVOpen() + let l:spec_path = RelatedSpec() + if filereadable(l:spec_path) + execute ":botright vsp " . l:spec_path + endif +endfunction + +nnoremap :call RelatedSpecVOpen() +nnoremap , :call RelatedSpecOpen() diff --git a/plugin/vim-ruby-conque.vim b/plugin/vim-ruby-conque.vim index a1ef901..43311d2 100644 --- a/plugin/vim-ruby-conque.vim +++ b/plugin/vim-ruby-conque.vim @@ -72,6 +72,11 @@ function! RunLastConqueCommand() endif endfunction +" Requires https://github.com/skwp/vim-spec-finder +function! RunRspecRelated() + call RunSingleConque(g:ruby_conque_rspec_command . " " . RelatedSpec() . " --color") +endfunction + " Get around Conques annoying trapping of input in some kind of strange " inputless input mode. Also added q to close the buffer. n and p jump between " errors in the output buffer. @@ -94,3 +99,4 @@ nmap rccl :call RunCucumberCurrentLineConque() nmap rccc :call RunCucumberCurrentFileConque() nmap rcRR :call RunRakeConque() nmap rcrl :call RunLastConqueCommand() +nmap rcrel :call RunRspecRelated()