Skip to content

Group Find-Member formatting by full name #22

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

Merged
merged 1 commit into from
Apr 30, 2022
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
4 changes: 2 additions & 2 deletions module/ClassExplorer.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<SelectionSetName>MemberInfo</SelectionSetName>
</ViewSelectedBy>
<GroupBy>
<ScriptBlock>[ClassExplorer.Internal._Format]::Type($PSItem.ReflectedType)</ScriptBlock>
<ScriptBlock>[ClassExplorer.Internal._Format]::FullType($PSItem.ReflectedType)</ScriptBlock>
<Label>ReflectedType</Label>
</GroupBy>
<TableControl>
Expand Down Expand Up @@ -132,7 +132,7 @@
<TypeName>System.RuntimeType</TypeName>
</ViewSelectedBy>
<GroupBy>
<ScriptBlock>[ClassExplorer.Internal._Format]::Type($PSItem.Namespace)</ScriptBlock>
<ScriptBlock>[ClassExplorer.Internal._Format]::Namespace($PSItem.Namespace)</ScriptBlock>
<Label>Namespace</Label>
</GroupBy>
<TableControl>
Expand Down
14 changes: 14 additions & 0 deletions src/ClassExplorer/Internal/_Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ public static string Type(Type value, int maxLength = -1)
return GetWriter(maxLength).TypeInfo(value).ToString();
}

[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
public static string FullType(Type value, int maxLength = -1)
{
return GetWriter(maxLength)
.TypeInfo(value, isForAttribute: false, isForDefinition: false, fullName: true)
.ToString();
}

[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
public static string Namespace(string value, int maxLength = -1)
{
return GetWriter(maxLength).Namespace(value).ToString();
}

[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
public static string TypeAndParent(Type value, int maxLength = -1)
{
Expand Down
173 changes: 110 additions & 63 deletions src/ClassExplorer/SignatureWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace ClassExplorer;

internal class SignatureWriter
{
private record struct TypeNameSettings(bool IsForAttribute, bool IsForDefinition, bool FullName);

private const string RefStructObsoleteMessage = "Types with embedded references are not supported in this version of your compiler.";

private const string IsReadOnlyAttribute = "System.Runtime.CompilerServices.IsReadOnlyAttribute";
Expand Down Expand Up @@ -728,70 +730,12 @@ public SignatureWriter TypeInfo(Type type, bool isForAttribute)

public SignatureWriter TypeInfo(Type type, bool isForAttribute, bool isForDefinition)
{
if (isForDefinition && type.IsGenericParameter && !Simple)
{
foreach (CustomAttributeData attribute in type.CustomAttributes)
{
Attribute(attribute).Space();
}
}

if (type.UnwrapConstruction() == typeof(Nullable<>))
{
TypeInfo(type.GetGenericArguments()[0]);
return Question();
}

if (type.IsArray)
{
TypeInfo(type.GetElementType()!);
int rank = type.GetArrayRank();
if (rank is 1)
{
return OpenSquare().CloseSquare();
}

OpenSquare().Append(',', rank - 1).CloseSquare();
return this;
}

if (type.IsPointer)
{
TypeInfo(type.GetElementType()!);
Append("*");
return this;
}

string? wellKnownType = GetWellKnownTypeName(type);
if (wellKnownType is not null)
{
return Keyword(wellKnownType);
}

if (type.IsNested && !type.IsGenericParameter)
{
Poly.Assert(type.ReflectedType is not null);
TypeInfo(type.ReflectedType).Dot();
}

if (!TypeHelpers.TryGetNonHereditaryGenericParameters(type, out ReadOnlySpan<Type> genericArgs))
{
if (isForAttribute)
{
return TypeInfo(Regex.Replace(type.Name, "Attribute$", string.Empty, RegexOptions.IgnoreCase));
}

return TypeInfo(type.Name);
}

TypeInfo(RemoveArity(type.Name)).OpenGeneric();
TypeInfo(genericArgs[0], false, isForDefinition);
for (int i = 1; i < genericArgs.Length; i++)
{
Comma().Space().TypeInfo(genericArgs[i], isForAttribute: false, isForDefinition);
}
return TypeInfo(type, isForAttribute, isForDefinition, fullName: false);
}

return CloseGeneric();
public SignatureWriter TypeInfo(Type type, bool isForAttribute, bool isForDefinition, bool fullName)
{
return TypeInfoImpl(type, new TypeNameSettings(isForAttribute, isForDefinition, fullName));
}

public static string? GetWellKnownTypeName(Type type)
Expand Down Expand Up @@ -1937,6 +1881,109 @@ public static string RemoveArity(string name)
return name.Substring(0, index);
}

public SignatureWriter Namespace(string name)
{
string[] parts = name.Split('.');
Escape(_colors.Type).Append(parts[0]);
for (int i = 1; i < parts.Length; i++)
{
Escape(_colors.Reset)
.Dot()
.Escape(_colors.Type)
.Append(parts[i]);
}

return Escape(_colors.Reset);
}

private SignatureWriter TypeInfoImpl(Type type, TypeNameSettings settings)
{
if (settings.IsForDefinition && type.IsGenericParameter && !Simple)
{
foreach (CustomAttributeData attribute in type.CustomAttributes)
{
Attribute(attribute).Space();
}
}

if (type.UnwrapConstruction() == typeof(Nullable<>))
{
TypeInfoImpl(type.GetGenericArguments()[0], settings);
return Question();
}

if (type.IsArray)
{
TypeInfoImpl(type.GetElementType()!, settings);
int rank = type.GetArrayRank();
if (rank is 1)
{
return OpenSquare().CloseSquare();
}

OpenSquare().Append(',', rank - 1).CloseSquare();
return this;
}

if (type.IsPointer)
{
TypeInfoImpl(type.GetElementType()!, settings);
Append("*");
return this;
}

string? wellKnownType = GetWellKnownTypeName(type);
if (wellKnownType is not null)
{
return Keyword(wellKnownType);
}

if (type.IsNested && !type.IsGenericParameter)
{
Poly.Assert(type.ReflectedType is not null);
TypeInfoImpl(type.ReflectedType, settings).Dot();
settings = settings with { FullName = false };
}

if (!TypeHelpers.TryGetNonHereditaryGenericParameters(type, out ReadOnlySpan<Type> genericArgs))
{
return AppendTypeName(type, settings);
}

AppendTypeName(type, settings).OpenGeneric();
settings = settings with { FullName = false, IsForAttribute = false };
TypeInfoImpl(genericArgs[0], settings);
for (int i = 1; i < genericArgs.Length; i++)
{
Comma().Space().TypeInfoImpl(genericArgs[i], settings);
}

return CloseGeneric();
}

private SignatureWriter AppendTypeName(Type type, TypeNameSettings settings)
{
if (settings.FullName && type.Namespace is { Length: > 0 } && !type.IsGenericParameter)
{
Namespace(type.Namespace).Dot();
}

Escape(_colors.Type);

string name = type.Name;
if (type.IsGenericType)
{
name = RemoveArity(type.Name);
}

if (settings.IsForAttribute)
{
name = Regex.Replace(name, "Attribute$", string.Empty);
}

return Append(name).Escape(_colors.Reset);
}

private bool CanWrite(int length, out int remaining)
{
if (_maxLength is -1)
Expand Down