Is there a way to "Format document" in Visual Studio to insert braces around single-statement blocks for C# code? For example this:
if (x)
y();
... would become something like:
if (x) { y(); }
The auto formatting seems to deal with indentation but not this brace insertion. Is there a way to do it?
Actually, there seems to be something built-in to Visual Studio to do this.
If you go to Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines and make sure that you have Place open braces on new lines for control block checked.
Then, go to your document and use the key combination CTRLKD, this should reformat your document and add the curly braces.
In case you have resharper you can configure it to force braces depending on your criteria.
Than in the existing code press ctrlaltshiftf, it will format whole file. Or select just part of the code, in this case resharper will format just selection
P.S. ctrlaltf opens clean up dialog. You can configure cleanup options.
I gave a vote to Gimly's answer as it is pretty much correct. These things change over time of course. I would have added a comment but I wanted to paste in some images. Location of settings in VS2019 is at:
Uncheck the appropriate check boxes and select OK.
The shortcut key did not work, which is a shame because, I love them! This documentation suggests that you use
CTRL-K, CTRL-E or, you use the broom icon:
.
Neither option worked for me perhaps because, that is not the intention, given the list of options. However, if you mark and cut all your code and then, paste the code back into the file, the new standard is adopted.
Related
The title.
It's in Visual Studio > settings > text editor > c# > advanced > comments.
(Hint: It does not add // when writing comments.)
What this feature is supposed to toggle is while editing a single-line comment, if you insert a newline in the middle, the next line (with remaining text) will continue the comment. For example, inserting a newline at the | in // foo|bar becomes
// foo
// bar
Without this, it would be
// foo
bar
The Split String Literals option that #StriplingWarrior mentioned in the comments is similar but specific to strings. "foo|bar" becomes
"foo" +
"bar"
As for why the option doesn't do anything, StriplingWarrior was also spot on. The code for this is in the Roslyn repository on GitHub, so:
the option is bound to the same setting storage as Split Strings
the feature implementation does use its own setting (it's not meant to be shared)
the setting defaults to true if not set otherwise (so you'll always see that behavior), and per the above misbinding, there's no way to set it to false.
TL;DR: it's a handy feature, and there's a bug that you can't turn it off.
edit: issue opened here
Is there any keyboard shortcut I can use to go up to start of, for example, foreach loop which I'm editing at the moment?
And maybe there is also a shortcut to go to the end of the loop?
Maybe there is something like that when I'm using Resharper, if not in vanilla Visual Studio?
I'm not writing about debugging! Just writing the code.
Sorry, but I can't find anything about this, but it seems like some basic functionality IDE should have...
You can use Ctrl+]: it
Moves the cursor to the matching brace in the document. If the cursor is on an opening >brace, this will move to the corresponding closing brace and vice versa
You can certainly jump between the braces. If you have your cursor either side of one of the braces (at the start or end of your loop) use CTRL + ] to jump to the other one. This will work on any set of braces, not just loops.
Do you mean you want to move the cursor to the starting/ending bracket?
I don't know of one that does this, although you can collapse the loop to see both what happens before and after at the same time.
although, to be fair, if your loop has so many lines that you can't see both brackets at the same time, it might be a good idea to make the code inside the brackets shorter (for example, with an extra method:
foreach (string entry in entryList)
{
AnalyseValue(entry);
}
You can create bookmarks on specific lines of code (Ctrl+K, Ctrl+K) and navigate to your next book or previous bookmark (Next Bookmark => Ctrl+K, Ctrl+N) (Previous Bookmark =>
Ctrl+P)
So this is going to be a kind of strange question, I have never run into this formatting issue with any other programs I have written. I think it is because my if statements has multiple || which needed multiple lines to make it appear more readable.
Notice the dotted line under my else if(). It is getting shifted right for some reason under the if instead of the else.
Is there some kind of formatting setting that can fix this? Or is there a different way I am supposed to do this else if statement? Having all those OR checks is pretty much necessary but I did not wan't to put them all on a single line as that would look real ugly.
Thanks!
Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting -> New Lines
Uncheck everything and save.
Then, CTRL + E, D to format the document.
Also, you're using a huge compound if statement there, which is quite frankly a mess. If you need to check multiple items as starting values, put them all into a list.
List<String> ModelNumberPrefixes = new List<String>();
ModelNumberPrefixes.Add("A1C1C");
ModelNumberPrefixes.Add("A1C1D");
//etc
ModelNumberPrefixes.ForEach(s => {
if (ModelNumber.StartsWith(s)) {
//Whatever you need to do in your big if block
}
});
To format a selection: Ctrl+K, Ctrl+F
To format a document: Ctrl+K, Ctrl+D
See the pre-defined keyboard shortcuts . (These two are Edit.FormatSelection and Edit.FormatDocument.)
I'm working in a C# project and using Visual Studio 2012. When Visual Studio tries to format my code, it breaks lines and make my code look difficult to read. The original code (what looks pretty nice to read for me and my team):
if (list.Any(x => x.Type == (int) EnumType.Customer))
{
}
And when Visual Studio tries to format:
if (
list.Any(
x => x.Type ==
(int) EnumType.Customer))
{
// Other break codes
}
There are a lot of other parts where it is breaking my code. I like auto formatting for some parts, but my question is: Is there a way to disable this break lines from auto formatting in Visual Studio?*
PS: I also have ReSharper installed.
Solution for long lines:
ReSharper, menu Options → Code Editing → C# → Formatting Style → Line Breaks And Wrapping.
And disable Wrap long lines:
And it really makes me crazy!
In ReSharper's settings, in the Languages section, you can change the formatting style of your C# code. The option you're looking to disable is something along the lines of "Indent anonymous method body." You can also look through the options to further customize the formatting style to your preference.
If I have code like this
if(true){
and I add
}
it is transformed into
if (true)
{
}
but I would like it to stay in format
if (true) {
}
Even if I copy code that has if like this, it is transformed into the longer version.
P.S. I understand that it's a C++ and C# standard to use the longer version, however I do come from a different standard, and it's easier for me to use the shorter version.
I bet there is some formatter option that can be turned off or changed.
Go to Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting-> New Lines
Here there are a lot of options. Remove the check on all options here to never put the open bracket on a new line.
EDIT
The section New Line Options for expressions does not relate to placement of code in conjunction with brackets, so those you don't need to touch.
Bear in mind that if you're using Resharper the above procedure won't help, since Resharper overrides the native VS behavior. If that's your case go to Resharper > Options, Code Editing > C# > Formatting Style > Braces Layout, set all top "Braces Layout" options to "At end of line (K&R style)".
Took me a while to figure this out, so I hope it helps someone.
For all the Mac Users out there, this is how I solved it:
Preferences -> Source Code -> Code Formatting -> C# source code -> C# Format -> Edit
There you can change it under the Category: New Lines
You can change that in the options of your Visual Studio. Go to Tools -> Options -> Text Editor -> C# -> formatting -> new lines (or something similar, I only have the german version of visual studio). You can then change where you want to put the brackets into the next line and where you don't. Personally, I removed all the options, but if you want to have a new line e.g. in loops, there are many options to customize this.
Have a look at Tools -> Options then Text Editor -> C# -> Formatting -> New Lines
For C++, the only way I can avoid VC++ putting '{' on new line for namespaces is by unchecking "Automatically format block when I type a }" option:
To do the same thing for CSS you have to go to: Tools > Options > Text Editor > CSS > Advanced > Formatting > Automatic Formatting > Off