Search tags only in current file
Asked Answered
P

3

9

I am using ":ta " to jump to a method.
For example i got two classes named A.java and B.java. They both have a foo() method and B.java have another method called fooBar(). Then i open A.java and input :ta foo then press TAB then i will got two completion : foo and fooBar. But what i want to jump now is just tag in current file, i don't like tag in other file to display.

And i found tagslist does very good in this job. So if i can use the tag generated by taglist to search from, it will be very nice.
Photoreceptor answered 28/12, 2011 at 13:22 Comment(4)
I have asked this question before but haven't get a good answer. So i clarify it in detail this time and will set it bounty after two days.Photoreceptor
If someone can tell me how to search search method in specify class can also solve this problemPhotoreceptor
Very similar question, but for C++ - #5080711Bilow
I am not look for a code completion tool, just specify scope when jumping in tags. ThanksPhotoreceptor
C
3

Depending on how many times you call your methods a couple of * may be enough.

Without using tags, gd can be used to go to the local declaration of the method under your cursor. I tend to choose the most low-tech solution usually, so I would go with this one.

But ctags is also able to generate tags for a single file only or for an arbitrary selection of files. It can be done in a few steps but it's definetely not as straightforward as what you are accustomed to do…

  1. Create a file with the name(s) of the file(s) you want to scan. Let's say it's called files.txt and it's located at the root of your working directory.

  2. Generate your tags file using the -L <file> argument: ctags -L files.txt.

At this point you should have a tags file containing only the tags present in the file(s) specified at step 1.

Generating different tags files for the whole project and for single files may be useful, here. A short script generating a tags file named after the current file and making it the sole tags source may make the whole thing easier.

EDIT

Actually, TagList and TagBar don't generate tags files. The output of the ctags <options> command they run is used internally and parsed with all kinds of regexp to filter by scope or filename or whatever.

Crack answered 28/12, 2011 at 16:48 Comment(2)
Thanks. I understand how taglist works. But i found that the generated tags file have a column called class, so i guess that may have some methods to search tags with specified class name.Photoreceptor
Exactly. I don't think that Vim's way of interacting with ctags allows to choose tags according to all the additional fields available. If you want to use tags for the current file only, you basically have two solutions: either you generate a specific tags file tied to one file or you find a way to filter your large project's tags file.Crack
B
2

Unfortunately this cannot be done using ctags. Ctags does not respect context, it is a pure list of all possible "functions". Try to open a tag file with an editor (e.g. vim) and you will see it is just a list of "functions" (in case of Java they are "methods"). Example:

getDesc src/com/redhat/rhn/internal/doclet/Handler.java /^    public String getDesc() {$/;" m   class:Handler
getDoc  src/com/redhat/rhn/internal/doclet/ApiCall.java /^    public String getDoc() {$/;"  m   class:ApiCall

Vim just search the file "as is" without giving it any context - it just search for a "function". It is able to search for files, classes, methods, enums etc. Tags format is described in more detail here: http://ctags.sourceforge.net/FORMAT

In Vim you have few possibilities. There are several plugins that gives Vim some context sensitivity, but you cannot use tags for that. Vim itself has a feature called OmniComplete and there are few plugins dedicated for Java. Then you can use Ctrl-X Ctrl-O to start a completition. I recommend you to map this to a different key (maybe Ctrl-Space if you like). More info about Java OmniComplete plugins here:

Vim omnicompletion for Java

Eclim (http://eclim.org/) is very comperhensive, but difficult to setup (you need to run Eclipse in the background). JDE script is easier and also robust (http://www.vim.org/scripts/script.php?script_id=1213). And please note IntelliJ IDEA Community Edition (free) also has a very nice Vim plugin that is free to use. But I understand you - Vim is Vim.

Good luck!

Bilow answered 28/12, 2011 at 14:50 Comment(2)
Thanks for your answer. But i found the info listed by TagList is just what i need. If i can search tag displayed by TagList then this problem is solved.Photoreceptor
And i found the completion tags have a priority. That is tags in current file display first and then tags in other file. So if can make tag in other file won't display then it's ok.Photoreceptor
E
1

Not exactly an answer to your question, but it seems like there's no way to do exactly what you need, so, i would recommend you the following: for your Java development in Vim, try eclim.

This tool helps you to use your favorite text editor Vim with power of an Eclipse (IDE). I can't find analogue for tab-completion of :ta, but i know a smart analogue for g] : this is a command :JavaSearchContext. You can map it to something.

For example, if you have two classes A and B, and you have method foo() in each class, then g] will ask you every time you want to jump to foo(), but :JavaSearchContext will always jump to the proper declaration of foo().

Of course, there are many other features.

Erlene answered 3/1, 2012 at 10:50 Comment(1)
Thanks. Maybe it's my fault to require from vim too much. But using tags function that vim currently has can full fill my need although not perfect.Photoreceptor

© 2022 - 2024 — McMap. All rights reserved.