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

3

14

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?

Cryptology answered 28/7, 2015 at 21:22 Comment(13)
The oddly named "Encapsulate field" refactor (oddly named as a public get/set with backing field is not encapsulation) is controlled via a snippet. So you should be able to edit that snippet. See msdn.microsoft.com/en-us/library/ms165392.aspxCyanogen
@DavidArno Thanks, I hadn't realized that code snippets were used outside the scope of shortcut keys. I've just opened up the snippet file "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Refactoring\EncapsulateField.snippet" but it seems to be formatted correctly in there :/Cryptology
i dont know the menu in 2015 but 2013 Tools->Options-> in the left panel TextEditor->c#->Formatting,and from there you will see various option in the general,indentation,newlines options that let you format the code.Bimolecular
@Bimolecular I've searched through there several times; I cannot see any control over generated properties though (unless I'm looking for the wrong thing).Cryptology
they work on all(snippets too :)Bimolecular
@terrybozzio: I tried those settings in VS 2015 RC, and they seem to be ignored when generated via the refactor. FWIW I get the opening {'s on new lines.Mesdemoiselles
what about using propfull then press tab and then tabHumidifier
@HadiHassan Thanks! Actually, propfull works perfectly. I am used to using Ctrl+R,E a lot when developing; I guess I could learn to use that instead... but its a pity that I have to change my ways because VS changed/broke something :(Cryptology
You could open a bug report. Their documentation does say that it should be honouring the snippets found under the Refactoring folder. But it clearly isn't doing that. So my hope is that they will fix this in an update, as long as they are made aware of this problem of course.Autobiographical
@Autobiographical I just sent the "Frown"; looking for an official bug report option; I know it's there somewhere; as soon as I find it I'll report this as a bug. I guess if they don't fix it might be worth somehow creating a custom implementation of "Encapsulate Field" using the VS SDK. Thanks all.Cryptology
Bug report: connect.microsoft.com/VisualStudio/feedback/details/1617595Cryptology
I like VS2015 is aiming for improvement, but reasons like this is why I am still using ReSharper for refactoring, it has options to choose things like this, and by default it does it the way you (and I) like it.Lyrist
@Lyrist interesting; perhaps I should take a look into Resharper as an alternative. ThanksCryptology
V
7

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.

Vesture answered 28/7, 2015 at 21:22 Comment(1)
What about the blank line above the setter; even if I could just make it respect the code formatting for that I would be a lot happier with it. I can't think of a good reason for that blank line and it would mean less manual reformatting.Cryptology
U
0

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

Upchurch answered 4/8, 2015 at 13:42 Comment(3)
Consider putting some of the information from your linked pages into your answer; that way, if someone can't access the link for any reason they can still get some information. It'll help everyone involved!Pathic
@FKunecke Thanks for the advice :) It's edited now :)Upchurch
Thanks; this was suggested previously but my snippet file already appears to be what I want: pasteboard.co/2tyolvl9.png unless I am looking in the wrong file.Cryptology
U
0

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".
Urbannal answered 30/11, 2018 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.