9

In Resharper 9.2 and Visual Studio 14 (2015), I would like to remove a blank line before a opening bracket on Cleanup Code(Ctrl+E, Ctrl+C). I cannot find a setting for this. Here is some sample code.

What I have:

namespace TestApp.Test

{
    public class Program

    {
        private string _foo;

        private string _bar;

        public string Qux { get; set; }

        private Program()

        {
        }
    }
}

What I want:

namespace TestApp.Test
{
    public class Program
    {
        private string _foo;

        private string _bar;

        public string Qux { get; set; }

        private Program()
        {
        }
    }
}

The options 'Remove blank lines after "{" and before "}" in declaration' and 'Remove blank lines after "{" and before "}" in code' do not work for this issue.

When I set option 'Keep max blank lines in declarations' to '0' the blank line is removed, but also all the blank lines between fields and properties. Therefor, I would like to keep it at '1'.

What I don't want:

namespace TestApp.Test
{
    public class Program
    {
        private string _foo;
        private string _bar;
        public string Qux { get; set; }
        private Program()
        {
        }
    }
}
hvk
  • 430
  • 4
  • 11

3 Answers3

4
  1. Go to Resharper->Options->Code Editing->C#->Formatting Style->Blank Lines
  2. Set 'Keep max blank lines in declarations' to '0'
  3. Set 'Around single line field' to '1'
  4. Enjoy
Dmitrii Zyrianov
  • 2,208
  • 1
  • 21
  • 27
1

Using Visual Studio 2017

in Current Document use shortcut

  • Open Tools > Options or press Alt + T + O
  • Under Environment tab > Keyboard
  • Search for "DeleteBlank" and select Edit.DeleteBlankLines
  • Add a new shortcut for example Ctrl+D,Ctrl+E
  • Assign > OK

select all text and hit the shortcut

enter image description here

in Current Project or Entire Solution use regular expressions

As mentioned here

Visual Studio has an ability to delete empty lines in replace operation using regular expressions.

  • Click Ctrl-H (quick replace)

  • Tick "Use Regular Expressions" or press Alt+E

  • In Find specify ^\s*$\n|\r

  • In Replace box delete everything.

  • Click "Replace All" or Alt+A

enter image description here

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
0

This issue affects me as well, what I do to get around it is do a simple regex find and replace first, then do my ReSharper code cleanup.

Find

(\r?\n)(\r?\n)*(?([^\r\n])\s)*([{])

Replace

$2$3

This isn't really an answer, I would have put it in a comment but SO doesn't allow me to comment yet. So I fully expect this to be flagged and deleted. Hope you see it first, because I would really like your feedback when you find the real solution!

  • We do an auto cleanup on save and I can't ask my colleagues to do this, but maybe I can do this from time to time. I still hope a ReSharper option will be available/made for this. :-) – hvk Oct 28 '15 at 10:08