You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When generating code, how can I properly format auto properties?
What I obtain (because of NormalizeWhitespace is the following:
public int Id
{
get;
set;
}
= 0;
public string Test
{
//...
Instead I would like to obtain:
public int Id { get; set; } = 0;
public string Test { get; set; }
//...
I know I can specify no crlf in the NormalizeWhitespace, but I have to call NormalizeWhitespace 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.
I tried to use the Formatting service but I can't find any 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.
When generating code, how can I properly format auto properties?
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)?
The main driver is because the initializer is in a horrible position.
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.
@CyrusNajmabadi Isn't any way to gave a better control over the single formatting rules
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.
I don't know how much effort is required to write a new overload of the NormalizeWhitespace
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.
How can I do that with Formatter?
@CyrusNajmabadi I just submitted a PR (#49495)
In the description I added the results of the formatting before and after the PR to make it easier for you to verify the intended results.
Activity
CyrusNajmabadi commentedon Nov 18, 2020
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.
raffaeler commentedon Nov 18, 2020
@CyrusNajmabadi Isn't any way to gave a better control over the single formatting rules (as VS / VS Code do)?
The main driver is because the initializer is in a horrible position.
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.
CyrusNajmabadi commentedon Nov 18, 2020
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..
raffaeler commentedon Nov 18, 2020
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.
How can I do that with Formatter?
Thanks @CyrusNajmabadi
CyrusNajmabadi commentedon Nov 18, 2020
There may be no such mechanism to do that.
raffaeler commentedon Nov 19, 2020
@CyrusNajmabadi I just submitted a PR (#49495)
In the description I added the results of the formatting before and after the PR to make it easier for you to verify the intended results.
HTH
Added support to normalize accessor list for a property/indexer/eventโฆ