Jumping to a Ruby bang method using Ctags in Vim

I have a problem transitioning to a Ruby bang method using Exhuberant Ctags. I was looking for others that have a similar problem and I can not find anything. An example of the problem can be shown using the following small Ruby class:

class Hello def start method! end def method # Blah end def method! # Blah end end 

When ctags -R . runs in this file, the resulting tags file contains the following two lines, demonstrating that both methods are detected in the generation:

 method test.rb /^ def method$/;" f class:Hello method! test.rb /^ def method!$/;" f class:Hello 

However, if you place the cursor on a method! call method! in line 3 and press ^] , then the cursor will move to the definition of method , and not to the correct version of bang. The exclamation point does not seem to be included in the identifier search.

Is there a way to fix this so that the correct method moves to?

+6
source share
3 answers

I understand that this is super-old, but I came across the same thing in Vim 8.0 and Neovim. If I enter :tag mymethod! from the vim command line, it will find the corresponding tag, but if I try <C-]> with my pointer to the method name, these are errors E426: tag not found: mymethod (note the absence ! in the name that he was looking for).

You can fix it by adding ! to the list of characters recognized as keywords in Ruby syntax:

 :set iskeyword+=! 

You can add this to ~/.vim/after/syntax/ruby.vim to apply it to any Ruby file you open. I have not tested this, so I canโ€™t say if it will affect anything else. I know that this will change the behavior of jumps. w will, for example, consider ! as part of a "little" word.

On the other hand, it will certainly misunderstand things like !some_test . If you press <C-]> with the cursor somewhere there, it will look for a method called !some_test , which is definitely not what you want. A better solution would be to write a wrapper function around the tag search for Ruby files. I'm actually working on something for this, so I will post when I have something presentable.

Update: I found a surprisingly simple way:

 nnoremap <buffer><silent> <C-]> :tag <CR><CW><CR> 

For some reason, the behavior of <CR><CW> in command line mode is different from the behavior of expand('<cword>') and possibly from the documentation. Even if ! is not a 'iskeyword' , but expand('<cword>') leads to mymethod , <CR><CW> leads to mymethod! . Does the same apply to is_this_your_method? . You can apply this workaround by putting the following in ~/.vim/ftplugin/ruby.vim :

 nnoremap <buffer><silent> <C-]> :tag <CR><CW><CR> nnoremap <buffer><silent> g] :tselect <CR><CW><CR> nnoremap <buffer><silent> g<C-]> :tjump <CR><CW><CR> 

Update 2

It turns out that the special <CR><CW> behavior was provided by vim-ruby (and is included by default in Vim files). This script sets up <CR><CW> and also adds a <Plug><cword> to correctly identify the Ruby cursor id. I just ran into abuse ! because I inadvertently sealed the mappings already provided by vim-ruby when I add what I find more convenient for keyboards:

 nnoremap <C-.> <C-]> 

If I did nmap , then the vim-ruby conversion could do its job. Alternatively, you can use what vim-ruby provides (in the ruby โ€‹โ€‹ftplugin file):

 nnoremap <buffer><silent> <C-]> :<CU>exe v:count1."tag <Plug><cword>"<CR> nnoremap <buffer><silent> g] :<CU>tselect <Plug><cword><CR> nnoremap <buffer><silent> g<C-]> :<CU>tjump <Plug><cword><CR> 
+2
source

You can always use :tag :

  :tag method! 

Or visual mode - if you select any text (using v +) before you press ^] , it will use the selected text as a tag instead of trying to find the "identifier" under the cursor. Therefore, if your cursor is on m in method! then

  vE^] 

gotta do the trick. If your cursor is elsewhere in the word, first press b .

+3
source

I used MacVim 63 snapshots while I posted this question. Now I am using snapshot 72 and the problem has disappeared. The only advice I can give here is to upgrade the version of Vim that you are using.

0
source

Source: https://habr.com/ru/post/914944/


All Articles