locked
Visual Studio 2015 Auto Formatting Issue RRS feed

  • Question

  • User612096356 posted

    I am using Visual Studio 2015 Professional.  In my current settings, under Text Editor|C#|Formatting|Spacing, I have selected the radio button "Remove spaces before and after binary operators."  

    My problem is that when I declare and create a new object on the same line, the above rule is ignored (regarding the = in the assignment) upon auto formatting.  For example, I get the following:

    StringBuilder sb = new StringBuilder();

    when what I want is:

    StringBuilder sb=new StringBuilder();

    The only way to get it to preserve my spacing (or lack thereof) around the = in the above declaration statement is to check the Ignore spaces in declaration statements checkbox.  This however does not give me a good "snap-to" of my desired formatting when dealing with code written by any number of the knuckle draggers, who enjoy typing, that I must deal with on a regular basis.

    Is this WAD, a bug or is my profile corrupt beyond the ability of /resetuserdata (already tried) to correct?

    Wednesday, August 3, 2016 2:58 PM

All replies

  • User3690988 posted

    I don't think that StringBuilder sb = new StringBuilder(); is considered a binary operator.  If you see the example when you select the Insert or Remove space before and after binary operators, the example shows this when removing:

    void Method()
    {
        int result = 1+2*3;
    }

    And this when adding:

    void Method()
    {
        int result = 1 + 2 * 3;
    }

    Wednesday, August 3, 2016 3:15 PM
  • User612096356 posted

    I would follow with you on that, except when you separate the assignment from the declaration, auto-format will force this:

    StringBuilder sb;
    sb=new StringBuilder();

    EDIT:  And to clarify, if I select Insert spaces before and after binary operators I get this on auto-format:

    StringBuilder sb;
    sb = new StringBuilder();

    Wednesday, August 3, 2016 3:18 PM
  • User3690988 posted

    Now, my head is ready to explode.  According to MSDN Statements. A Declaration Statement is:   int c;

    If I select Ignore spaces in declaration statements, I would think that if I type:

    int               c;

    it would remove extra spaces, but it seems to do the opposite:

                int        c;     // ignore spaces set
                int d;   // ignore spaces not set
    

    I'm baffled.

    Wednesday, August 3, 2016 3:51 PM
  • User612096356 posted

    I'm baffled.
     I would expect checking "Ignore spaces in declaration statements" to leave spacing the way it is, whatever that is, when auto-formatting.

    Wednesday, August 3, 2016 4:03 PM
  • User3690988 posted

     I would expect checking "Ignore spaces in declaration statements" to leave spacing the way it is, whatever that is, when auto-formatting.

    Thanks,  it's not my way of thinking but, I'll buy that.  I can put the Prozac away.

    Wednesday, August 3, 2016 4:07 PM
  • User-166373564 posted

    Hi,

     I would expect checking "Ignore spaces in declaration statements" to leave spacing the way it is, whatever that is, when auto-formatting.

    Go to Tools -> Options, you could try the option below:

    Regards,

    Angie

    Wednesday, August 31, 2016 6:26 AM