-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Binding SG] Refactor data models (#24462)
* Extracted PathPart into separate file * Changed BindingSGUtilities to extension methods * Split GeneratorDataModels into meaningful parts
- Loading branch information
Showing
8 changed files
with
227 additions
and
223 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/Controls/src/BindingSourceGen/BindingInvocationDescription.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Microsoft.Maui.Controls.BindingSourceGen; | ||
|
||
public sealed record BindingInvocationDescription( | ||
InterceptorLocation Location, | ||
TypeDescription SourceType, | ||
TypeDescription PropertyType, | ||
EquatableArray<IPathPart> Path, | ||
SetterOptions SetterOptions, | ||
bool NullableContextEnabled, | ||
InterceptedMethodType MethodType); | ||
|
||
public sealed record SourceCodeLocation(string FilePath, TextSpan TextSpan, LinePositionSpan LineSpan) | ||
{ | ||
public static SourceCodeLocation? CreateFrom(Location location) | ||
=> location.SourceTree is null | ||
? null | ||
: new SourceCodeLocation(location.SourceTree.FilePath, location.SourceSpan, location.GetLineSpan().Span); | ||
|
||
public Location ToLocation() | ||
{ | ||
return Location.Create(FilePath, TextSpan, LineSpan); | ||
} | ||
|
||
public InterceptorLocation ToInterceptorLocation() | ||
{ | ||
return new InterceptorLocation(FilePath, LineSpan.Start.Line + 1, LineSpan.Start.Character + 1); | ||
} | ||
} | ||
|
||
public sealed record InterceptorLocation(string FilePath, int Line, int Column); | ||
|
||
public sealed record TypeDescription( | ||
string GlobalName, | ||
bool IsValueType = false, | ||
bool IsNullable = false, | ||
bool IsGenericParameter = false) | ||
{ | ||
public override string ToString() | ||
=> IsNullable | ||
? $"{GlobalName}?" | ||
: GlobalName; | ||
} | ||
|
||
public enum InterceptedMethodType | ||
{ | ||
SetBinding, | ||
Create | ||
} | ||
|
||
public sealed record SetterOptions(bool IsWritable, bool AcceptsNullValue = false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 0 additions & 50 deletions
50
src/Controls/src/BindingSourceGen/BindingSourceGeneratorUtilities.cs
This file was deleted.
Oops, something went wrong.
162 changes: 0 additions & 162 deletions
162
src/Controls/src/BindingSourceGen/GeneratorDataModels.cs
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
src/Controls/src/BindingSourceGen/ITypeSymbolExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.Maui.Controls.BindingSourceGen; | ||
|
||
internal static class ITypeSymbolExtensions | ||
{ | ||
internal static bool IsTypeNullable(this ITypeSymbol typeInfo, bool enabledNullable) | ||
{ | ||
if (!enabledNullable && typeInfo.IsReferenceType) | ||
{ | ||
return true; | ||
} | ||
|
||
return typeInfo.IsNullableValueType() || typeInfo.IsNullableReferenceType(); | ||
} | ||
|
||
internal static TypeDescription CreateTypeDescription(this ITypeSymbol typeSymbol, bool enabledNullable) | ||
{ | ||
var isNullable = IsTypeNullable(typeSymbol, enabledNullable); | ||
return new TypeDescription( | ||
GlobalName: GetGlobalName(typeSymbol, isNullable, typeSymbol.IsValueType), | ||
IsNullable: isNullable, | ||
IsGenericParameter: typeSymbol.Kind == SymbolKind.TypeParameter, //TODO: Add support for generic parameters | ||
IsValueType: typeSymbol.IsValueType); | ||
} | ||
|
||
private static bool IsNullableValueType(this ITypeSymbol typeInfo) => | ||
typeInfo is INamedTypeSymbol namedTypeSymbol | ||
&& namedTypeSymbol.IsGenericType | ||
&& namedTypeSymbol.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T; | ||
|
||
private static bool IsNullableReferenceType(this ITypeSymbol typeInfo) => | ||
typeInfo.IsReferenceType && typeInfo.NullableAnnotation == NullableAnnotation.Annotated; | ||
|
||
|
||
private static string GetGlobalName(this ITypeSymbol typeSymbol, bool isNullable, bool isValueType) | ||
{ | ||
if (isNullable && isValueType) | ||
{ | ||
// Strips the "?" from the type name | ||
return ((INamedTypeSymbol)typeSymbol).TypeArguments[0].ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); | ||
} | ||
|
||
return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); | ||
} | ||
} | ||
|
||
internal static class ISymbolExtensions | ||
{ | ||
internal static bool IsAccessible(this ISymbol symbol) => | ||
symbol.DeclaredAccessibility == Accessibility.Public | ||
|| symbol.DeclaredAccessibility == Accessibility.Internal | ||
|| symbol.DeclaredAccessibility == Accessibility.ProtectedOrInternal; | ||
|
||
internal static AccessorKind ToAccessorKind(this ISymbol symbol) | ||
{ | ||
return symbol switch | ||
{ | ||
IFieldSymbol _ => AccessorKind.Field, | ||
IPropertySymbol _ => AccessorKind.Property, | ||
_ => throw new ArgumentException("Symbol is not a field or property.", nameof(symbol)) | ||
}; | ||
} | ||
} | ||
|
||
public enum AccessorKind | ||
{ | ||
Field, | ||
Property, | ||
} |
Oops, something went wrong.