-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Correct formatting of config binder generator #83614
Conversation
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsFixes: #83538
|
...Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingSourceGenerator.Emitter.cs
Show resolved
Hide resolved
...ons.Configuration.Binder/tests/SourceGenerationTests/Baselines/TestBindCallGen.generated.txt
Outdated
Show resolved
Hide resolved
...ons.Configuration.Binder/tests/SourceGenerationTests/Baselines/TestBindCallGen.generated.txt
Outdated
Show resolved
Hide resolved
...ons.Configuration.Binder/tests/SourceGenerationTests/Baselines/TestBindCallGen.generated.txt
Outdated
Show resolved
Hide resolved
...onfiguration.Binder/tests/SourceGenerationTests/ConfingurationBindingSourceGeneratorTests.cs
Outdated
Show resolved
Hide resolved
...onfiguration.Binder/tests/SourceGenerationTests/Baselines/TestConfigureCallGen.generated.txt
Outdated
Show resolved
Hide resolved
...onfiguration.Binder/tests/SourceGenerationTests/Baselines/TestConfigureCallGen.generated.txt
Show resolved
Hide resolved
...onfiguration.Binder/tests/SourceGenerationTests/Baselines/TestConfigureCallGen.generated.txt
Outdated
Show resolved
Hide resolved
...onfiguration.Binder/tests/SourceGenerationTests/Baselines/TestConfigureCallGen.generated.txt
Show resolved
Hide resolved
94c973a
to
fc5263e
Compare
src/libraries/Microsoft.Extensions.Configuration.Binder/gen/TypeSpec.cs
Outdated
Show resolved
Hide resolved
...Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingSourceGenerator.Helpers.cs
Outdated
Show resolved
Hide resolved
...Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingSourceGenerator.Helpers.cs
Outdated
Show resolved
Hide resolved
...Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingSourceGenerator.Emitter.cs
Outdated
Show resolved
Hide resolved
...libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
Show resolved
Hide resolved
AssignmentWithNullCheck = 2, | ||
Declaration = 3, | ||
} | ||
|
||
private sealed class SourceWriter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why we need this class at all. The Regex source generator just uses an IntendedTextWriter:
runtime/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs
Lines 120 to 122 in 967250c
// At this point we'll be emitting code. Create a writer to hold it all. | |
var sw = new StringWriter(); | |
IndentedTextWriter writer = new(sw); |
Why can't we follow the same pattern here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure I'll look into switching to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One issue I find with that class is that it doesn't intercept line breaks in user strings to introduce additional indentation. So it actual breaks once you try to use multi-line string literals:
var sw = new StringWriter();
var writer = new IndentedTextWriter(sw);
writer.WriteLine("{");
writer.Indent++;
writer.WriteLine("""
var x = 42;
return x.ToString();
""");
writer.Indent--;
writer.WriteLine("}");
Console.WriteLine(sw.ToString());
Prints
{
var x = 42;
return x.ToString();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the Regex source generator doesn't use multi-line strings:
runtime/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs
Lines 210 to 218 in 967250c
writer.WriteLine($"/// <summary>Provides a factory for creating <see cref=\"RegexRunner\"/> instances to be used by methods on <see cref=\"Regex\"/>.</summary>"); | |
writer.WriteLine($"private sealed class RunnerFactory : RegexRunnerFactory"); | |
writer.WriteLine($"{{"); | |
writer.WriteLine($" /// <summary>Creates an instance of a <see cref=\"RegexRunner\"/> used by methods on <see cref=\"Regex\"/>.</summary>"); | |
writer.WriteLine($" protected override RegexRunner CreateInstance() => new Runner();"); | |
writer.WriteLine(); | |
writer.WriteLine($" /// <summary>Provides the runner that contains the custom logic implementing the specified regular expression.</summary>"); | |
writer.WriteLine($" private sealed class Runner : RegexRunner"); | |
writer.WriteLine($" {{"); |
When using a CodeWriter
(which derives from IndentedTextWriter
) in the RequestDelegateGenerator, it looks like we don't use multi-line strings either:
However, we do use multi-line strings in the RequestDelegateGenerator when just appending strings together
cc @captainsafia @stephentoub - thoughts on how to generate code here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, but I'd say we should use a component that does not prohibit use of multi-line strings in future iterations. I find that it's easier to write and understand in practice:
Perhaps having a bespoke class in the Common
folder that does support it in all the right ways this might be appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that being able to write both single and multi-line strings conveniently is beneficial and we should permit source generators to do both. Multi-line strings make the code more readable while single lines help with source composition/nesting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally use multiline strings in the RequestDelegateGenerator if the code emitted doesn't depend on any conditions and doesn't have complex indentation requirements. As @eiriktsarpalis mentioned, multi-line strings are a lot harder to read then invocations to the CodeWriter
.
8d72491
to
af4468f
Compare
...Microsoft.Extensions.Configuration.Binder/gen/ConfigurationBindingSourceGenerator.Helpers.cs
Outdated
Show resolved
Hide resolved
142d0c3
to
9f8adbd
Compare
Fixes: #83538
General emitted structure now follows feedback from initial PR, observable by inspecting baseline files: