Lookup a specific kind of tag in Vim
Asked Answered
T

3

9

So here's my problem. I've gotten exuberant ctags working with Vim, and it works great, most of the time. One thing that still irks me though is whenever I try to search for a function that is named the same as some variable name. I sometimes get the right tag on the first try, sometimes not. Then after I pull up the list of alternate tags with :tselect, it comes up with a list of tags for both function definitions or variable definitions/assignments. (I'm in PHP so definitions and assignments are syntactically indistinguishable).

However, I notice that there's a column labeled 'kind' that has a value of 'f' or 'v', for function and variable, respectively. I can't seem to find a whole lot of information about this field, it seems like it may not be exactly standardized or widely used. My question is: can you filter tag results in Vim by "kind"?

Ideally, the default would be to search the whole tags file, but by specifying some extra flag, you could search a specific ('f' or 'v') kind only.

This is such a small problem for me as it doesn't come up THAT often, but sometimes it's the small problems that really annoy you.

Teacake answered 28/10, 2011 at 17:28 Comment(2)
This blog post looks promising along the lines of what you (and I) are after: andrewradev.com/2011/10/15/…Higherup
Even if you could produce a tags file that has enough information to distinguish everything, that is only half the battle, and nobody seems to be focused on the other half. The other half is for Vim to take into account the context from which you are looking up a tag, and automatically only search for a specific type of tag based on the context. As far as I know Vim does not automatically do anything in this regard, and I have never seen anyone attempt to configure Vim to work this way.Narial
C
8

You can certainly generate ctag files with any combination of php-kinds that you want (see the ouput of the command ctags --list-kinds.)

If you feel it's worth the effort you can make a vim function tagkind and bind it to a command. The tagkind function can overwrite the current tags vim variable to point at only the tag file with the kinds that you are interested in and call :tag. Optionally, it can store the previous version of the tags variable and restore it after this one call.

Unfortunately, I don't know of anyway other than this. Perhaps someone else would know.

Constitute answered 28/10, 2011 at 19:39 Comment(1)
Interesting idea -- works for me. If anyone cares, I put up my snippet of .vimrc code at gist.github.com/1349053 I'm not very good at vimscript, but it seems to be good enough to me.Teacake
S
5

I generate python ctags with --python-kinds=-i to exclude tags for import statements (which are useless). Maybe you could generate with --php-kinds=-v and drop a class of tags completely.

You can read :help tag-priority. Apparently the "highest-priority" tag is chosen based on some hard-coded logic.

Silo answered 29/10, 2011 at 0:37 Comment(0)
H
1

fzf with fzf.vim has a :Tags (for the whole project) and :BTags for the current file option that generates ctags on the fly.

An issue raised on the plugin 'Skip tag kinds in :BTags and :Tags' gives the following code that you can use to only generated tags for a particular kind. I've modified the below so that it should only search for the PHP f kind.

command! BTagsEnhanced
        \ call fzf#vim#buffer_tags(<q-args>, [
        \ printf('ctags -f - --sort=no --php-kinds=f --excmd=number --language-force=%s %s', &filetype, expand('%:S'))], {})

Note as per my comment on the question, there is a potential Vim tagfinder.vim plugin via a blog post on Vim and Ctags: Finding Tag Definitions. But I haven't tried it.

Higherup answered 12/9, 2017 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.