How to prevent Resharper of removing whitespace for new () - c#

Resharper removes whitespace after runinng Code cleanup. What I mean is:
Person person = new (); // there is a whitespace between `new` and `()`:
However the above code after running Code cleanup becomes without whitespace between new and ():
Person person = new(); // no whitespace between `new` and `()`
Is it possbile to keep whitespace between new and () after running Code cleanup of Resharper.
We are using:
Visual Studio 2022
JetBrains ReSharper 2022.2.3

This is controlled by the Resharper option:
Code editing > C# > Formatting Style > Spaces > Before other parentheses > 'new' parentheses

Related

how do i turn off auto-insert of newline after code block?

I might have some degree of OCD, maybe that's why it bothers me when I close a line with ';' below a while{} for{} or other code block and resharper would automatically insert a line break above my line.
I DO NOT want an extra line.
I looked through the Editing option page under braces and new lines, but nothing seems to jump out.
sample:
if (true){
GC.Collect();
}
string s = "Any statement here, ending the line with ';' causes newline above";
Visual Studio (2019) has these formatting settings. I'm assuming you're using VS and resharper isn't doing the formatting.
Tools > Options > Text Editor > C# > Code Style > Formatting > New Line
Find "Place open brace on new line for control blocks". There are a bunch others there as well.
In ReSharper options: Code Editing->C#->Formating Style->Blank Lines - set After statements with child blocks to 0.

Wrong Braces Layout after "Move To Another File"

When using the Refactoring Tool Move To Another File or Move Types Into Matching Files Resharper uses the wrong line break style after the namespace. If I refactor a class with these tools it generates code like so, although my Braces Layout is configured with At next line (BSD style)
namespace My.Namespace { // <- This brace should be on the next line
public class MyClass
{
public MyClass()
{
// ...
}
}
}
When I'm doing a Code Cleanup afterwards, the code gets formatted correctly:
namespace My.Namespace
{
public class MyClass
{
public MyClass()
{
// ...
}
}
}
Anyone know what might cause this behaviour? Why isn't there a line break before the namespace's brace in the first place?
I'm using ReSharper 2018.3.1
There is setting for braces on
ReSharper > Options > Code Editing > C# > Formatting Style > Braces Layout
Can you check Type and namespace declaration setting is set for At Next Line (BSD Style)
I am using ReSharper 2018.2.3 and everything works perfectly.
Update
I updated my ReSharper to 18.3.1 and still everything is OK. Can you also check for VS default options for braces on Tools > Options > Text Editor > C# > Code Style > Formatting > New Lines > New line options for braces.
I am afraid ReSharper can not overriden all Visual Stuido behaviours. In order to avoid this, just turn off all 'Automatically
format ...' options on Tools > Options > Text Editor > C# > Formatting > General, so that it doesn't get in the way.
This fixed my problem:
ReSharper > Options > Code Editing > c# > Formatting Style > Braces Layout
The Setting Empty braces formatting was on Together on the same line. So I changed that to On different lines. After this change it worked.

Why is Resharper breaking line after method opening brace and how to prevent it?

I've been using Resharper 2016 with Visual Studio 2015, and I had code formatted like this:
list.Add(new SomeClass
{
Value = 1,
Name = "some name"
});
And life was beautiful...
But after updating to Visual Studio 2017, and to Resharper 2018, suddenly after hitting ctrl + E + C, and performing built-in R# option "Reformat code", I got this:
list.Add(
new SomeClass
{
Value = 1,
Name = "some name"
});
Which is terrible, because I usually want indentation only after {.
The only exception being single line statement like 1-liner if, where I sometimes skip the curly brace but still have indentation.
I've tried to switch all possible R# options to "simple wrap" but it didn't help. I've tried to set different values, where it made sense for me, but still no success.
Only after switching "revert to old formatting engine" it helped, but it also break other things, so that is not a viable solution for me.
If you select code and invoke with Alt+Enter "Format selection -> Configure", you'll see list of settings which affect formatting of this selection.
If you still have problems, you can reset settings in menu "ReSharper->Manage Options"
p.s. try uncheck "Line breacks and wrapping -> Arrangement of initializers -> prefer wrap after ( in invocation"

Disable automatic closing parenthesis in Visual Studio / Resharper

Visual Studio 2015 with Resharper 10 here. And a very annoying behaviour I have no idea how to get rid of:
If I type this for example
Thread.Sleep(
I get the intellisense list as expected. I then move the selection bar down to TimeSpan.FromMinutes with the cursor down key and hit enter.
Result:
Thread.Sleep( TimeSpan.FromMinutes);
Question: what can I do to not get the ); inserted automatically?
Options that I turned off already:
in VS:
Automatic brace completion
in Resharper:
Auto-insert pair brackets, parentheses and quotes
Auto-insert closing brace
Automatically insert parentheses after completion

ReSharper - force curly braces around single line

Can I configure ReSharper to fix C# code when curly braces are not used to surround a single-line code block, like this:
if (blnSomeCondition)
DoSomething(); // complain
if (blnSomeOtherCondition)
{
DoSomethingElse(); // don't complain
}
Thanks
In the new version of ReSharper (2016.x) is has been moved to the Code Style.
UPD1: for ReSharper 2017.x
UPD2: for ReSharper 2018.x
UPD3: for ReSharper 2019.x
UPD4: for ReSharper 2020.x
Ryan is correct (note however that the his link refers to R# 2.0 help). The specific procedure is as follows:
Go to ReSharper > Options > Languages > C# > Formatting Style > Braces Layout
Set "Braces in "if-else" statement" to "Use braces for multiline"
After saving the changes, select a scope to reformat (could be a code selection, file, folder, project, solution - anything you want).
Choose ReSharper > Tools > Cleanup Code.
Profit.
Remember that Code Cleanup does numerous things and they're not only related to code formatting (see details at http://www.jetbrains.com/resharper/webhelp/Code_Cleanup__Index.html), so use the feature wisely.
Gorohoroh's solution is close, but instead of selecting "Use braces for multiline" I had to select "Add braces" to force it to add the braces in the single-line scenario. And I had to set that in all six dropdowns under "Force Braces" to catch all the scenarios:
What I was really hoping for was to be able to set up ReSharper to do a yellow warning or red error when the rule was violated, but I haven't found a way to do that. Because you're right, Gorohoroh, Cleanup Code does a lot of stuff and I'd like to have the option to look at and fix the rule violations one at a time.
Thanks for your help!
For ReSharper 2016.2.2.
You should edit your profile for cleaning up. The proper item is called Add/Remove braces for single statements in "if-else", "for", "foreach", "while", "do-while", "using". This item can be found within the C# => Code styles item.
If the item is checked, braces will be added; if unchecked, braces will be removed.
Note: adding doesn't work now (don't know why - perhaps, too fresh version of ReSharper). However, if you will uncheck the mentioned item, the existent braces around single statements will not be removed.
UPD: The problem solved, see the first two comments under this post.
You can configure it as seen on the screenshot.
PS: for the ones to have problem seeing Imgur.com urls -> https://ibb.co/b4xijT
Just if it can help somebody else to save time, on one machine of my company, following Sergey advices was not enough despite the great quality of his post.
On concerned setup (VS2017, R# 2019, both up to date) these settings on R# side were already good, but I also had to modify it in Visual Studio options :
In Options window, go to Text editor -> C# -> Code style ->
Formatting -> General,
Check "Perform Additional code cleanup
during formatting" and "Add/remove braces for single-line control
statement"
Well it is moved again. The new place is under the Syntax Style on version ReSharper 2020.01.
Go to ReSharper > Options > Code Editing > C# > Syntax Style > Braces
Here is the screen shot from ReSharper 2020.01

Categories

Resources