Skip to content
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

Closed
raffaeler opened this issue Nov 18, 2020 · 6 comments
Closed

Formatting auto properties #49468

raffaeler opened this issue Nov 18, 2020 · 6 comments

Comments

@raffaeler
Copy link
Contributor

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?

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 18, 2020
@CyrusNajmabadi
Copy link
Member

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 CyrusNajmabadi added Area-Compilers Question Resolution-Answered The question has been answered and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 18, 2020
@raffaeler
Copy link
Contributor Author

@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
Copy link
Member

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

@raffaeler
Copy link
Contributor Author

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
Copy link
Member

How can I do that with Formatter?

There may be no such mechanism to do that.

@raffaeler
Copy link
Contributor Author

@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

AlekseyTs pushed a commit that referenced this issue Dec 8, 2020
… 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; }
}


```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants