dotnet / dotnet/runtime

[API Proposal]: Add `WriteLineNoTabs[Async]` with no parameters to `IndentedTextWriter`

Open
#125,108 6 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-System.CodeDom
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Background and motivation

`IndentedTextWriter` has a `WriteLine[Async]` methods wich have no parameters. This is similar to how there is a `Console.WriteLine()` with no parameters for the same purpose. However, `IndentedTextWriter` emits indent string when writing, so it has `WriteLineNoTabs[Async]` to avoid that when writing a string. However, there is no counterpart to that overload that just emits a blank line. This can even be observed in BCL source generators, which use this class to emit code, like a regex generator, e.g. for this code:
```cs
using System.Text.RegularExpressions;

public partial class Program
{
[GeneratedRegex("\\w")]
private partial Regex Word { get; }
}
```
Go to implementation part of `Word` and inspect generatd sources with white spaces shown in the IDE. You will encounter blank lines with spaces like this:
Image
This isn't a big deal, but the investment into new API is also minimal

### API Proposal

```diff
namespace System.CodeDom.Compiler;

public class IndentedTextWriter
{
+ public void WriteLineNoTabs();
+ public Task WriteLineNoTabsAsync();
}
```

### API Usage

```csharp
// Somewhere in a source generator:

IndentedTextWriter writer = ...;

foreach (var member in membersToGenerate)
{
WriteMember(writer, member);
writer.WriteLineNoTabs(); // Write a blank line between members for better looking generated code
}
```

### Alternative Designs

- Leave as is
- Change `WriteLine[Async]` methods without parameters to not emit an indent string

### Risks

Minimal

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.