I have some lines of code in c# that Resharper indents like this:
Console.WriteLine("Hello");
this.MySuperFunction(
argument1,
argument2,
argument3
);
Console.WriteLine("World");
Due to my personal coding style, I would like the above to appear with the closing parenthesis (or brace) without any indentation, like so:
Console.WriteLine("Hello");
this.MySuperFunction(
argument1,
argument2,
argument3
);
Console.WriteLine("World");
I tried playing with the various options on Resharper, but couldn't find any. Is there a way I can make this work?
It seems this is not currently possible with ReSharper. There is however an open issue that appears to be slated for release with version 9. If you're interested in raising awareness to the issue and hope to ensure it gets included in the release I recommend you create an account and up vote the issue.
Update to 2016.2 version. An option Intent method calls' parenthesis appeared in this version:
Related
R# helps me with removing curly braces around single-statement blocks and I like that. However I usually leave comments there and they're getting deleted every time the the curly braces are removed, despite that it's legal in C# to keep these comments exactly where they are without the curly braces.
The following sample:
if (true)
{
// comment.
Call();
}
Is being reformatted into this:
if (true)
Call();
Instead of this:
if (true)
// comment.
Call();
And I need my comments to be exactly there before each call, not before the if/else statements. How do I prevent R# from deleting my comments when removing those braces?
This issue was fixed in Rider 2019.1, and Resharper on Rider 2019.1 no longer deletes comments within optional braces if they're being removed automatically while applying the formatting style or performing code cleanup.
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.
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.
I cannot seem to find a way to disable the closing bracket to be auto-indented with all the other args. Example (note the spaces in front of closing bracket)
func(
arg1,
arg2
)
I want to have it aligned with the function call.
func(
arg1,
arg2
)
But I cannot find the setting in VS formatting options. I am pretty sure my previous VS instances did not have this auto-setting. Now, when I finish typing arg2 and press ENTER, it auto aligns me to the wrong spot.
EDIT: All autoformatting is disabled and tried pretty much all in Formatting options. Maybe the issue is with extensions? I am using Resharper.
So I finally found the answer, the reason why VS formatting options did not take effect was because the formatting options from ReSharper took precedence.
The setting is in Resharper -> Editor -> Editor Behavior
"Auto-format on closing brace"
"Auto-format on semicolon"
"Auto-insert closing brace"
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: