dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.04k stars 4.03k forks source link

Formatter should be configurable for a space before and after curly braces in interpolated string #58015

Open jamiehankins opened 2 years ago

jamiehankins commented 2 years ago

Brief description:

In most cases, the IDE determines that there should be a space before and after the curly braces in C# code. It does not determine either way for interpolated strings.

We should have a config that determines how the IDE handles this, and the IDE should actively reformat based on this setting.

Suggested name: csharp_space_around_string_insert Values: false, before, after, before_and_after

Languages applicable:

C#

Code example that the analyzer should report:

For example, if you create an array and add an initialization list, the IDE adds the spaces for you, like this:

var values = new int[] { 1, 2, 3 };

If you intentionally omit the spaces, then do a "Format Document" command, the IDE adds them back.

However, this is treated differently for an interpolated string. The IDE doesn't care how you do it.

For example, if you create an interpolated string like this:

var value1 = $"The first value is { _portName }.";
var value2 = $"The second value is {_baudRate}.";

Notice that they are formatted differently. If you do a "Format Document", neither is changed.

Additional information:

This omission causes inconsistencies in codebases and should be rectified.

Documentation requirements:

When this analyzer is implemented, it must be documented by following the steps at Documentation for IDE CodeStyle analyzers.

jinujoseph commented 2 years ago

Related to #31691