From a5b5dd63e08508806ba0e7d383ce30e9f5074566 Mon Sep 17 00:00:00 2001 From: Yan Pritzker Date: Mon, 8 Oct 2012 18:39:08 -0700 Subject: [PATCH] Implement jump to ruby method (bang-aware) It always annoyed me that vim jump-to-tag (ctrl-] or ,f in yadr) totally flopped when it came to ruby bang methods. This function handles methods! and method.invocations! to find bang versions of methods. --- README.md | 1 + vim/plugin/settings/smart_jump_to_tag.vim | 46 +++++++++++++++++++++++ vim/plugin/settings/yadr-keymap.vim | 7 ---- 3 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 vim/plugin/settings/smart_jump_to_tag.vim diff --git a/README.md b/README.md index c17bda43..9d5c2ff4 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,7 @@ If you omit the key combo, you'll get a list of all the maps. You can do the sam #### Search/Code Navigation * `,f` - instantly Find definition of class (must have exuberant ctags installed) + * `ctrl-m` - jump to Method. Same as vim's built in jump to tag, but much more aware of ruby bang_methods! and method.invocations! * `,F` - same as ,f but in a vertical split * `,gf` or `Ctrl-f` - same as vim normal gf (go to file), but in a vertical split (works with file.rb:123 line numbers also) * `gF` - standard vim mapping, here for completeness (go to file at line number) diff --git a/vim/plugin/settings/smart_jump_to_tag.vim b/vim/plugin/settings/smart_jump_to_tag.vim new file mode 100644 index 00000000..bc921212 --- /dev/null +++ b/vim/plugin/settings/smart_jump_to_tag.vim @@ -0,0 +1,46 @@ +function! JumpToRubyMethod() + " Grab the current WORD. Which could be something like + " object.some_method! + let l:stuff_under_cursor = expand("") + + " Figure out if this is a method call (obj.some_method) + " by looking for the period + let l:method_invocation = split(matchstr(l:stuff_under_cursor, '\..*'), '\.') + + " If there was no method invocation in the current word then + " we want to avoid the which might be something + " like foo_bar(baz). We just want the method name, which + " is already stored for us as by vim + if empty(l:method_invocation) + " See if this is a regular method ending in ! + let l:bang_method = matchstr(l:stuff_under_cursor, '.*!') + + if(empty(l:bang_method)) + let l:method_name = expand("") + else + let l:method_name = l:bang_method + end + else + " If there is a method invocation, then figure out + " the method name, which is the first element in the match + let l:method_name = l:method_invocation[0] + endif + + try + execute ':tag ' . l:method_name + catch + endtry +endfunction + +" hit ,f to find the definition of the current class +" this uses ctags. the standard way to get this is Ctrl-] +" Using viW so we select the entire word, including any +" exclamation point in a ruby method. FIXME this is not +" good for methods like this(foo) which has parens +" nnoremap ,f viW +nnoremap ,f + +nnoremap :call JumpToRubyMethod() +" use ,F to jump to tag in a vertical split +nnoremap ,F :let word=expand(""):vsp:wincmd w:exec("tag ". word) + diff --git a/vim/plugin/settings/yadr-keymap.vim b/vim/plugin/settings/yadr-keymap.vim index 7f493b07..e0b783bf 100644 --- a/vim/plugin/settings/yadr-keymap.vim +++ b/vim/plugin/settings/yadr-keymap.vim @@ -126,13 +126,6 @@ nnoremap ,gcp :GitGrepCurrentPartial "GitGrep Current File nnoremap ,gcf :call GitGrep(expand("%:t:r")) -" hit ,f to find the definition of the current class -" this uses ctags. the standard way to get this is Ctrl-] -nnoremap ,f - -" use ,F to jump to tag in a vertical split -nnoremap ,F :let word=expand(""):vsp:wincmd w:exec("tag ". word) - "Move back and forth through previous and next buffers "with ,z and ,x