Skip to content

Commit

Permalink
feat: ItemTypeAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Oct 3, 2023
1 parent 6f25178 commit 979ef4b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public IEnumerable<string> Pseudoclasses
foreach (var ret2 in ret)
if (ret2 is not null)
yield return ret2;

}
}
public override string ToString() => Name;
Expand Down Expand Up @@ -149,6 +148,20 @@ public bool IsSubclassOf(ITypeInformation? type)
}
}
}
IReadOnlyList<ICustomAttributeInformation>? _customAttributes;
public IReadOnlyList<ICustomAttributeInformation> CustomAttributes
{
get
{
if (_customAttributes is null)
{
_customAttributes = _type.CustomAttributes
.Select(a => new CustomAttributeWrapper(a))
.ToArray();
}
return _customAttributes!;
}
}
}

internal class CustomAttributeWrapper : ICustomAttributeInformation
Expand All @@ -171,10 +184,17 @@ internal class ConstructorArgumentWrapper : IAttributeConstructorArgumentInforma
{
public ConstructorArgumentWrapper(CAArgument ca)
{
Value = ca.Value;
if (ca.Value is ClassSig cs)
{
Value = cs.AssemblyQualifiedName;
}
else
{
Value = ca.Value;
}
}

public object Value { get; }
public object? Value { get; }
}

internal class PropertyWrapper : IPropertyInformation
Expand Down Expand Up @@ -376,6 +396,21 @@ public MethodWrapper(MethodDef method)
public string ReturnTypeFullName { get; }
public string QualifiedReturnTypeFullName { get; }

private IReadOnlyList<ICustomAttributeInformation>? _customAttributes;
public IReadOnlyList<ICustomAttributeInformation> CustomAttributes
{
get
{
if (_customAttributes == null)
{
_customAttributes = _method.CustomAttributes
.Select(a => new CustomAttributeWrapper(a))
.ToArray();
}
return _customAttributes;
}
}

public override string ToString() => Name;
}

Expand All @@ -385,7 +420,7 @@ internal class ParameterWrapper : IParameterInformation

public ParameterWrapper(Parameter param)
{
TypeFullName = param.Name;
TypeFullName = param.Type.FullName;
QualifiedTypeFullName = param.Type.AssemblyQualifiedName;
_type = new Lazy<ITypeInformation?>(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public interface ITypeInformation
bool IsInternal { get; }
IEnumerable<string> EnumValues { get; }
IEnumerable<ITypeInformation> NestedTypes { get; }
IReadOnlyList<ICustomAttributeInformation> CustomAttributes { get; }

bool IsSubclassOf(ITypeInformation? parentType);
}

Expand All @@ -62,6 +64,7 @@ public interface IMethodInformation
IList<IParameterInformation> Parameters { get; }
string ReturnTypeFullName { get; }
string QualifiedReturnTypeFullName { get; }
IReadOnlyList<ICustomAttributeInformation> CustomAttributes { get; }
}

public interface IFieldInformation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,11 @@ private static MetadataType GetOrCreateMetadataType(Dictionary<string, MetadataT
{
return mt;
}
var itemsType = type is null
? null
: type.Methods
.Where(m => !m.IsStatic && m.IsPublic && m.Name == "Add" && m.Parameters.Count == 1 && m.Parameters[0].Type is not null)
.Select(m => GetOrCreateMetadataType(types, m.Parameters[0].Type, m.Parameters[0].QualifiedTypeFullName, m.Parameters[0].TypeFullName))
.FirstOrDefault();
mt = ConvertTypeInfomation(type!, itemsType);
mt = ConvertTypeInfomation(type!, null);
foreach (var key in keys)
{
types[key] = mt;
}
return mt;
}

Expand Down Expand Up @@ -976,5 +974,34 @@ IEnumerable<string> filterLocalRes(MetadataType type, string? currentAssemblyNam

metadata.AddType(Utils.Xaml2006Namespace, typeArguments);
}

// Valorizze items type

var metaTypes = types.Values.ToArray();

foreach (var mt in metaTypes)
{
if (mt.ItemsType is null && mt.Type is { } ti)
{
var attributes = ti.CustomAttributes;
if (attributes.Count > 0)
{
if (attributes.FirstOrDefault(a => a.TypeFullName == "Avalonia.Metadata.ItemTypeAttribute") is { } attribute)
{
if (attribute.ConstructorArguments[0].Value is string name)
{
mt.ItemsType = GetOrCreateMetadataType(types, null, name);
}
}
}
if (mt.ItemsType is null)
{
mt.ItemsType = ti.Methods
.Where(m => !m.IsStatic && m.IsPublic && m.Name == "Add" && m.Parameters.Count == 1 && m.Parameters[0].Type is not null)
.Select(m => GetOrCreateMetadataType(types, m.Parameters[0].Type, m.Parameters[0].QualifiedTypeFullName, m.Parameters[0].TypeFullName))
.FirstOrDefault();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public IEnumerable<string> FilterTypeNames(string? prefix, bool withAttachedProp
{
TriggerCompletionAfterInsert = true,
});
if (parentType?.Properties?.FirstOrDefault(p => p.IsContent) is { } contentProperty)
if (parentType?.ItemsType is null && parentType?.Properties?.FirstOrDefault(p => p.IsContent) is { } contentProperty)
{
parentType = contentProperty.Type;
}
Expand Down

0 comments on commit 979ef4b

Please sign in to comment.