I'm using VS 2010 + resharper, and i'm tired reformatting bracket indentation in code as i want it. As example if i have code like:
operators.Keys
.ToList()
.ForEach(k => filters
.AddRange(CustomHtmlHelpers.GetIdAndValueListByPrefix(queryString, k)
.Select(t => new QueryFilter()
{
Operation = operators[k],
PropertyName = t.Item1,
Value = t.Item2
})))
And if i put ; in the end VS (or resharper) 'fixes' bracket indentation so code becomes like:
operators.Keys
.ToList()
.ForEach(k => filters
.AddRange(CustomHtmlHelpers.GetIdAndValueListByPrefix(queryString, k)
.Select(t => new QueryFilter()
{
Operation = operators[k],
PropertyName = t.Item1,
Value = t.Item2
})));
Same happens if i use resharper's code cleanup. I probably could turn off automatic code reformatting on ; but i need it in other situations.
I tried changing code formating options both in VS and resharper setting but never got indentation as i want it.
How could i configure vs or resharper so that it would not do more than one tab formating? Or maybe there is other plugin i can use (together with r#) specificly for this purpose?
EDIT: for anyone interested in this problem here is same question in r# forum http://devnet.jetbrains.net/thread/304794 anyone who would like to see better nested code indentation from r# are welcome to vote for it here http://youtrack.jetbrains.net/issue/RSRP-88220
just guessing...
go to ReSharper -> options -> Code Editing -> c# -> Formatting Style -> Other
Search in Align Multiline Constructs and try toggling the state of checkbox "Chained method calls" (I suppose your value is 'checked' for this checkbox).
if not this one, i expect that required setting is somewhere very near :-)
Related
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.)
Resharper allows to replace only one issue. How to replace all same issues in entire proect?
For example, to replace
ctx.Shops.Where(t => t.ShopId == currentBase.ID).Single()
by
ctx.Shops.Single(t => t.ShopId == currentBase.ID)
you should push button as shown in the
How to replace all accurances of Sinle method in entire project?
Version of resharper 9.1 and 10
Hmm, fixing this for entire file/project/solution (aka "fix in scope") does not seem to be supported.
The closest I could think of is selecting Inspection "Replace with single call..." > Find similar issues..., and then navigate between the results and fix manually.
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.
I read a lot of answers about formatting options for fluent indendation.
( Resharper formatting code into a single line and ReSharper fluent indentation and http://youtrack.jetbrains.com/issue/RSRP-88220 ) like this:
mockCrypto.Expect(c => c.Hash("authenticationHashSalt", "ignoring arguments"))
.IgnoreArguments()
.Return("hashed");
But I have not found information about formatting code like this:
kernel.Bind<ICameraController>()
.To<NikonCameraController>()
.NamedLikeFactoryMethod((ICameraFactory f) => f.GetNikonCamera());
mock.Setup(framework => framework.DownloadExists("2.0.0.0"))
.Returns(true)
.AtMostOnce(); // (it's from moq QuickStart)
But the style is very common and I often see it in the documentation for frameworks.
How to set up Resharper auto-formatting for the use of this style?
I'm using next settings in R# in Options -> Code Editing -> C# -> Formatting Style:
Line Breaks and Wrapping -> Arrangement of Member Access Expressions section -> Wrap chained method calls == Chop always
Tabs, Indents, Alignment -> Align Multiple Constructs section -> Chained method calls checked
Result:
Based on https://blog.jetbrains.com/dotnet/2012/11/12/code-formatting-improvements-in-resharper-71/ . Path for Chained method calls was is some version between 7.1 and R# 2018
Unfortunately, Resharper doesn't seem to be able to do this at the moment and it's an issue that's been flagged for quite some time.
As a potentially acceptable trade off, to minimize the pain of Resharper undoing the formatting of existing fluent invocations, you can select "Keep existing line breaks" in Resharper options under "Formatting Style" -> "Line Breaks and Wrapping" -> "Preserve Existing Formatting".
When editing a C# source file, I type
new {
Visual Studio auto-corrects it to
new object{
Is there a way to stop this behavior?
You can configure which characters being typed commit the current intellisense selection. In Tools | Options | Text Editor | C# | IntelliSense.
Remove "{" and ensure committed by the space bar is not checked.
NB. This option is no longer present as of Visual Studio 2015.
I ran into this issue, and the answers above didn't work for me. In my case, it was caused by Resharper, and I addressed it by navigating to Resharper -> Options -> Environment -> Intellisense -> Completing Characters and adding the opening curly brace character "{" to the "Do not complete on" list for C#.
What are you typing before the new {?
I've just tried it and it auto-completes with the object type, so if I type:
Button test = new {
it becomes:
Button test = new Button{
But if I type:
var test = new {
it leaves it alone.
I haven't configured my VS2008 install in any way.
Have you checked your auto-complete options for ReSharper? I just tried this in a new (empty) class with the default ReSharper settings and couldn't duplicate it. What version of studio/ReSharper are you using?
Try typing the left brace first, then going back and typing new. VS2008 does this for me (without ReSharper) and, if I remember, this is what I do. It's less typing than deleting the inserted "object".