Besides ctags / cscope,
what are good vim plugins for navigating C++ code base?
Ideally, when my cursor is on a variable name, I would like to be able to know:
what is the class of this variable,
what functions can I call on this varaible,
jump to the class of this varaible ...
and do this across multiple namespaces.
What tools do you suggest?
You might look into Eclim, Vim and Eclipse integration. Best of both worlds.
Related
Is there any plugin for VIM that I can use to index an C++ project code base?
I would apreciate functionalities like being capable of specifing a class and and may be a method and see what file/line the method/class is defined.
Regarding code navigation (and completion),
I'd take a look at clang_indexer (and clang_complete) --
ctag understanding of C++ code is quite bad, but universal-ctags has greatly improved the situation ; cscope understanding of C++ is non-existent.
Regarding plugins for C++ coding,
I have a suite for C and C++ programming. It is mainly oriented toward C++ programming, however a few, and unique features can be used in C as well:
context sensitive snippets (they require other plugins I'm maintaining);
a way to jump to a function definition from its declaration (or create it on the fly if it doesn't exists yet) (it used to requires the plugin alternate, which is a must have, however that I've forked it for my own needs) -> :GOTOIMPL;
a little tool that lists functions with a declaration and no definition, or functions with a definition and no declaration (NB: I haven't used it against C static function yet) (it requires ctags).
:Override that searches for overridable functions
:DOX that analyses C++ function signature to generate the appropriate (customizable) doxygen comment (with \param, \throw, ...)
a mapping to include the header file where the symbol under the cursor is defined* (which requires an up-to-date ctags base)
and few other things
Otherwise, I also use:
plugins like project/local_vimrc in order to have project specific settings ;
searchInRuntime to open/jump to files without the need to browse the directories of the current project ;
a refactoring plugin (that still lacks a few things ...) ;
a wrapper around :make in order to do background compiling, and to filter &makeprg results (e.g. pathnames conversions between cygwin posix form and dos form ; application of STLfilt ; etc.) (-> BuildToolWrapper which is stable, but still in an alpha stage) ;
and a few other things which have already been mentioned (alternate, ctags, ...).
Other Plugins.
Other people use c.vim, other templating systems (snipmate & co), pyclewn (that I highly recommend for debugging (with gdb) from within vim), other bracket-surrounding-and-expansion systems, ...
PS: I've answered, slightly differently, a question on the same subject on quora.
cscope is a nice tool for browsing. There is nice tutorial here.
ctags is another nice tool, I use it in my projects. Tutorial here. If you are in Ubuntu, you can install ctags by doing:
apt-get install exuberant-ctags
gtags is another tool.
I use taglist extensively.
The "Tag List" plugin is a source code browser for the Vim editor. It provides an overview of the structure of source code files and allows you to efficiently browse through source code files in different programming languages. It is the top-rated and most-downloaded plugin for the Vim editor.
Is there a way to programmatically enumerate a namespace and its members in C++?
I have a large C++ program which utilizes several namespaces. I am unfamiliar with the codebase, and would like to determine which functions/classes/variables are associated with which namespaces.
My current approach involves simply removing the 'using namespace' directives one by one and checking what breaks during compilation, but I assume there is a much better way to achieve the same goal.
This is not possible in C++.
However, you can use external tools, such as Doxygen, that will create documentation (HTML, and other formats) that will list all the members of your namespaces.
Unfortunately, introspection is NOT one of C++'s big features. There's no way (within the language) to do what you want. You'll need an external code analysis tool (something that can parse the code and build a reference) to do the job. I use cscope for a lot of analysis, but to my knowledge it doesn't really know about namespaces, so probably not the right tool for you.
You can use a C++ front-end (e.g. Elsa) to do the job for you.
Also consider using a good IDE that has a 'Go To Defiinition' functionality (e.g. Microsoft Visual Studio).
You can start by running Doxygen to generate an index of all the functions/classes/namespaces defined in your project. Make sure to edit the settings to generate the index for undocumented symbols.
If you know which namespaces you're looking for, you can just generate a map file (g++ -Wl,-Map,MyMapFile.map). Then search for e.g. MyNamespace:: in the map file.
I am making a c++ program with vi. It has only one file but it's getting kind of big. It would be nice if I could easily see all the functions I created and jump to any one of them without having to search for them. Can vi do this, or is there a similar program that can?
This seems like a dup of Jump to function definition in vim.
To sum up that answer, use ctags, and take a look at Vim and Ctags tips and tricks.
I use a vim plugin to do this :
http://www.vim.org/scripts/script.php?script_id=273
It summarizes classes, struct, function, with jump functionality.
I'm using Vim for a C++ project that I've started working on and I've been spending a lot of time lately browsing through the existing code to get a hang of it. To make the browsing easier, I've setup ctags and cscope in Vim to jump to definitions and find references.
However, I find that neither of them are intelligent enough to know which class a member variable/function belongs to. For example:
class Square;
...
Square a;
...
a.rotate();
Attempting to find the definition of rotate() will bring up member functions from other classes too, like Triangle. I use g] in Vim to bring up a list of matching tags and fortunately ctags lists the associated class for each entry. However, when there are 200 classes with the same member function, it can be tiresome to hunt down the correct tag.
Also, if I am at a function definition:
Square::rotate()
{
...
}
Attempting to find all calls to rotate() using cscope brings up calls to Triangle's and other classes' rotate functions.
Because of this, I find myself jumping to Visual Slickedit every now and then to find the definition or reference to a member function or member variable. Is there any way I can accomplish this in good old Vim?
SrcExpl might be the plugin you needed. Try it.
I've looked for better solutions than cscope in the past, but never found something. In the end perhaps cscope lack of intelligence isn't really that much of a bother.
The problem is that there is no powerful and open intellisense library on the market. Perhaps CodeInsight.
Seems like this would be a good candidate http://vim.wikia.com/wiki/C%2B%2B_code_completion. I had some good luck with it doing similar things in Java. Not entirely sure it gets you everything you're trying to do though.
You could try taglist plugin, though it still to some extend suffers the same problem as the built-in ctag support. It works the same sort of way that SrcExpl does.
Many times when I am watching others code I just want to find where and how a variable is defined. Normally what I do now is look for the type of the variable until I find the definition, that is very time consuming. And I guess that there are some tools that can help me in this rutinary situation. Any suggestion in some tools or commands to help me in this task?.
I know that using a GUI and creating a project this is done automatically I am talking of a way to do this without a GUI. I am working with only text mode. I am running under Linux and I am using C/C++, but suggestions for other languages are welcome.
Thanks a lot.
A possible solution
Michel in one of his comments propose a simple an effective solution define again the variable, in that case in compilation time, the compiler will inform where is the previous definiton. Of course to apply this solution we need to think previously in the locality of the variable.
You've already given the most appropriate tool: an IDE. This is exactly the kind of thing which an IDE excels at. Why would you not want to use an IDE if you're finding development painful without one?
Note that Emacs, Vim etc can work as IDEs - I'm not talking about forcing you the world of GUIs if you want to stay in a text-only situation, e.g. because you're SSHing in.
(I'm really not trying to be rude here. I just think you've discounted the obvious solution without explaining why.)
Edit: OK, you say you're using C++. I'm editing my response. I would use the C preprocessor and then grep for the variable. It will appear in the first place.
cpp -I...(preprocessor options here) file.cpp | grep variable
The C preprocessor will join all the includes that the program uses, and the definition has to be before any usage of that variable in the file. Not a perfect thing, but without an IDE or a complete language description/managing tool, you only have the text.
Another option would be using ctags. It understands the C and C++ syntaxes (among others), and can be searched for variables and functions using command line tools, emacs and vi, among others.
I use cscope and ctags-exuberant religiously. Run it once on my code base and then in Vim, I can use various commands like ^] or [D or [I or similar to find any definitions or declarations for a given word.
This is similar to facilities provided by mega-IDEs like Visual Studio and Eclipse.
Cscope also functions as a stand-alone tool that performs these searches.
I use one of three methods:
I will use CTags to process my source tree (nightly) and then can easily use commands in Vim (or other editors) to jump right to the definition.
I will just use grep (linux) or findstr (windows) to look for all occurrences of the variable name or type. The definition is usually quite obvious.
In Vim, you can just search backward in the scope and often find what you are looking for.
Grep for common patterns for variable declarations. Example: *, &, > or an alphanumeric followed by one or more whitespace characters then the name of the variable. Or variable name followed by zero or more whitespace characters, then a left parenthesis or a semicolon. Unless it was defined under really weird circumstances (like with some kind of macro), it works every time.
In VIM you can use gd to see local variable declarations or gD to see global variable declarations, if they're defined in the current file. Reference Go_to_definition_using_g
You can also use [i to see the definition without jumping to it, or [I to see all occurrences of the variable in all the included files as well, which will naturally show the definition as well.
If you work in Microsoft Visual Studio (which I think you could use for C++ as well, but would require working on a Windows workstation) there's an easily accessible right-click menu option for "Go to Definition...", which will take you to the definition of any currently marked variable, type or method.
if you insist on staying text mode, you can do this with either emacs or vi with the appropriate plug-ins.
But really, move into the 21st century.
EDIT: You commented that you are doing this over SSH because you need the build speed of the remote server cluster.
In that case, mount the drive on your local machine and use an IDE, and just SSH in to kick off a build.