24

Visual studio keeps doing this:

[DataContract] 
public class MyContract 
{
    [DataMember]
    public bool MyBool { get; set; }
    [DataMember]
    public string MyString { get; set; } 
}

I would like this:

[DataContract] 
public class MyContract 
{
    [DataMember] public bool MyBool { get; set; }
    [DataMember] public string MyString { get; set; } 
}

No big deal if the 'public class MyContract' is on the same line as the [DataContract].

Visual studio seems to have a lot of detailed autoformatting options, but I can't find any regarding newlines after attributes. Am I missing something here? Or is it just not available.

EDIT: At very least, I'd like a "don't change what I entered" formatting option, as opposed to a "always insert" or "always remove" newline option. It's super-annoying that it keeps unformatting my code after I type.

Clyde
  • 8,017
  • 11
  • 56
  • 87
  • 1
    I've also ran into this annoyance. VS2013 is out and it seems there's nothing done about it unless I'm missing something, am I? - Have you found a better solution than just Ctrl-Z'ing? – vexe Jan 07 '14 at 11:39
  • 1
    Same for VS 2015 @vexe. – Zev Spitz Sep 12 '15 at 20:10
  • https://developercommunity.visualstudio.com/t/Text-EditorCCode-StyleFormattingNew/1472174 – Yarl Jul 11 '21 at 13:31

6 Answers6

7

What I usually do is hit Ctrl-Z the very moment autoformat jumps in where I don't appreciate it.

I.e., on a closing accolade, which formats a whole class or method. Type the closing accolade, see it changing the way you don't like it and then hit Ctrl-Z. (I know you were looking for an actual option, I don't know if any exists).

Abel
  • 56,041
  • 24
  • 146
  • 247
  • oh! that's actually not too bad. I always kind of assumed that would delete my last actual typing, but you obviously have realized that it doesn't. If there turns out to be no way to do a real configuration, this is a reasonable workaround – Clyde Dec 10 '09 at 13:39
  • 1
    Different programs act differently here. OpenOffice (personal favorite) for instance removes both autoformat *and* last typing, which is a nuisance. In Eclipse it depends on the plugin (often totally ruining the undo-list). Most Microsoft products (Visual Studio, Office) add it as an extra entry in the undo-list. Once you get used to it, it's not so bad indeed ;-) – Abel Dec 10 '09 at 14:21
2

Not sure if it works for attributes, but look under Tools -> Options -> Text Editor -> C# -> Formatting -> Wrapping -> Leave block on single line or Leave statements and member declarations on the same line.

Agent_9191
  • 7,216
  • 5
  • 33
  • 57
  • 2
    No...already have those checked. They do not apply to attributes – Clyde Dec 10 '09 at 14:11
  • It works if the declaration scope is all in one line. works on all sorts of attributes (on method, property, field, class, struct etc.). but as soon as your declaration goes beyond 1 line, the attribute will be wrapped – Cool guy Aug 21 '23 at 09:41
2

ReSharper can do that. It has options for:

  • Place type attribute on same line
  • Place method attribute on same line
  • Place property/indexer/event attribute on same line (this is the one you want)
  • Place field attribute on same line

It costs a few bucks but if you're as obsessive as I am it's worth every penny. ;)

I also remap Ctrl+K, Ctrl+D to ReSharper's silent format code to experience formatting bliss.

Behrooz
  • 1,696
  • 2
  • 32
  • 54
Mike
  • 411
  • 3
  • 8
  • It doesn't change how it's formatting even after I set it to put on the same line. – Louis Hong May 11 '15 at 05:25
  • 3
    I feel kind of bad about the downvote, but honestly I'm not a fan of these "buy this product" answers. The question specifically asks about Visual Studio, not Resharper. I've used Resharper in the past, and I know that it's a good product, but I don't have it now, and am not getting it any time soon. My problem right now is VS. – Kenneth K. Oct 26 '15 at 22:51
0

What worked best for me was to disable auto format on ; and paste. I hardly ever want auto format at these times anyways. This allows you to type out attributes and move them around without pesky interference.

Options > Text Editor > C# > Formatting > General

Settings screenshot

mkatic
  • 141
  • 3
  • 13
-1

It is ReSharper. Extensions/ReSharper/Options/Code Editiong/C#/Formatting Style/ Line Breakes and Wrapping:

Line Breaks and Wrapping: Line Breaks and Wrapping

Adriaan
  • 17,741
  • 7
  • 42
  • 75
-4

Yeah, Ctrl+E, D is your friend. You can optimize the formatting in Text editor options

Jay Zeng
  • 1,423
  • 10
  • 10