-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Formatting auto properties #49468
Comments
You will have to specifcy the whitespace yourself. When you go through NormalizeWhitespcae you are effectively saying "i do not want to have find-grained control here, just go ensure that hte code is parseable." Alternatively, you could create a PR for normalize whitespace to produce different formatting here as long as it's non contentious. |
@CyrusNajmabadi Isn't any way to gave a better control over the single formatting rules (as VS / VS Code do)? I don't know how much effort is required to write a new overload of the NormalizeWhitespace, but if it is not huge, I can do it. |
Not when using NormalizeWhitespace. the exact purpose of that funciton is to format only such that the resultant code can be parsed. It has no other considerations. So if you use that function, this is what you get. You can use https://github.com/dotnet/roslyn/blob/master/src/Workspaces/Core/Portable/Formatting/Formatter.cs if you want fine-grained control.
You don't need to create a new overload. I'm just saying that you update the existing NormalizeWhitespace to produce a more palatable formatting here for property accessor blocks.. |
As I wrote in the first message, I already tried to use the Formatter, but I found not OptionSet to control exclusively the Auto Properties. Thanks @CyrusNajmabadi |
There may be no such mechanism to do that. |
@CyrusNajmabadi I just submitted a PR (#49495) HTH |
… on a single line when there are no accessors with block body. (#49495) The discussion started here: #49468 The work consisted in: - Added support in the SyntaxNormalizer class for the auto-properties (the ones who have null Body property) - Added more tests for properties including init accessor and initializers - Fixed the two existing tests (property and indexer) to reflect the changes The following comparisons shows the results of the actual tests, before and after this PR. ``` Before: class a { b c { get; } } After: class a { b c { get; } } ``` ``` Before: class a { int X { get; set; } = 2; } After: class a { int X { get; set; } = 2; } ``` ``` Before: class a { int Y { get; set; } = 99; } After: class a { int Y { get; set; } = 99; } ``` ``` Before: class a { int Z { get; } } After: class a { int Z { get; } } ``` ``` Before: class a { int T { get; init; } int R { get => 1; } } After: class a { int T { get; init; } int R { get => 1; } } ``` ``` Before: class a { int Q { get { return 0; } init { } } int R { get => 1; } } After: class a { int Q { get { return 0; } init { } } int R { get => 1; } } ``` ``` Before: class a { int R { get => 1; } } After: class a { int R { get => 1; } } ``` ``` Before: class a { int S => 2; } After: class a { int S => 2; } ``` ``` Before: class a { b this[c d] { get; } } After: class a { b this[c d] { get; } } ```
When generating code, how can I properly format auto properties?
What I obtain (because of
NormalizeWhitespace
is the following:Instead I would like to obtain:
NormalizeWhitespace
, but I have to callNormalizeWhitespace
with spaces and crlf on the root node in order to format the namespace and the class properly. This operation messes up the properties declarations.CSharpFormattingOptions
with the options for the auto properties.I see other people used regexes or replacing string which is, IMO, not the way to go at all.
Suggestions?
The text was updated successfully, but these errors were encountered: