Skip to content

Commit 621a787

Browse files
Group Find-Member formatting by full name (#22)
Fixes #15 Also format namespace grouping of Find-Type better.
1 parent c66b035 commit 621a787

File tree

3 files changed

+126
-65
lines changed

3 files changed

+126
-65
lines changed

module/ClassExplorer.format.ps1xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
<SelectionSetName>MemberInfo</SelectionSetName>
9292
</ViewSelectedBy>
9393
<GroupBy>
94-
<ScriptBlock>[ClassExplorer.Internal._Format]::Type($PSItem.ReflectedType)</ScriptBlock>
94+
<ScriptBlock>[ClassExplorer.Internal._Format]::FullType($PSItem.ReflectedType)</ScriptBlock>
9595
<Label>ReflectedType</Label>
9696
</GroupBy>
9797
<TableControl>
@@ -132,7 +132,7 @@
132132
<TypeName>System.RuntimeType</TypeName>
133133
</ViewSelectedBy>
134134
<GroupBy>
135-
<ScriptBlock>[ClassExplorer.Internal._Format]::Type($PSItem.Namespace)</ScriptBlock>
135+
<ScriptBlock>[ClassExplorer.Internal._Format]::Namespace($PSItem.Namespace)</ScriptBlock>
136136
<Label>Namespace</Label>
137137
</GroupBy>
138138
<TableControl>

src/ClassExplorer/Internal/_Format.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ public static string Type(Type value, int maxLength = -1)
5353
return GetWriter(maxLength).TypeInfo(value).ToString();
5454
}
5555

56+
[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
57+
public static string FullType(Type value, int maxLength = -1)
58+
{
59+
return GetWriter(maxLength)
60+
.TypeInfo(value, isForAttribute: false, isForDefinition: false, fullName: true)
61+
.ToString();
62+
}
63+
64+
[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
65+
public static string Namespace(string value, int maxLength = -1)
66+
{
67+
return GetWriter(maxLength).Namespace(value).ToString();
68+
}
69+
5670
[Hidden, EditorBrowsable(EditorBrowsableState.Never)]
5771
public static string TypeAndParent(Type value, int maxLength = -1)
5872
{

src/ClassExplorer/SignatureWriter.cs

Lines changed: 110 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace ClassExplorer;
1818

1919
internal class SignatureWriter
2020
{
21+
private record struct TypeNameSettings(bool IsForAttribute, bool IsForDefinition, bool FullName);
22+
2123
private const string RefStructObsoleteMessage = "Types with embedded references are not supported in this version of your compiler.";
2224

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

729731
public SignatureWriter TypeInfo(Type type, bool isForAttribute, bool isForDefinition)
730732
{
731-
if (isForDefinition && type.IsGenericParameter && !Simple)
732-
{
733-
foreach (CustomAttributeData attribute in type.CustomAttributes)
734-
{
735-
Attribute(attribute).Space();
736-
}
737-
}
738-
739-
if (type.UnwrapConstruction() == typeof(Nullable<>))
740-
{
741-
TypeInfo(type.GetGenericArguments()[0]);
742-
return Question();
743-
}
744-
745-
if (type.IsArray)
746-
{
747-
TypeInfo(type.GetElementType()!);
748-
int rank = type.GetArrayRank();
749-
if (rank is 1)
750-
{
751-
return OpenSquare().CloseSquare();
752-
}
753-
754-
OpenSquare().Append(',', rank - 1).CloseSquare();
755-
return this;
756-
}
757-
758-
if (type.IsPointer)
759-
{
760-
TypeInfo(type.GetElementType()!);
761-
Append("*");
762-
return this;
763-
}
764-
765-
string? wellKnownType = GetWellKnownTypeName(type);
766-
if (wellKnownType is not null)
767-
{
768-
return Keyword(wellKnownType);
769-
}
770-
771-
if (type.IsNested && !type.IsGenericParameter)
772-
{
773-
Poly.Assert(type.ReflectedType is not null);
774-
TypeInfo(type.ReflectedType).Dot();
775-
}
776-
777-
if (!TypeHelpers.TryGetNonHereditaryGenericParameters(type, out ReadOnlySpan<Type> genericArgs))
778-
{
779-
if (isForAttribute)
780-
{
781-
return TypeInfo(Regex.Replace(type.Name, "Attribute$", string.Empty, RegexOptions.IgnoreCase));
782-
}
783-
784-
return TypeInfo(type.Name);
785-
}
786-
787-
TypeInfo(RemoveArity(type.Name)).OpenGeneric();
788-
TypeInfo(genericArgs[0], false, isForDefinition);
789-
for (int i = 1; i < genericArgs.Length; i++)
790-
{
791-
Comma().Space().TypeInfo(genericArgs[i], isForAttribute: false, isForDefinition);
792-
}
733+
return TypeInfo(type, isForAttribute, isForDefinition, fullName: false);
734+
}
793735

794-
return CloseGeneric();
736+
public SignatureWriter TypeInfo(Type type, bool isForAttribute, bool isForDefinition, bool fullName)
737+
{
738+
return TypeInfoImpl(type, new TypeNameSettings(isForAttribute, isForDefinition, fullName));
795739
}
796740

797741
public static string? GetWellKnownTypeName(Type type)
@@ -1937,6 +1881,109 @@ public static string RemoveArity(string name)
19371881
return name.Substring(0, index);
19381882
}
19391883

1884+
public SignatureWriter Namespace(string name)
1885+
{
1886+
string[] parts = name.Split('.');
1887+
Escape(_colors.Type).Append(parts[0]);
1888+
for (int i = 1; i < parts.Length; i++)
1889+
{
1890+
Escape(_colors.Reset)
1891+
.Dot()
1892+
.Escape(_colors.Type)
1893+
.Append(parts[i]);
1894+
}
1895+
1896+
return Escape(_colors.Reset);
1897+
}
1898+
1899+
private SignatureWriter TypeInfoImpl(Type type, TypeNameSettings settings)
1900+
{
1901+
if (settings.IsForDefinition && type.IsGenericParameter && !Simple)
1902+
{
1903+
foreach (CustomAttributeData attribute in type.CustomAttributes)
1904+
{
1905+
Attribute(attribute).Space();
1906+
}
1907+
}
1908+
1909+
if (type.UnwrapConstruction() == typeof(Nullable<>))
1910+
{
1911+
TypeInfoImpl(type.GetGenericArguments()[0], settings);
1912+
return Question();
1913+
}
1914+
1915+
if (type.IsArray)
1916+
{
1917+
TypeInfoImpl(type.GetElementType()!, settings);
1918+
int rank = type.GetArrayRank();
1919+
if (rank is 1)
1920+
{
1921+
return OpenSquare().CloseSquare();
1922+
}
1923+
1924+
OpenSquare().Append(',', rank - 1).CloseSquare();
1925+
return this;
1926+
}
1927+
1928+
if (type.IsPointer)
1929+
{
1930+
TypeInfoImpl(type.GetElementType()!, settings);
1931+
Append("*");
1932+
return this;
1933+
}
1934+
1935+
string? wellKnownType = GetWellKnownTypeName(type);
1936+
if (wellKnownType is not null)
1937+
{
1938+
return Keyword(wellKnownType);
1939+
}
1940+
1941+
if (type.IsNested && !type.IsGenericParameter)
1942+
{
1943+
Poly.Assert(type.ReflectedType is not null);
1944+
TypeInfoImpl(type.ReflectedType, settings).Dot();
1945+
settings = settings with { FullName = false };
1946+
}
1947+
1948+
if (!TypeHelpers.TryGetNonHereditaryGenericParameters(type, out ReadOnlySpan<Type> genericArgs))
1949+
{
1950+
return AppendTypeName(type, settings);
1951+
}
1952+
1953+
AppendTypeName(type, settings).OpenGeneric();
1954+
settings = settings with { FullName = false, IsForAttribute = false };
1955+
TypeInfoImpl(genericArgs[0], settings);
1956+
for (int i = 1; i < genericArgs.Length; i++)
1957+
{
1958+
Comma().Space().TypeInfoImpl(genericArgs[i], settings);
1959+
}
1960+
1961+
return CloseGeneric();
1962+
}
1963+
1964+
private SignatureWriter AppendTypeName(Type type, TypeNameSettings settings)
1965+
{
1966+
if (settings.FullName && type.Namespace is { Length: > 0 } && !type.IsGenericParameter)
1967+
{
1968+
Namespace(type.Namespace).Dot();
1969+
}
1970+
1971+
Escape(_colors.Type);
1972+
1973+
string name = type.Name;
1974+
if (type.IsGenericType)
1975+
{
1976+
name = RemoveArity(type.Name);
1977+
}
1978+
1979+
if (settings.IsForAttribute)
1980+
{
1981+
name = Regex.Replace(name, "Attribute$", string.Empty);
1982+
}
1983+
1984+
return Append(name).Escape(_colors.Reset);
1985+
}
1986+
19401987
private bool CanWrite(int length, out int remaining)
19411988
{
19421989
if (_maxLength is -1)

0 commit comments

Comments
 (0)