Automatic Clean-Up of Code in Visual Studio 2010 - visual-studio-2010

I am wondering, if it is possible, to have some kind of automatic code-clean-up in Visual Studio 2010.
Especially interesting would be:
Auto-Indent
Remove tailing empty lines
Remove unused usings
at special cases, e. g. on save of the file.
I haven't found anything in the options, but maybe I was just kinda blinded... ...afaik in eclipse it was at least possible to intend on save. There should be something like that in Visual Studio?
UPDATE: Thanks for the fast replies, the thing is that I am ware of the keyboard-shortcuts (but nevertheless thanks for mentioning), but I am working through tons of foreign code by now and it would be awesom not having to do it manually for every file :)

Auto-indent can be done with Edit/Advanced/Format Document, or equivalent keyboard shortcut (Ctrl-K, Ctrl-D for me, YMMV).
Tailing empty lines I'm not aware of an automatic solution for, though there may be an extension available (or you could write your own).
Removing unused usings is a built in VS refactoring, and also available in tools like Resharper or DevExpress. (Right click code, then Organize Usings/Remove and sort.)
I'm not aware of a standard way to have these happen automatically for every document save, but you could probably write a macro or VS extension that would make this happen.

Some of the above already exist directly in Visual Studio:
Ctrl - K - D will reformat the document (Edit -> Advanced -> Format Document).
There is a refactoring that deals with using statements (right click in the code, there is an option for "Organise Using").
As for empty lines, I don't know of anything built in.
But tools like Resharper do have code cleanup capabilities that deal with all of the above and more (Resharper menu -> Tools -> Cleanup Code).
None of the above will execute on save by default, but you should be able to write a macro that runs on the save event.

You could try CodeMaid. Its an open source solution for what you are describing.

To Auto-indent you can ue ctrl+k+d pretty much like ctrl+shift+f in eclipse. But for unused usings I dont know any.

Related

Cleanup spacing - C++ / Visual Studio 2010

Is there a built-in feature or available add-on for Visual Studio 2010 that will clean up spacing in C++ code so that annoying blocks like this:
RandomVar=RandomList.RandomMethod();
will become
RandomVar = RandomList.RandomMethod();
(Same goes for spacing in loops, etc.)
...or do I need to do this myself with find/replace and regex?
I think You are looking for Edit->Advanced->Format document. This will re-format your current document according to settings in Tools->Options->Text Editor->c/c++->Formatting.
It's keyboard shortcut Ctrl+E+D in my case.
Although originally for C code formatting lint has been modified to cover C++ and ported to many platforms.
Some lint variants only report inconsistent layout, others can fix it for you. some are free others paid for versions.
This would be an outside the IDE fix as this is a sperate tool (at least traditionally it is!)
This wikipeadia article lists a few possible sources for lint tools
(this was a footer in my orginal question, but as that posed two solutions I extracted it here so you can accept a specifc answer should you find one apropriate)
This is not a proper "Solution" but a possible work-around without the need for finding external tools
In the "Tools" menu pick "Options..."
In this dialog navigate to
"Text Editor -> C/C++ -> Formatting"
Set the layout options as you
would like your code to look.
Click OK.
Now in a unit with "bad"
formatting from the menu select "Edit->Advanced->Format document" and the IDE wil reformat the
document to match your settings.
For C#, VB etc the "Formating" option has several sub nodes that provide a fine grain of control for the sort of spacing options you ask for. The list for C/C++ is flat and very limited by comparison. I suspect there is not enough flexability for the layout you want.
So you probably cannot get the formatting you want directly. How about...
Set the spacing rules you want for C++ in the C# settings
Temporarily adding a C# project to your solution
adding a class to that
emptying it.
paste the C++ in (causing a reformat)
copy back to the original file
rinse and repeat
remove the temporary C# project
I have to say this is not pretty, but given the syntactical similarities between C++ and C# its probably a close match.
The auto-formatting settings for C++ in Eclipse are more extensive than in Visual Studio, so I ended up using that instead.

Visual Assist X: curly braces are moving during refactoring

I use Visual Assist X, build from 05.01.2009, but the same problem occurred in the previous releases as well. (I run it on MSVS 2005)
When I do some refactoring (like extracting a method), everything's fine, but all the curly braces move forward. For example, before refactoring the code looked like this:
while (expr)
{
doSmth();
}
After refactoring:
while (expr)
{
doSmth();
}
So, I need to move manually all the brackets. Sure, the problem is minor, but it becomes annoying when you do a lot of refactoring. Is it a bug or just default settings? So, does anyone know a workaround?
It looks like Visual Assist takes its cue from the Visual Studio brace placement settings. To tweak this option, visit Tools|Options, then navigate to Text Editor, C/C++, Formatting. Make sure "Indent braces" is unticked.
(A related tip for general formatting issues after fiddling around with text is that you can get Visual Studio to do the reformatting for you. Put the cursor on one of the mispositioned braces, press Ctrl+Shift+] (extend selection to matching brace), then press Ctrl+K, Ctrl+F (reformat selection).)
BTW for genuine Visual Assist bugs the forum is a good place to go:
http://www.wholetomato.com/forum/
They seem to be only a small company, but they're generally pretty good at keeping track of open issues.

Visual Studio: Is there an incremental search for the entire solution?

I am very fond of the keyboard shortcuts built into Visual Studio. One of my favorites is Ctrl+i, which triggers the incremental search. It jumps over the text in the current document as I fill in the searchword. After the desired searchword is typed, I use F3 to jump through the matches.
It works fine, except that is is limited to searching in the current document. Also, triggering incremental search, automatically changes the Look In option in the Find And Replace dialog to Current Document.
Is there an incremental search for the entire solution? Is there a keyboard shortcut for it? Or at the very least is there a way to prevent an usage of incremental search from changing the Look In option?
I use Ctrl-Shift-F which is Find in Files. Once I get a list of results, you can hit F8 and Shift-F8 to cycle through them. Ctrl-I does not affect the scope of this search.
Also, after you've found your first match with Ctrl-I, you don't have to hit F3, you can just hit Ctrl-I and Ctrl-Shift-I to cycle through the matches.
In addition, a quick way to jump to a specific file or class is to use something like SonicFileFinder
ReSharper has some really nice navigation and search features, including an incremental Go To Type search which works across the whole solution.
Visual Sidekick performs incremental search over the whole solution. You can search for file names and for symbols names as well. This tool has been a huge time saver for the C++ development we do. It is ridiculous that Visual Sidekick works better than Visual Studio's own IntelliSense. It hasn't been updated in a while though and doesn't support Visual Studio 2010 yet. ReSharper doesn't seem to support C++ though.
Visual Assist X supports many languages (C++, C#, VB, ASP/ASP.NET, HTML, XML, JavaScript, VBScript, XAML) but it offers a lot of refactoring tools that usually don't work with C++ (e.g. Find References) because C++ is so difficult to parse (macros, templates, etc).

Visual Studio keyboard-shortcut for automatically adding the 'using' statement

What is the keyboard-shortcut that expands the menu, from the little red line, and offers the option to have the necessary using statement appended to the top of the file?
Ctrl + . shows the menu. I find this easier to type than the alternative, Alt + Shift + F10.
This can be re-bound to something more familiar by going to Tools > Options > Environment > Keyboard > Visual C# > View.QuickActions
Alt + Shift + F10 will show the menu associated with the smart tag.
I can highly recommend checking out the Visual Studio plugin ReSharper. It has a QuickFix feature that does the same (and a lot more).
But ReSharper doesn't require the cursor to be located on the actual code that requires a new namespace. Say, you copy/paste some code into the source file, and just a few clicks of Alt + Enter, and all the required usings are included.
Oh, and it also makes sure that the required assembly reference is added to your project. Say for example, you create a new project containing NUnit unit tests. The first class you write, you add the [TestFixture] attribute. If you already have one project in your solution that references the NUnit DLL file, then ReSharper is able to see that the TestFixtureAttribute comes from that DLL file, so it will automatically add that assembly reference to your new project.
And it also adds required namespaces for extension methods. At least the ReSharper version 5 beta does. I'm pretty sure that Visual Studio's built-in resolve function doesn't do that.
On the down side, it's a commercial product, so you have to pay for it. But if you work with software commercially, the gained productivity (the plug in does a lot of other cool stuff) outweighs the price tag.
Yes, I'm a fan ;)
In Visual Studio 2010 you will find the keyboard command to resolve namespaces in a command called View.ShowSmartTag. Mine was also mapped to Shift + Alt + F10 which is a lot of hassle - so I usually remap that promptly.
On Pete commenting on ReSharper - yes, for anyone with the budget, ReSharper makes life an absolute pleasure. The fact that it is intelligent enough to resolve dependencies outside the current references, and add them both as usings and references will not only save you countless hours, but also make you forget where all framework classes reside ;-) That is how easy it makes development life... Then we have not even started on ReSharper refactorings yet.
DevExpress' CodeRush offers no assistance on this regard; or nothing that is obvious to me - and DevExpress under non-expert mode is quite forthcoming in what it wants to do for you :-)
Last comment - this IDE feature of resolving dependencies is so mature and refined in the Java IDE world that the bulk of the Internet samples don't even show the imports (using) any more.
This said, Microsoft now finally has something to offer on this regard, but it is also clear to me that Microsoft development (for many of us) has now come full circle - the focus went from source, to visual designers right back to focus being on source again - meaning that the time you spend in a source code view / whether it is C#, VB or XAML is on the up and the amount of dragging and dropping onto 'forms' is on the down. With this basic assumption, it is simple to say that Microsoft should start concentrating on making the editor smarter, keyboard shortcuts easier, and code/error checking and evaluation better - the days of a dumb editor leaving you to google a class to find out in which library it resides are gone (or should be in any case) for most of us.
Context Menu key (one one with the
menu on it, next to the right
Windows key)
Then choose "Resolve"
from the menu. That can be done by
pressing "s".
It's ctrl + . when, for example, you try to type List you need to type < at the end and press ctrl + . for it to work.

Plugin for Visual Studio to Mimic Eclipse's "Open Type" or "Open Resource" Keyboard Access

If you've ever used Eclipse, you've probably noticed the great keyboard shortcuts that let you hit a shortcut key combination, then just type the first few characters of a function, class, filename, etc. It's even smart enough to put open files first in the list.
I'm looking for a similar functionality for Visual Studio 2008. I know there's a findfiles plugin on codeproject, but that one is buggy and a little weird, and doesn't give me access to functions or classes.
Vs11 (maybe 2010 had it too) has the Navigate To... functionality which (on my machine) has the Ctrl+, shortcut.
By the way it understands capitals as camelcase-shortucts (eclipse does so too). For instance type HH to get HtmlHelper.
This isn't exactly the same as Eclipse from your description, but Visual Studio has some similar features out of the box (I've never used Visual Assist X, but it does sound interesting).
The Find ComboBox in the toolbar ends up being a sort of "Visual Studio command line". You can press Ctrl+/ (by default) to set focus there, and Visual Studio will insert an ">" at the beginning of the text (indicating that you want to enter a command instead of search). It even auto-completes as you type, helping you to find commands.
Anyway, to open a file from there, type "open <filename>". It will display any matching files in the drop down as you type (it pulls the list of files from the currently open solution).
To quickly navigate to a function, in the code editor press Ctrl+I to start an incremental search. Then just start typing until you find what you are looking for. Press Escape to cancel the search, or F3 to search again using the same query. As you are typing in the search query, the status bar in the lower left corner will contain what Visual Studio is searching for. Granted, this won't search across multiple files (I've never used Eclipse much, but that sounds like what it does from your description), but hopefully it will help you at least a little bit.
If anyone stumbles upon this thread:
There's a free plugin (created by me) for Visual Studio 2008 that mimics the Eclipse Ctrl+Shift+R Open Resource dialog (note, not the Open Type dialog). It works with any language and/or project type.
You can find it at Visual Studio Gallery.
Some of the neat features are available in Visual Assist X, though not all of them. I've asked on their forums, but they haven't appeared as yet. VAX gets updated regularly on a rough 4 week period for bug fixes and a new feature every couple of months.
If you are looking for an add-in like this to quickly navigate to source files in your project:
try the Visual Studio 2005/2008 add-in SonicFileFinder.
Resharper does this with the Ctrl-N keyword. Unfortunately it doesn't come for free.
Visual Studio doesn't have anything like this feature beyond Find.
Found this thread while searching for Eclipse's Ctrl+Shift+R, and after seeing the Visual Studio Gallery, found the DPack Tools (they are free, and no, I'm not endorsed in any way by them).
But it's exactly what I was searching:
- Alt+U -> File Browser (a la Eclipse Ctrl+Shift+R)
- Alt+M -> Code Browser (Method list in the actual class)
It has more features, but I'm happy with these ones.
I have been using biterScripting along with Visual Studio to do more flexible searching and manipulation.
It can search the entire workspace.
It can search within any project - EVEN IF THAT PROJECT IS NOT LOADED OR EVEN PART OF A WORKSPACE.
It can find things using regular expressions.
AND, ABOVE ALL, it can make bulk changes. For example, want to change the name of a class from CCustomer to CUser, I can do it in just a few command lines - Actually, I have written scripts for things like this I do often. I DON'T HAVE TO CLICK ON EACH INSTANCE AND MANUALLY DO THE CHANGE.
And, it is inexpensive ($0). I downloaded it from http://www.biterscripting.com .
I'm also comming from the Java Development side and was looking for the CTRL+T feature in the Visual Studio. The other answers refer to open file, but since in C# the class name and file name can be different this is not what i was looking for.
With the Class View or the Object Browser you can search for Objects and Classes
[View]->[Class View] or [View]->[Object]

Resources