@@ -18,6 +18,8 @@ namespace ClassExplorer;
18
18
19
19
internal class SignatureWriter
20
20
{
21
+ private record struct TypeNameSettings ( bool IsForAttribute , bool IsForDefinition , bool FullName ) ;
22
+
21
23
private const string RefStructObsoleteMessage = "Types with embedded references are not supported in this version of your compiler." ;
22
24
23
25
private const string IsReadOnlyAttribute = "System.Runtime.CompilerServices.IsReadOnlyAttribute" ;
@@ -728,70 +730,12 @@ public SignatureWriter TypeInfo(Type type, bool isForAttribute)
728
730
729
731
public SignatureWriter TypeInfo ( Type type , bool isForAttribute , bool isForDefinition )
730
732
{
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
+ }
793
735
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 ) ) ;
795
739
}
796
740
797
741
public static string ? GetWellKnownTypeName ( Type type )
@@ -1937,6 +1881,109 @@ public static string RemoveArity(string name)
1937
1881
return name . Substring ( 0 , index ) ;
1938
1882
}
1939
1883
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
+
1940
1987
private bool CanWrite ( int length , out int remaining )
1941
1988
{
1942
1989
if ( _maxLength is - 1 )
0 commit comments