Resharper: formatting if statements? - c#

I've been using ReSharper a lot recently, and I must say I am enjoying it, although I couldn't find the setting for a certain something.
Take the below code for example, I have these chunks of if statements, infact I think ReSharper might force it to be formatted like this but they're all pushed together.
if (something)
{
// potentially do something
}
if (somethingElse)
{
// potentially do something else
}
if (somethingElseAgain)
{
// potentially do something else again
}
In a usual project, without resharper it would probably be set out better, somethin like the layout shown below maybe?
if (something)
{
// potentially do something
}
if (somethingElse)
{
// potentially do something else
}
if (somethingElseAgain)
{
// potentially do something else again
}
See how much nicer it looks? Now, getting to the actual question, I couldn't find any option to add line breaks after an if statement, only before it? and even if I applied that, it still wouldn't fix the issue I currently have?
Can anyone tell me how I can fix this issue so that when I run code cleanup it actually fixes this?

I checked your example but do not get the same result.
What's your if-statement configuration? Here is a screenshot of mine to compare it with yours:
To access this menu highlighted the if-statements, a brush appears on the right, right click that brush, and select Format selection -> Configure... from the sub-menu.
You may also want to check ReSharper | Options | Code Editing | C# | Formatting Style | Braces Layout...

Related

Can I make Resharper align case clauses in switch statements on one line?

(Note: This, 7-year-old reply refers to a JetBrains request for this feature which was subsequently marked as "done" in 2017. But they've rearranged their Options dialog since then and I cannot find the equivalent. So I am posting again)
I'm typing a switch statement in C#. When the case statements are short, I generally want to line them up in columns because it makes the code easier to grasp at a glance. So for example, I might want it look like this
switch (XyLengthUnit)
{
case LengthUnit.CM: _precision = 6; break;
case LengthUnit.MM: _precision = 3; break;
}
And mostly it works. But every time I get to the end of a line and type the semicolon, Resharper insists on moving the break down to the next line
switch (XyLengthUnit)
{
case LengthUnit.CM: _precision = 6; break;
case LengthUnit.MM: _precision = 3;
break;
}
So I have to hit backspace to fix it. Every time.
I am sure that it is Resharper doing this because when I disable it, the behavior stops and my 'break' statement remains on the same line.
I've gone through every single setting under Resharpers Options >> Code Editing >> C# >> Formatting Style >> Tabs, Indents, Alignment Nothing there seems to change this. I found a few settings under "Line Breaks and Wrapping that seemed appropriate (and related the previously mentioned feature request) but they did not change the behavior
(This is how I have the settings now but I've monkeyed with them extensively. They do not seem to change anything. As soon as I hit semicolon, the break gets moved)
I know that many Resharper settings have "hard breaks" at a certain column and all that but I generally set those to really high numbers (e.g. column 150).
Is there a setting for it that I am missing? Is it possible to make Resharper align things like this?
No, it's not possible right now. The "Place simple case statements" takes effect when there's only one statement inside a case section, where's in your case there are two including the break; statement.

Reading a text file and perform the conditional statements manually

I have a text file - a template file, which has all the imports, parameters, constants, class name, methods etc.
I need to read it manually and wherever needed, need to replace the corresponding value from excel file.
In this within the method we have if, else statements too.
Where i need to read the statement, if the condition statisfies, enter the loop and proceed further. The corresponding else part needs to be ignored.
Any idea\suggestions on how this can be done? the if else needs to be executed manually.
Sample code Example:
if(0==0) {
if(1==0) {
value=0;
} else {
value=1;
}
return value;
} else {
return 0;
}
So in this case, if the first if part returns, I need to skip the last else. Im counting the condition satisfaction by counting the braces.
For those purpose i like to use IronPython. It is a language based on the Dynamic Language Runtime (DLR). This should give you a nice tool to do what you want. More information can be found here: Home of IronPython
To complete the answer, also IronRuby should be a solution for you. But with this i have no experiance...
EDIT
Even with the newest .net framework and Roslyn you have a big amount of possibilities to do what you want. Just read a litte bit about c#-scripting with roslyn.
If IronPython/Ruby aren't desirable options, you may want to look at Roslyn. If that's not an option there's always the System.Reflection.Emit namespace.

How to customize formatting of code that is generated by "Encapsulate Field"?

Previously I am fairly certain that the "Encapsulate Field" command would turn something like the following:
public int SomeNumber;
into the following (what I want from VS 2015):
private int someNumber;
public int SomeNumber {
get { return someNumber; }
set { someNumber = value; }
}
but in Visual Studio 2015 I am seeing the following:
private int someNumber;
public int SomeNumber {
get {
return someNumber;
}
set {
someNumber = value;
}
}
Is there a way to fix this?
This was a design change in VS2015. In previous versions, the refactoring command paid attention to the Tools > Options > Text Editor > C# > Wrapping > "Leave block on single line" option. With it turned on, you'll get the property getter and setter body the way it encoded in the snippet, braces on the same line. The way you like it.
Different in VS2015, it now pays attention to the Tools > Options > Text Editor > C# > Formatting > New Lines > "Place open brace on new line for methods" setting. You get to choose between "egyptian" braces or having the opening brace separate. Neither of which you like.
Accidents happen when Microsoft creates new VS versions, this was not an accident. Whether this was done by "popular demand" is hard to reverse-engineer, I consider it pretty likely since this refactoring is usually done to write a non-trivial getter or setter, the kind that won't fit a single line. Providing us with a choice between all three possible formatting preferences looks like a problem to me, the existing formatting options are not a good match.
Only real option is to let Microsoft know that you are not happy with the change. There is an existing UserVoice article that proposes a change. You can vote for it or write your own. Post a link to it in your question so other SO users can vote.
Will that help you:
-Encapsulate Field Refactoring (C#):
https://msdn.microsoft.com/en-us/library/a5adyhe9.aspx
This website seems to offer a solution a the end of the topic.
Check also this post :
-Different Refactoring style for field encapsulation: how to make Visual Studio change it?
Different Refactoring style for field encapsulation: how to make Visual Studio change it?
This one is corresponding to your question in a certain manner: the question is really related to yours :)
I myself have tried changing the snippet file to suite my file but VS doesn't take effect. What I end up doing is
Encapsulate the fields as usual.
Copy and paste the code into notepad++ and do a Find and Replace.
Find:
(\{)*(\s*)*(get|set)\r\n\s+{\r\n\s+(.*)\r\n\s+\}\s+
Replace with:
$1$3\{$4\}
Paste the result back into VS. VS will format it follow the "Leave block on single line".

Removing an image from a label at runtime

In C#, I have a 2d array of labels who's image I want to change depending on conditions. More specifically, I'd like to get it toggle between a given image and no image at all (turning it into a transparent label) and back again when conditions are met. Currently, to wipe the label clear again, I'm using this:
someLabelArray[i][j].Image = null;
But it always throws a Null Reference Exception, which makes me suspect this isn't the 'right' way to do things. Or perhaps I'm misunderstanding the error? Is there a better way to do this?
I suspect that would be absolutely fine - but that either someLabelArray is null, or someLabelArray[i] is null for whatever value of i you're using.
Simple way to test this: change your code to:
someLabelArray[i][j].Text = "foo";
which obviously has nothing to do with images, and will definitely work if your array is okay. I suspect this will fail in the same way, in which case you need to look carefully at how you're constructing your array.
If this works - well, I'll have another look then :)

What R# setting is reformatting this line?

VS2010 / R#5.1
I have this "line" of code:
With.Mocks(_mocks).Expecting(() => {
_fooServiceMock.Expect(x => x.FooMethod()).Return(fooMockData);
}).Verify(() => {
});
I perform a R# code cleanup, which changes the code as follows:
With.Mocks(_mocks).Expecting(() => { _fooServiceMock.Expect(x => x.FooMethod()).Return(fooMockData); }).Verify(() => { });
That is, it reformats the statement such that it appears entirely on one line.
What IDE/R# setting is responsible for this? What can I change to preserve my line breaks when I perform a R# code cleanup?
I would have thought 'R# / Options / Languages / C# / Formatting Style / Line Breaks and Wrapping / Preserve Existing Formatting / Keep existing line breaks', but that doesn't seem to make any difference.
It's Place simple anonymous method on single line option in Line Breaks and Wrapping category.
Go to Tools --> Options then scroll to the bottom and under Tools (different than the first) go to Code Cleanup. If you do not have a profile to edit then just create one and select your settings. I think for what you are looking to do you want to have reformat code unchecked.
Now the next time you run Code Cleanup it won't move it to one line.
For more help check out
http://www.jetbrains.com/resharper/webhelp/Code_Cleanup__Creating_Custom_Profiles.html
Edit: Noticed this
Reformat code
Reformats you code according to options configurable in ReSharper | Options | Languages | C# | Formatting Style for C# code.

Categories