1

In Rider (basically a standalone ReSharper for those who don't know), I can't figure out why automatic code formatting is placing an empty line between my if statements.

Before formatting:

string output = "";

if (i % 3 == 0) { output += "Fizz"; }
if (i % 5 == 0) { output += "Buzz"; }

if (output == "") { Console.WriteLine(i); } else { Console.WriteLine(output); }

After formatting:

string output = "";

if (i % 3 == 0) { output += "Fizz"; }

if (i % 5 == 0) { output += "Buzz"; }

if (output == "") { Console.WriteLine(i); } else { Console.WriteLine(output); }

I cannot figure out for the life of me the setting that is doing this, but it's quite irritating when you have multiple similar one-line if-statements grouped together and Rider/ReSharper displaces them all the time.

5
  • Correction: Rider is an IDE; ReSharper is a code analysis tool. Commented Dec 4, 2018 at 0:57
  • 1
    File -> Settings -> Editor -> Code Style -> C# -> Blank Lines) Blank Lines in Code: what values do you have for 'After statements with child blocks'? Does it work as expected if you set this value to 0? Commented Dec 4, 2018 at 1:18
  • @John You are correct, but Rider uses the same code analysis techniques and and shares a lot of similarities with ReSharper, because they're both developed by JetBrains. Commented Dec 5, 2018 at 1:07
  • That doesn't make Rider a "standalone ReSharper" though. It just means that they have included their code analysis tools (ReSharper) in their Rider IDE. If it were a standalone ReSharper, it would be able to analyse code but not let you edit or compile it, etc. Commented Dec 5, 2018 at 1:08
  • 1
    @JonathonChase Yes, that works wonderfully. Exactly what I needed, thank you. Feel free to add a question answer below so I can mark it as solved. If not, I'll do it myself just for others with a similar/same problem in future. Commented Dec 5, 2018 at 1:09

1 Answer 1

5

The setting you're looking for can be located by the following navigation: File -> Settings -> Editor -> Code Style -> C# -> Blank Lines

Under the subsection Blank Lines in Code, you're looking for After statements with child blocks. The reason this particular setting is adding lines in your case is the inclusion of braces.

if (i % 3 == 0) { output += "Fizz"; }
if (i % 5 == 0) { output += "Buzz"; }

could also be written as

if (i % 3 == 0) output += "Fizz";
if (i % 5 == 0) output += "Buzz";

The statements would no longer be considered to have child blocks, and therefore be unaffected. If keeping the braces is part of your desired style, you can set the value for After statements with child blocks to 0 and you'll get the formatting behavior you want when using single-line blocks.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I knew something was wrong. I changed the braces settings because I liked the look of it more, but I had no idea it would affect this particular setting. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.