Skip to content
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
33 changes: 14 additions & 19 deletions src/Controls/src/BindingSourceGen/AccessExpressionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
using System;
using System.Linq;
using System.Text;
namespace Microsoft.Maui.Controls.BindingSourceGen;

namespace Microsoft.Maui.Controls.BindingSourceGen
public static class AccessExpressionBuilder
{
public static class AccessExpressionBuilder
{
public static string Build(string previousExpression, IPathPart nextPart)
=> nextPart switch
{
Cast { TargetType: var targetType } => $"({previousExpression} as {CastTargetName(targetType)})",
ConditionalAccess conditionalAccess => Build(previousExpression: $"{previousExpression}?", conditionalAccess.Part),
IndexAccess { Index: int numericIndex } => $"{previousExpression}[{numericIndex}]",
IndexAccess { Index: string stringIndex } => $"{previousExpression}[\"{stringIndex}\"]",
MemberAccess memberAccess => $"{previousExpression}.{memberAccess.MemberName}",
_ => throw new NotSupportedException($"Unsupported path part type: {nextPart.GetType()}"),
};
public static string Build(string previousExpression, IPathPart nextPart)
=> nextPart switch
{
Cast { TargetType: var targetType } => $"({previousExpression} as {CastTargetName(targetType)})",
ConditionalAccess conditionalAccess => Build(previousExpression: $"{previousExpression}?", conditionalAccess.Part),
IndexAccess { Index: int numericIndex } => $"{previousExpression}[{numericIndex}]",
IndexAccess { Index: string stringIndex } => $"{previousExpression}[\"{stringIndex}\"]",
MemberAccess memberAccess => $"{previousExpression}.{memberAccess.MemberName}",
_ => throw new NotSupportedException($"Unsupported path part type: {nextPart.GetType()}"),
};

private static string CastTargetName(TypeDescription targetType)
=> targetType.IsValueType ? $"{targetType.GlobalName}?" : targetType.GlobalName;
}
private static string CastTargetName(TypeDescription targetType)
=> targetType.IsValueType ? $"{targetType.GlobalName}?" : targetType.GlobalName;
}
13 changes: 4 additions & 9 deletions src/Controls/src/BindingSourceGen/BindingCodeWriter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;

namespace Microsoft.Maui.Controls.BindingSourceGen;

Expand Down Expand Up @@ -77,9 +72,9 @@ private static bool ShouldUseSetter(BindingMode mode, BindableProperty bindableP
}
""";

private readonly List<CodeWriterBinding> _bindings = new();
private readonly List<SetBindingInvocationDescription> _bindings = new();

public void AddBinding(CodeWriterBinding binding)
public void AddBinding(SetBindingInvocationDescription binding)
{
_bindings.Add(binding);
}
Expand Down Expand Up @@ -113,7 +108,7 @@ public BindingInterceptorCodeBuilder(int indent = 0)
_indentedTextWriter = new IndentedTextWriter(_stringWriter, "\t") { Indent = indent };
}

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

Expand Down Expand Up @@ -216,7 +211,7 @@ private void AppendInterceptorAttribute(InterceptorLocation location)
AppendLine($"[InterceptsLocationAttribute(@\"{location.FilePath}\", {location.Line}, {location.Column})]");
}

private void AppendSetterAction(CodeWriterBinding binding, string sourceVariableName = "source", string valueVariableName = "value")
private void AppendSetterAction(SetBindingInvocationDescription binding, string sourceVariableName = "source", string valueVariableName = "value")
{
var assignedValueExpression = valueVariableName;

Expand Down
4 changes: 2 additions & 2 deletions src/Controls/src/BindingSourceGen/BindingSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ static BindingDiagnosticsWrapper GetBindingForGeneration(GeneratorSyntaxContext
return ReportDiagnostics(pathDiagnostics);
}

var codeWriterBinding = new CodeWriterBinding(
var binding = new SetBindingInvocationDescription(
Location: sourceCodeLocation.ToInterceptorLocation(),
SourceType: BindingGenerationUtilities.CreateTypeNameFromITypeSymbol(lambdaSymbol.Parameters[0].Type, enabledNullable),
PropertyType: BindingGenerationUtilities.CreateTypeNameFromITypeSymbol(lambdaTypeInfo.Type, enabledNullable),
Path: new EquatableArray<IPathPart>([.. parts]),
SetterOptions: DeriveSetterOptions(lambdaBody, context.SemanticModel, enabledNullable));
return new BindingDiagnosticsWrapper(codeWriterBinding, new EquatableArray<DiagnosticInfo>([.. diagnostics]));
return new BindingDiagnosticsWrapper(binding, new EquatableArray<DiagnosticInfo>([.. diagnostics]));
}

private static EquatableArray<DiagnosticInfo> VerifyCorrectOverload(InvocationExpressionSyntax invocation, GeneratorSyntaxContext context, CancellationToken t)
Expand Down
5 changes: 2 additions & 3 deletions src/Controls/src/BindingSourceGen/GeneratorDataModels.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

Expand All @@ -11,10 +10,10 @@ public class TrackingNames
}

public sealed record BindingDiagnosticsWrapper(
CodeWriterBinding? Binding,
SetBindingInvocationDescription? Binding,
EquatableArray<DiagnosticInfo> Diagnostics);

public sealed record CodeWriterBinding(
public sealed record SetBindingInvocationDescription(
InterceptorLocation Location,
TypeDescription SourceType,
TypeDescription PropertyType,
Expand Down
2 changes: 0 additions & 2 deletions src/Controls/src/BindingSourceGen/HashCode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

Expand Down
15 changes: 7 additions & 8 deletions src/Controls/src/BindingSourceGen/IsExternalInitCompat.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System.ComponentModel;

namespace System.Runtime.CompilerServices
namespace System.Runtime.CompilerServices;

/// <summary>
/// This dummy class is required to compile records when targeting .NET Standard
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class IsExternalInit
{
/// <summary>
/// This dummy class is required to compile records when targeting .NET Standard
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class IsExternalInit
{
}
}
1 change: 0 additions & 1 deletion src/Controls/src/BindingSourceGen/PathParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;


namespace Microsoft.Maui.Controls.BindingSourceGen;

internal class PathParser
Expand Down
3 changes: 0 additions & 3 deletions src/Controls/src/BindingSourceGen/SetterBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;

namespace Microsoft.Maui.Controls.BindingSourceGen;

public sealed record Setter(string[] PatternMatchingExpressions, string AssignmentStatement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static void CodeIsEqual(string expectedCode, string actualCode)
}
}

internal static void BindingsAreEqual(CodeWriterBinding expectedBinding, CodeGeneratorResult codeGeneratorResult)
internal static void BindingsAreEqual(SetBindingInvocationDescription expectedBinding, CodeGeneratorResult codeGeneratorResult)
{
AssertNoDiagnostics(codeGeneratorResult);
Assert.NotNull(codeGeneratorResult.Binding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class BindingCodeWriterTests
public void BuildsWholeDocument()
{
var codeWriter = new BindingCodeWriter();
codeWriter.AddBinding(new CodeWriterBinding(
codeWriter.AddBinding(new SetBindingInvocationDescription(
Location: new InterceptorLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeDescription("global::MyNamespace.MySourceClass", IsValueType: false, IsNullable: false, IsGenericParameter: false),
PropertyType: new TypeDescription("global::MyNamespace.MyPropertyClass", IsValueType: false, IsNullable: false, IsGenericParameter: false),
Expand Down Expand Up @@ -131,7 +131,7 @@ private static bool ShouldUseSetter(BindingMode mode, BindableProperty bindableP
public void CorrectlyFormatsSimpleBinding()
{
var codeBuilder = new BindingCodeWriter.BindingInterceptorCodeBuilder();
codeBuilder.AppendSetBindingInterceptor(id: 1, new CodeWriterBinding(
codeBuilder.AppendSetBindingInterceptor(id: 1, new SetBindingInvocationDescription(
Location: new InterceptorLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeDescription("global::MyNamespace.MySourceClass", IsValueType: false, IsNullable: false, IsGenericParameter: false),
PropertyType: new TypeDescription("global::MyNamespace.MyPropertyClass", IsValueType: false, IsNullable: false, IsGenericParameter: false),
Expand Down Expand Up @@ -201,7 +201,7 @@ public static void SetBinding1(
public void CorrectlyFormatsBindingWithoutAnyNullablesInPath()
{
var codeBuilder = new BindingCodeWriter.BindingInterceptorCodeBuilder();
codeBuilder.AppendSetBindingInterceptor(id: 1, new CodeWriterBinding(
codeBuilder.AppendSetBindingInterceptor(id: 1, new SetBindingInvocationDescription(
Location: new InterceptorLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeDescription("global::MyNamespace.MySourceClass", IsValueType: false, IsNullable: false, IsGenericParameter: false),
PropertyType: new TypeDescription("global::MyNamespace.MyPropertyClass", IsValueType: false, IsNullable: false, IsGenericParameter: false),
Expand Down Expand Up @@ -267,7 +267,7 @@ public static void SetBinding1(
public void CorrectlyFormatsBindingWithoutSetter()
{
var codeBuilder = new BindingCodeWriter.BindingInterceptorCodeBuilder();
codeBuilder.AppendSetBindingInterceptor(id: 1, new CodeWriterBinding(
codeBuilder.AppendSetBindingInterceptor(id: 1, new SetBindingInvocationDescription(
Location: new InterceptorLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeDescription("global::MyNamespace.MySourceClass", IsNullable: false, IsGenericParameter: false, IsValueType: false),
PropertyType: new TypeDescription("global::MyNamespace.MyPropertyClass", IsNullable: false, IsGenericParameter: false, IsValueType: false),
Expand Down Expand Up @@ -330,7 +330,7 @@ public static void SetBinding1(
public void CorrectlyFormatsBindingWithIndexers()
{
var codeBuilder = new BindingCodeWriter.BindingInterceptorCodeBuilder();
codeBuilder.AppendSetBindingInterceptor(id: 1, new CodeWriterBinding(
codeBuilder.AppendSetBindingInterceptor(id: 1, new SetBindingInvocationDescription(
Location: new InterceptorLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeDescription("global::MyNamespace.MySourceClass", IsNullable: false, IsGenericParameter: false),
PropertyType: new TypeDescription("global::MyNamespace.MyPropertyClass", IsNullable: true, IsGenericParameter: false),
Expand Down Expand Up @@ -404,7 +404,7 @@ public static void SetBinding1(
public void CorrectlyFormatsBindingWithCasts()
{
var codeBuilder = new BindingCodeWriter.BindingInterceptorCodeBuilder();
codeBuilder.AppendSetBindingInterceptor(id: 1, new CodeWriterBinding(
codeBuilder.AppendSetBindingInterceptor(id: 1, new SetBindingInvocationDescription(
Location: new InterceptorLocation(FilePath: @"Path\To\Program.cs", Line: 20, Column: 30),
SourceType: new TypeDescription("global::MyNamespace.MySourceClass", IsNullable: false, IsGenericParameter: false),
PropertyType: new TypeDescription("global::MyNamespace.MyPropertyClass", IsNullable: false, IsGenericParameter: false),
Expand Down
Loading