We recently released a feature for C/C++ that has long been requested: Add Include. A lot of work had previously been put into our #include completion listboxes – but that work is moot because now you don’t need to type #includes directives by hand.
The Add Include command is available when you place the caret on a symbol which is declared in a header file that is not directly included in the active file.
If the header that the symbol is defined in is already directly included in the current file, the command is disabled. Otherwise, it is enabled – even though the dependency may already be satisfied by another directive that itself #includes the header. This is by design as many programming style guidelines mandate explicit inclusion of declarations (as opposed to happenstance or indirect inclusion).
For types and functions, Add Include straightforwardly adds an include directive for the header in which the type or function is declared. For variables that are declared in the active file, Add Include acts on the type of the variable (no need to go to the declaration of the variable to add the include for its type). For variables that are not declared in the active file, Add Include operates in the same way as it does for types and functions.
While the command is available in the usual VA Refactor menus, the easiest way to invoke it (at least for keyboardists) is to place the caret on a symbol and press alt+x, r, n.
This product just gets more an more awesome!
I can not imagine writing code without your plugin ever again!
Now if we could eliminate #includes that we don’t actually reference!
add include is great but… there’s always a but! It would be really nice if it copied the case of the filename.
My nicely named “CamelCapsFileNames.h” become rather flat “camelcapsfilenames.h” and they’re a little tricky to ready at times.
I’ve written a piece of VBScript for Visual Studio 2003-2008 which does do some of the work of removing unused includes. I’ve used it a lot but it can be a little long winded. It basically runs through a file (or even every file in a solution!), knocking out the #includes one by one and checking the compile. If you have biiig files then it’s a pain, if you have nice reasonable size classes then it works fine.
If you want a copy then let me know.
@caerwyn: Please contact us via this page:
http://www.wholetomato.com/support/contact.asp
so that we can get more details about the loss of filename case. Thanks.
@caerwyn: We found a reproducible case of this. It will be addressed in the next release cycle. Thanks.
I love your program. Its the best addin made for an IDE. Although recently I ditched windows and Visual studio is no more useful to me.
I am now using Xcode for mac. It would be a dream come true if you guys could bring the same functionality in Xcode.
w00t!
This is great! I use it all the time.
One annoyance (beside the file name case issue mentioned above) is that when I do this on std::string, it adds:
#include
Then I have to edit it to be just for standards conformance.
negritot: The comment system ate part of your comment, but I see the problem and will open a case for it. thanks
One more thing: when it adds an include using a relative path, it uses backslashes for the path separator, leading to non-portable code. For example, instead of this:
#include “..\MyClass.h”
I’d like to see
#include “../MyClass.h”
@negritot: Before using the default path delimiter, VA will look for a delimiter in other #include statements in the current file. If none is found, then VA uses the default. Starting in build 1734, you can change the default via modification of the DefaultAddIncludeDelimiter registry value.
Awesome! Seems like the out-of-the box default should be a forward slash, though.
Idea:
Suggest a minimal set of include files that is needed to compile the current file. This would be useful to clean-up existing source code.
The existing #include lines can be used to find out the places where all of the symbols in the file are declared, and based on the findings a new minimal set of include files can be synthesized that would give access to those same symbols. The new set of include files then can replace the existing #include directives. This could e.g. neatly clean-up include files which are included only because they happen to include another file that has the required declarations.
(This probably sounds easier than it is, but it seems that Add Include already solves a lot of the problems that would come up in such feature.)