Skip to content

Formatting auto propertiesย #49468

Closed
Closed

Description

@raffaeler

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.

Suggestions?

Activity

  1. CyrusNajmabadi commented on Nov 18, 2020

    @CyrusNajmabadi
    Contributor

    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.

  2. raffaeler commented on Nov 18, 2020

    @raffaeler
    ContributorAuthor

    @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.

  3. CyrusNajmabadi commented on Nov 18, 2020

    @CyrusNajmabadi
    Contributor

    @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.

    You can use https://github.com/dotnet/roslyn/blob/master/src/Workspaces/Core/Portable/Formatting/Formatter.cs if you want fine-grained control.

    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..

  4. raffaeler commented on Nov 18, 2020

    @raffaeler
    ContributorAuthor

    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

  5. CyrusNajmabadi commented on Nov 18, 2020

    @CyrusNajmabadi
    Contributor

    How can I do that with Formatter?

    There may be no such mechanism to do that.

  6. raffaeler commented on Nov 19, 2020

    @raffaeler
    ContributorAuthor

    @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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions