Reformatting code often doesn't work

I use Ctrl-K, Ctrl-D a lot to format my code, but find that sometimes LinqPad refuses to format. For example, I was working on a sample bit of code (had the Language set to C# Program) and got as far as the following...

void Main() {
List codes = new List {
};
}

class SupportTicketProblemCode {
public bool Active { get; set; }
public string Description { get; set; }
public List ChildProblemCodes { get; set; }
}

At this stage, Ctrl-K, Ctrl-D was formatting the code fine. I then added some extra lines inside the List<> initialiser...

List codes = new List {
new SupportTicketProblemCode{
Active=true,
Description="Jim"
},
};

...and Ctrl-K, Ctrl-D doesn't do anything. There aren't any errors, as I can run the program (which doesn't actually do anything at the moment), but reformatting doesn't work. As you can see, the indentation on the SupportTicketProblemCode object is wrong, and there are missing spaces around the equals sign.

Any idea why Ctrl-K, Ctrl-D fails to format? I don't get any messages, don't get anything.

Thanks

Comments

  • Ah, just realised that the forum code ignores whitespace, so you can't see the incorrect formatting anyway! either way, the question remains the same.
  • LINQPad uses the standard Roslyn formatter, so you'll see the same results in Visual Studio when auto-formatting.

    I think the reason it doesn't format in some cases is that there's too much variation in how constructs such as list initializers are formatted by different programmers, and it doesn't want to mess up a potentially valid format.
  • Hmm, I see what you mean, but I would at least have expected it to add spaces around the the equals signs.
  • Sure. If you report this as a Visual Studio bug, any fixes will find their way into LINQPad too.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!