I'm using VS 2010, and I'm getting constantly annoyed when I type "else" and then it auto returns line and adds braces. Something like:
else
{
}
I cannot imagine I'm the only one who often puts one-liners after the else and prefers no braces. How do I stop this from happening?
I also found this behavior very annoying.
My first attempt involved opening the Templates Explorer (ReSharper / Templates Explorer...), selecting C#, then unchecking "else". This did get rid of the braces. Unfortunately, hitting enter after "else" now just added a space! I was having to hit enter twice. Not quite what I was hoping for.
But if you double-click on the else template it opens up an editor where you can specify what happens. I found that simply removing the braces gave me the functionality I desired - hitting enter after an else was now putting me on a new line, tabbed over under the else - without braces.
To recap, to fix this you need to open Resharper's Templates Explorer, select the C# scope, then double-click on the else template and remove the braces. Your template "snippet" should look like this (just two lines):
else
$END$
This works in Resharper 6.1 and Visual Studio 2008. I have no idea about other versions of the software.
Okay, finally figured out what the hell is doing this. I have ReSharper installed on this machine, and apparently the "else" keyword is a ReSharper autocomplete keyword. So even after I turned off the braces completion, ReSharper was still kicking in and adding them. Rage. In order to stop this, you have to turn off the ReSharper autocomplete.
I don't think there's any way to disable brace auto-complete for specific keywords. If you go to:
Tools -> Options -> Text Editor -> C# -> Formatting
you'll find the various formatting options. It's possible to disable auto complete in general or affect indentation and such, but I don't see any way to disable brace auto complete for specific keywords.
Are you using Productivity Power Tools? You can turn it off in Tools, Options, "Productivity Power Tools", "Automatic Brace Completion". (I know I do)
Related
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.
Update: Since I see this is one of my most looked at questions, now a few years later I should point out what I found to be the most common cause of this problem: bad syntax elsewhere in your code. 100% of the time I've had this happen, it was because I'd forgotten a curly brace for another block of code, or I had a dangling if, or an earlier line of code I didn't finish. Check for IDE errors first for something like ") or } expected"
Original Question:
I feel like this is an all time dumb question to ask, but I have no idea how to fix this and google is turning up nothing.
In visual studio, when I type:
try {
}
VS would automatically reformat it to
try
{
}
But now it's just leaving it at the first one. I assume I accidentally hit a hotkey or something. Help me fix this please?
Tools->Options
Click Text Editor -> Whatever language -> Formatting
Tools->Options->Text Editor->C# (or whatever language)->Formatting->New Lines
Select the options you want.
If you have Power Commands installed you can then go to the Tools->Options->Power Commands and make sure "Format Document on Save" is selected.
Whenever you save the edited file the Curly Braces will be auto aligned.
In addition to the default Visual Studio settings mentioned, for ReSharper users, this is configured via:
ReSharper [menu] -> Options
In the Options dialog, navigate to Environment -> Editor -> Editor Behavior -> "Auto-format on closing brace". See image:
9 times out of 10 this problem is caused by bad code elsewhere on my document. Either I'm missing a ; on a line, or perhaps a closing }. Visual Studio is unable to figure out what I'm trying to write in code and thus it's unable to format. Check for compiler errors, fix them, and then press Ctrl+K then Ctrl + D to make Visual Studio reformat the current document (your hotkeys may vary, depending on the version of Visual Studio and your settings).
I'm using VS 2010 with resharper. If I have a class called ConfigParserTests and then write
new ConfigParser(
the code will auto complete to
new ConfigParserTests()
which is not what I want. Any idea how to disable this nasty feature.
[Edit]
What is want is to write out
new ConfigParser()
without pressing "Esc". In this case I am using TDD so ConfigParser does not exist.
[/Edit]
I have been looking around in both the resharper and VS intellisense menus without finding anything helpful.
Open Options window, navigate to Intellisense > Autopopup. You will see auto-complete options correspond to several cases and categorized according to language:
As you see, you can select for each single case:
Do not display.
Display but do not preselect. (seems proper for your case)
Display and preselect. (default)
Different options may be selected in different cases or languages. For example, you can specify different behaviours in C#:
After dot
After 'new'
In doc comments
Letter and digits
Where value is expected
HTH
It's not clear whether you're complaining about which class name it's filling in, or the parentheses. I'll address both.
If the class you're trying to use is in another namespace, and you haven't added the appropriate using yet, then the code completion is doing just what you would expect -- you told it which namespaces to use, and you didn't tell it to use the one with ConfigParser in it; so it uses the nearest match, as expected.
But ReSharper has shortcuts that can save you work by finding the class and adding the using for you. For this case, I would suggest that you look into the different Ctrl+Space options in ReSharper. You could write new cp <Ctrl+Alt+Space> and ReSharper will give you a pop-up menu asking whether you mean ConfigParser or ConfigParserTests. When you hit Enter to select the one you want, it will add the necessary using to the top of your file, and complete new ConfigParser() with the cursor between the parentheses.
(If the ConfigParser class doesn't exist yet, then that's one of the cases where you don't want code completion. Just type new ConfigParser and then hit Esc before typing your open paren.)
If your problem is that it adds a close paren, be aware that if you type ) ReSharper will not add a second close parenthesis -- it will recognize that you're typing a paren that it already added, so it will just move the cursor to the right. If for some reason you still don't want it to complete the open paren for you, #jdv-Jan de Vaan's answer explains where to change this preference.
When doing TDD you should put the VS intellisense into suggestion mode instead of the normal completion mode.
The difference is described here
Unfortunately Resharper takes over the intellisense so you can not change mode without deactivating resharper.
Select Resharper \ Options. From the list on the left, select Enirionment \ Intellisence \ Completion behavior.
Then disable the checkboxes under "Automatically complete single item with:"
If you are using resharper 6.0 I would upgrade to 6.1 as there were a lot of these sort of preselecting bugs introduced in 6.0 and fixed in 6.1.
CTRL+ALT+Space Bar
Gotta give props to Zain for showing us how to get around these issues when utilizing TDD
http://blogs.msdn.com/b/zainnab/archive/2010/01/22/intellisense-suggestion-mode-vstipedit0012.aspx
Select Resharper \ Options \ Environment\ Intellisence \ Completion Behavior
Then Change the options under “Automatically insert parentheses after completion: can disable all together Really Old Man Voice I want to type my own (), Opening Only, or Young Hip Coder Dude I like resharper to do it all for me man
We have an old project that we maintain that uses brace indenting. Rather than having to change my Visual Studio options every time I switch projects, I'd like to be able to do this quickly and easily.
I'm trying to write a macro in Visual Studio to turn Brace Indenting on and off.
The checkbox is under Text Editor -> C/C++ -> Formatting
Indentation: Indent Braces
This doesn't work:
DTE.Properties("TextEditor", "C/C++").Item("IndentBraces").Value = True
I get "Value does not fall within the expected range."
Any ideas? I haven't been able to find anything on this.
I found the following is the correct macro code to make this work
DTE.Properties("TextEditor", "C/C++ Specific").Item("IndentBraces").Value = True
see: "Determining Names of Property Items in Tools Options Pages" which is only mildly helpful in this situation but gave me enough of a clue to find it.
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.