Skip to content

Commit

Permalink
Merge pull request #9 from simonrozsival/no-global-idcounter
Browse files Browse the repository at this point in the history
No global ID counter
  • Loading branch information
simonrozsival committed Apr 2, 2024
2 parents 283dd8f + c75e0ee commit 7a50f83
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 36 deletions.
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

0 comments on commit 7a50f83

Please sign in to comment.