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

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.

Related

Why does the Notepad++ [NULL] character not paste?

I am new to this site, and I don't know if I am providing enough info - I'll do my best =)
If you use Notepad++, then you will know what I am talking about -- When a user loads a .exe into Notepad++, the NUL / \x0 character is replaced by NULL, which has a black background, and white text. I tried pasting it into Visual Studio, hoping to obtain the same output, but it just pasted some spaces...
Does anyone know if this is a certain key-combination, or something? I would like to put the NULL character in replacement of \x0, just like Notepad++ =)
Notepad++ is a rich text editor unlike your regular notepad. It can display custom graphics so common in all modern text editors. While reading a file whenever notepad++ encounters the ASCII code of a null character then instead of displaying nothing it adds the string "NULL" to the UI setting the text background colour to black and text colour to white which is what you are seeing. You can show any custom style in your rich text editor too.
NOTE: This is by no means an efficient solution. I'm clearly traversing a read string 2 times just to take benefit of already present methods. This can be done manually in a single pass. It is just to give a hint about how you can do it. Also I wrote the code carefully but haven't ran it because I don't have the tools at the moment. I apologise for any mistakes let me know I'll update it
Step 1 : Read a text file by line (line ends at '\n') and replace all instances of null character of that line with the string "NUL" using the String.Replace(). Finally append the modified text to your RichTextBox.
Step 2 : Re traverse your read line using String.IndexOf() finding start indexes of each "NUL" word. Using these indexed you select text from RichTextBox and then style that selected text using RichTextBox.SelectionColor and RichTextBox.SelectionBackColor
richTextBoxCursor basically just represents the start index of each line in RichTextBox
StreamReader sr = new StreamReader(#"c:\test.txt" , Encoding.UTF8);
int richTextBoxCursor = 0;
while (!sr.EndOfStream){
richTextBoxCursor = richTextBox.TextLength;
string line = sr.ReadLine();
line = line.Replace(Convert.ToChar(0x0).ToString(), "NUL");
richTextBox.AppendText(line);
i = 0;
while(true){
i = line.IndexOf("NUL", i) ;
if(i == -1) break;
// This specific select function select text start from a certain start index to certain specified character range passed as second parameter
// i is the start index of each found "NUL" word in our read line
// 3 is the character range because "NUL" word has three characters
richTextBox.Select(richTextBoxCursor + i , 3);
richTextBox.SelectionColor = Color.White;
richTextBox.SelectionBackColor = Color.Black;
i++;
}
}
Notepad++ may use custom or special fonts to show these particular characters. This behavior also may not appropriate for all text editors. So, they don't show them.
If you want to write a text editor that visualize these characters, you probably need to implement this behavior programmatically. Seeing notepad++ source can be helpful If you want.
Text editor
As far as I know in order to make Visual Studio display non printable characters you need to install an extension from the marketplace at https://marketplace.visualstudio.com.
One such extension, which I have neither tried nor recomend - I just did a quick search and this is the first result - is
Invisible Character Visualizer.
Having said that, copy-pasting binaries is a risky business.
You may try Edit > Advanced > View White Space first.
Binary editor
To really see what's going on you could use the VS' binary editor: File->Open->(Open with... option)->Binary Editor -> OK
To answer your question.
It's a symbolic representation of 00H double byte.
You're copying and pasting the values. Notepad++ is showing you symbols that replace the representation of those values (because you configured it to do so in that IDE).

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.

Visual Studio 2015 C# -- How to make open curly brace not insert space?

When typing a function like the following:
public bool doSomething()
With the cursor at the end of the line, if I insert a { character, Visual Studio types {, inserting an extra space before the brace. I don't want the extra space. This does not appear to be an option in Text Editor -> C# -> Formatting -> Spacing. If I Ctrl+z it removes the space, so it knows it's a separate operation. I've turned off all auto-format options and it's still auto-formatting. How do I make it stop trying to be smarter than me?
Edit: I've also just discovered that when I use the keyboard shortcut to uncomment a block of code, it adds that space back in. This is also a problem.

resharper code formatting introduces needless linebreak on starting parens

When I have simple wrap enabled on Wrap invocation arguments, I often end up with a formatted line like:
string.Format(
"example string tralala {0} {1}",
foo, bar);
no combination of settings I find prevents this line break other than enabling chop if long. unfortunately that can be a bit of a line hog (it forcibly makes each argument take a line if a function call takes up more than your max word-wrap space).
Is there any solution to this that I am overlooking?
In Resharper 9
Go to Resharper(from Visual Studio Menu)->Options->Code Editing->C#->Formatting Style->Line Breaks and Wrapping
and Uncheck Wrap Long Line from Line Wrapping option
There are a few settings you can use to format your code just the way you like it in re-sharper. in Visual Studio Go: Tools>Options>Re-sharper and click the Options button there again.
Then under the Code Editing section, under c#>Formatting Style. You can use one of the following settings:
Prefer wrap before ( in declaration\invocation
Prefer warp after ( in declaration\invocation
Or set the wrap invocation argument to either (this is most likely the one need setting)
chop always.
chop if too long.
simple wrap.
The un-tick the wrap long lines all together right on top op the *line wrappingsection, willstop-wrapping`, but you'll need to go through these setting to get it just right - or to get it to format the way you code.

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