33
44namespace Ubiquity . NET . CodeAnalysis . Utils
55{
6- /// <summary>Custom formatter for <see cref="NamespaceQualifiedName"/></summary>
6+ /// <summary>Custom formatter for <see cref="NamespaceQualifiedName"/> and <see cref="NamespaceQualifiedTypeName"/> </summary>
77 public class NamespaceQualifiedNameFormatter
88 : IFormatProvider
99 , ICustomFormatter < NamespaceQualifiedName >
10+ , ICustomFormatter < NamespaceQualifiedTypeName >
1011 {
1112 /// <summary>Initializes a new instance of the <see cref="NamespaceQualifiedNameFormatter"/> class.</summary>
1213 /// <param name="globalPrefix">Global prefix for the language this formatter will produce</param>
1314 /// <param name="aliasMap">Map of aliases for the language (Key is the full namespace name of a type, the values are the alias)</param>
1415 public NamespaceQualifiedNameFormatter ( string globalPrefix , IReadOnlyDictionary < string , string > aliasMap )
1516 {
1617 GlobalPrefix = globalPrefix ?? string . Empty ;
17- AliasMap = aliasMap ?? throw new ArgumentNullException ( nameof ( aliasMap ) ) ;
18+ AliasMap = aliasMap ?? throw new ArgumentNullException ( nameof ( aliasMap ) ) ;
1819 }
1920
2021 /// <summary>Gets the global prefix for the language formatting (example: "global::")</summary>
@@ -30,12 +31,24 @@ public NamespaceQualifiedNameFormatter( string globalPrefix, IReadOnlyDictionary
3031 /// <inheritdoc/>
3132 public string Format ( string format , object arg , IFormatProvider ? formatProvider )
3233 {
33- return arg is not NamespaceQualifiedName self
34- ? string . Empty
35- : Format ( format , self , formatProvider ) ;
34+ if ( arg is NamespaceQualifiedName name )
35+ {
36+ return Format ( format , name , formatProvider ) ;
37+ }
38+
39+ if ( arg is NamespaceQualifiedTypeName typeName )
40+ {
41+ return Format ( format , typeName , formatProvider ) ;
42+ }
43+
44+ // Not a type supported; use the type to do it, if it's formattable itself use that
45+ // otherwise fall back to simple Object.ToString().
46+ return arg is IFormattable formattable
47+ ? formattable . ToString ( format , formatProvider )
48+ : arg . ToString ( ) ;
3649 }
3750
38- /// <summary>Formats this instance according to the args </summary>
51+ /// <summary>Formats <paramref name="arg"/> according to <paramref name="format"/> </summary>
3952 /// <param name="format">Format string for this instance (see remarks)</param>
4053 /// <param name="arg">The value to format</param>
4154 /// <param name="formatProvider">[ignored]</param>
@@ -76,10 +89,39 @@ public string Format( string format, NamespaceQualifiedName arg, IFormatProvider
7689 : rawName ;
7790 }
7891
92+ /// <summary>Formats <paramref name="arg"/> according to <paramref name="format"/></summary>
93+ /// <param name="format">Format string for this instance (see remarks)</param>
94+ /// <param name="arg">The value to format</param>
95+ /// <param name="formatProvider">[ignored]</param>
96+ /// <returns>Formatted string representation of this instance</returns>
97+ /// <remarks>
98+ /// The supported values for <paramref name="format"/> are:
99+ /// <list type="table">
100+ /// <listheader><term>Value</term><description>Description</description></listheader>
101+ /// <item><term>A</term><description>Format as a language specific alias if possible</description></item>
102+ /// <item><term>G</term><description>Format with a language specific global prefix.</description></item>
103+ /// <item><term>AG</term><description>Format with a language specific alias if possible, otherwise include a global prefix.</description></item>
104+ /// <item><term>R</term><description>The raw full name without any qualifications</description></item>
105+ /// </list>
106+ /// <note type="important">
107+ /// This will always append a <c>?</c> to the end of the formatted value IF the <see cref="NamespaceQualifiedTypeName.NullableAnnotation"/>
108+ /// indicates the value is nullable.
109+ /// </note>
110+ /// </remarks>
111+ /// <exception cref="NotSupportedException"><paramref name="format"/> is not supported</exception>
112+ public string Format ( string format , NamespaceQualifiedTypeName arg , IFormatProvider ? formatProvider )
113+ {
114+ string formattedString = Format ( format , ( NamespaceQualifiedName ) arg , formatProvider ) ;
115+ return arg . NullableAnnotation == NullableAnnotation . Annotated
116+ ? $ "{ formattedString } ?"
117+ : formattedString ;
118+ }
119+
79120 /// <inheritdoc/>
80121 public object ? GetFormat ( Type formatType )
81122 {
82123 return formatType == typeof ( ICustomFormatter < NamespaceQualifiedName > )
124+ || formatType == typeof ( ICustomFormatter < NamespaceQualifiedTypeName > )
83125 ? this
84126 : null ;
85127 }
0 commit comments