Resharper fluent interface code formatting (align to the dot) - c#

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".

Related

Format document in Visual Studio to insert braces?

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.

Break lines and wrapping in auto formatting of Visual Studio with ReSharper

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.

Making Resharper put a space in-between class/namespace identifier and the curly brace

I've just installed Resharper on my machine, and by default he presents me with the following C# code formatting:
namespace machineLearning{
public class Class1{
}
}
I've tried fiddling with the different options on Options -> C# -> Formatting Style but I can't seem to find what the option to correct this behaviour is. There seems to be no option explicitly or less-explicitly concerning adding a space between the identifier and the following brace.
How to accomplish that?
In Resharper 7 It is under Resharper -> Manage Options -> Code Editing -> C# -> Formatting Style -> Braces Layout -> Method declaration -> (set the value to ) At End of line (K & R Style)

Resharper multiline method invocation parenthesis alignment

Resharper is formatting multiline method calls like this:
foo.Bar(
x,
y
);
I would prefer it to align the closing parenthesis with the first line e.g.:
foo.Bar(
x,
y
);
I've looked through the Resharper code layout options, but cannot see the right setting. Can someone tell me how to achieve this alternative formatting?
In Visual Studio, go to Resharper > Options > Languages > C# > Formatting Style > Other > Indentation > Continuous line indent multiplier and set it to 0.
Can I recommend you take a look at StyleCop, and then StyleCop for ReSharper?
StyleCop allows you to create exceptions or warnings for various types of formatting. The latter project, StyleCop for ReSharper allows ReSharper to automatically implement certain formats. There is quite a steep learning curve initially with SfR, but it is well worthwhile. This is a very under-used tool, and I wish Microsoft would advertise StyleCop more.
Both are Codeplex projects, and can be found at their respective URL's:
http://stylecop.codeplex.com/
http://stylecopforresharper.codeplex.com/
I hope this helps you out somewhat.
In 2016.2 version appeared an option Intent method calls' parenthesis.
It does exactly what you expect:

How to make Visual Studio not put { on a new line?

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

Categories