Lea Hayes
Lea Hayes

Reputation: 64206

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?

Upvotes: 14

Views: 3958

Answers (3)

anyhingwilldo
anyhingwilldo

Reputation: 21

I myself have tried changing the snippet file to suite my file but VS doesn't take effect. What I end up doing is

  1. Encapsulate the fields as usual.
  2. 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\}
  1. Paste the result back into VS. VS will format it follow the "Leave block on single line".

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941970

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.

Upvotes: 7

DueSouth
DueSouth

Reputation: 117

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 :)

Upvotes: 0

Related Questions