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

No global ID counter #9

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Controls/src/BindingSourceGen/BindingCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ private string GenerateBindingMethods(int indent)
{
using var builder = new BidningInterceptorCodeBuilder(indent);

foreach (var binding in _bindings)
for (int i = 0; i < _bindings.Count; i++)
{
builder.AppendSetBindingInterceptor(binding);
builder.AppendSetBindingInterceptor(id: i + 1, _bindings[i]);
}

return builder.ToString();
Expand All @@ -84,13 +84,13 @@ public BidningInterceptorCodeBuilder(int indent = 0)
_indentedTextWriter = new IndentedTextWriter(_stringWriter, "\t") { Indent = indent };
}

public void AppendSetBindingInterceptor(CodeWriterBinding binding)
public void AppendSetBindingInterceptor(int id, CodeWriterBinding binding)
{
AppendBlankLine();

AppendLine(GeneratedCodeAttribute);
AppendInterceptorAttribute(binding.Location);
Append($"public static void SetBinding{binding.Id}");
Append($"public static void SetBinding{id}");
if (binding.SourceType.IsGenericParameter && binding.PropertyType.IsGenericParameter)
{
Append($"<{binding.SourceType}, {binding.PropertyType}>");
Expand Down
3 changes: 0 additions & 3 deletions src/Controls/src/BindingSourceGen/BindingSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class BindingSourceGenerator : IIncrementalGenerator
// Edge cases
// Optimizations

static int _idCounter = 0;
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var bindingsWithDiagnostics = context.SyntaxProvider.CreateSyntaxProvider(
Expand Down Expand Up @@ -119,7 +118,6 @@ static BindingDiagnosticsWrapper GetBindingForGeneration(GeneratorSyntaxContext
}

var codeWriterBinding = new CodeWriterBinding(
Id: ++_idCounter,
Location: sourceCodeLocation,
SourceType: CreateTypeNameFromITypeSymbol(lambdaSymbol.Parameters[0].Type, enabledNullable),
PropertyType: CreateTypeNameFromITypeSymbol(lambdaSymbol.ReturnType, enabledNullable),
Expand Down Expand Up @@ -235,7 +233,6 @@ public sealed record BindingDiagnosticsWrapper(
Diagnostic[] Diagnostics);

public sealed record CodeWriterBinding(
int Id,
SourceCodeLocation Location,
TypeName SourceType,
TypeName PropertyType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public void BuildsWholeDocument()
{
var codeWriter = new BindingCodeWriter();
codeWriter.AddBinding(new CodeWriterBinding(
Id: 1,
Location: new SourceCodeLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeName("global::MyNamespace.MySourceClass", IsNullable: false, IsGenericParameter: false),
PropertyType: new TypeName("global::MyNamespace.MyPropertyClass", IsNullable: false, IsGenericParameter: false),
Expand Down Expand Up @@ -104,8 +103,7 @@ namespace Microsoft.Maui.Controls.Generated
public void CorrectlyFormatsSimpleBinding()
{
var codeBuilder = new BindingCodeWriter.BidningInterceptorCodeBuilder();
codeBuilder.AppendSetBindingInterceptor(new CodeWriterBinding(
Id: 1,
codeBuilder.AppendSetBindingInterceptor(id: 1, new CodeWriterBinding(
Location: new SourceCodeLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeName("global::MyNamespace.MySourceClass", IsNullable: false, IsGenericParameter: false),
PropertyType: new TypeName("global::MyNamespace.MyPropertyClass", IsNullable: false, IsGenericParameter: false),
Expand Down Expand Up @@ -164,8 +162,7 @@ public void CorrectlyFormatsSimpleBinding()
public void CorrectlyFormatsBindingWithoutAnyNullablesInPath()
{
var codeBuilder = new BindingCodeWriter.BidningInterceptorCodeBuilder();
codeBuilder.AppendSetBindingInterceptor(new CodeWriterBinding(
Id: 1,
codeBuilder.AppendSetBindingInterceptor(id: 1, new CodeWriterBinding(
Location: new SourceCodeLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeName("global::MyNamespace.MySourceClass", IsNullable: false, IsGenericParameter: false),
PropertyType: new TypeName("global::MyNamespace.MyPropertyClass", IsNullable: false, IsGenericParameter: false),
Expand Down Expand Up @@ -220,8 +217,7 @@ public void CorrectlyFormatsBindingWithoutAnyNullablesInPath()
public void CorrectlyFormatsBindingWithoutSetter()
{
var codeBuilder = new BindingCodeWriter.BidningInterceptorCodeBuilder();
codeBuilder.AppendSetBindingInterceptor(new CodeWriterBinding(
Id: 1,
codeBuilder.AppendSetBindingInterceptor(id: 1, new CodeWriterBinding(
Location: new SourceCodeLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeName("global::MyNamespace.MySourceClass", IsNullable: false, IsGenericParameter: false),
PropertyType: new TypeName("global::MyNamespace.MyPropertyClass", IsNullable: false, IsGenericParameter: false),
Expand Down Expand Up @@ -274,8 +270,7 @@ public void CorrectlyFormatsBindingWithoutSetter()
public void CorrectlyFormatsBindingWithIndexers()
{
var codeBuilder = new BindingCodeWriter.BidningInterceptorCodeBuilder();
codeBuilder.AppendSetBindingInterceptor(new CodeWriterBinding(
Id: 1,
codeBuilder.AppendSetBindingInterceptor(id: 1, new CodeWriterBinding(
Location: new SourceCodeLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeName("global::MyNamespace.MySourceClass", IsNullable: false, IsGenericParameter: false),
PropertyType: new TypeName("global::MyNamespace.MyPropertyClass", IsNullable: false, IsGenericParameter: false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ public void GenerateSimpleBinding()
label.SetBinding(Label.RotationProperty, static (string s) => s.Length);
""";

var actualBinding = SourceGenHelpers.GetBinding(source) with { Id = 0 }; // TODO: Improve indexing of bindings
var actualBinding = SourceGenHelpers.GetBinding(source);
var expectedBinding = new CodeWriterBinding(
0,
new SourceCodeLocation("", 3, 7),
new TypeName("string", false, false),
new TypeName("int", false, false),
Expand All @@ -42,9 +41,8 @@ public void GenerateBindingWithNestedProperties()
label.SetBinding(Label.RotationProperty, static (Button b) => b.Text.Length);
""";

var actualBinding = SourceGenHelpers.GetBinding(source) with { Id = 0 }; // TODO: Improve indexing of bindings
var actualBinding = SourceGenHelpers.GetBinding(source);
var expectedBinding = new CodeWriterBinding(
0,
new SourceCodeLocation("", 3, 7),
new TypeName("global::Microsoft.Maui.Controls.Button", false, false),
new TypeName("int", false, false),
Expand Down Expand Up @@ -74,9 +72,8 @@ class Foo
}
""";

var actualBinding = SourceGenHelpers.GetBinding(source) with { Id = 0 }; // TODO: Improve indexing of bindings
var actualBinding = SourceGenHelpers.GetBinding(source);
var expectedBinding = new CodeWriterBinding(
0,
new SourceCodeLocation("", 3, 7),
new TypeName("global::Foo", false, false),
new TypeName("int", true, false),
Expand All @@ -103,9 +100,8 @@ public void GenerateBindingWithNullableReferenceSourceWhenNullableEnabled()
label.SetBinding(Label.RotationProperty, static (Button? b) => b?.Text.Length);
""";

var actualBinding = SourceGenHelpers.GetBinding(source) with { Id = 0 }; // TODO: Improve indexing of bindings
var actualBinding = SourceGenHelpers.GetBinding(source);
var expectedBinding = new CodeWriterBinding(
0,
new SourceCodeLocation("", 3, 7),
new TypeName("global::Microsoft.Maui.Controls.Button", true, false),
new TypeName("int", true, false),
Expand Down Expand Up @@ -135,9 +131,8 @@ class Foo
}
""";

var actualBinding = SourceGenHelpers.GetBinding(source) with { Id = 0 }; // TODO: Improve indexing of bindings
var actualBinding = SourceGenHelpers.GetBinding(source);
var expectedBinding = new CodeWriterBinding(
0,
new SourceCodeLocation("", 3, 7),
new TypeName("global::Foo", false, false),
new TypeName("int", true, false),
Expand All @@ -161,9 +156,8 @@ public void GenerateBindingWithNullableSourceReferenceAndNullableReferenceElemen
label.SetBinding(Label.RotationProperty, static (Button? b) => b?.Text?.Length);
""";

var actualBinding = SourceGenHelpers.GetBinding(source) with { Id = 0 }; // TODO: Improve indexing of bindings
var actualBinding = SourceGenHelpers.GetBinding(source);
var expectedBinding = new CodeWriterBinding(
0,
new SourceCodeLocation("", 3, 7),
new TypeName("global::Microsoft.Maui.Controls.Button", true, false),
new TypeName("int", true, false),
Expand Down Expand Up @@ -194,9 +188,8 @@ class Foo
}
""";

var actualBinding = SourceGenHelpers.GetBinding(source) with { Id = 0 };
var actualBinding = SourceGenHelpers.GetBinding(source);
var expectedBinding = new CodeWriterBinding(
0,
new SourceCodeLocation("", 4, 7),
new TypeName("global::Foo", true, false),
new TypeName("int", false, false),
Expand Down Expand Up @@ -227,9 +220,8 @@ class Foo
}
""";

var actualBinding = SourceGenHelpers.GetBinding(source) with { Id = 0 };
var actualBinding = SourceGenHelpers.GetBinding(source);
var expectedBinding = new CodeWriterBinding(
0,
new SourceCodeLocation("", 4, 7),
new TypeName("global::Foo", true, false),
new TypeName("int", true, false),
Expand Down Expand Up @@ -258,9 +250,8 @@ class Foo
}
""";

var actualBinding = SourceGenHelpers.GetBinding(source) with { Id = 0 }; // TODO: Improve indexing of bindings
var actualBinding = SourceGenHelpers.GetBinding(source);
var expectedBinding = new CodeWriterBinding(
0,
new SourceCodeLocation("", 3, 7),
new TypeName("global::Foo", false, false),
new TypeName("int", false, false),
Expand Down Expand Up @@ -291,9 +282,8 @@ class Foo
}
""";

var actualBinding = SourceGenHelpers.GetBinding(source) with { Id = 0 }; // TODO: Improve indexing of bindings
var actualBinding = SourceGenHelpers.GetBinding(source);
var expectedBinding = new CodeWriterBinding(
0,
new SourceCodeLocation("", 4, 7),
new TypeName("global::Foo", false, false),
new TypeName("int", false, false),
Expand Down
Loading