11namespace TypeReferences . Editor . Util
22{
33 using System ;
4+ using System . Collections . Generic ;
5+ using System . Linq ;
46 using TypeReferences ;
57 using UnityEngine ;
68
1012 /// </summary>
1113 internal static class TypeNameFormatter
1214 {
15+ private const string BuiltInTypesPrefix = "Built-in." ;
16+
17+ private static readonly Dictionary < string , string > BuiltInTypes = new Dictionary < string , string >
18+ {
19+ { "System.Boolean" , "bool" } ,
20+ { "System.Byte" , "byte" } ,
21+ { "System.SByte" , "sbyte" } ,
22+ { "System.Char" , "char" } ,
23+ { "System.Decimal" , "decimal" } ,
24+ { "System.Double" , "double" } ,
25+ { "System.Single" , "float" } ,
26+ { "System.Int32" , "int" } ,
27+ { "System.UInt32" , "uint" } ,
28+ { "System.Int64" , "long" } ,
29+ { "System.UInt64" , "ulong" } ,
30+ { "System.Int16" , "short" } ,
31+ { "System.UInt16" , "ushort" } ,
32+ { "System.Object" , "object" } ,
33+ { "System.String" , "string" }
34+ } ;
35+
1336 /// <summary>Generates a path for a dropdown item according to <paramref name="grouping"/>.</summary>
1437 /// <param name="type">Type to generate the path for.</param>
1538 /// <param name="fullTypeName">Full name of the type.</param>
@@ -35,6 +58,35 @@ public static string Format(Type type, string fullTypeName, Grouping grouping)
3558 }
3659 }
3760
61+ /// <summary>
62+ /// Replaces <paramref name="fullTypeName"/> with a built-in name if the built-in analogue exists.
63+ /// </summary>
64+ /// <param name="fullTypeName">Full name of the type.</param>
65+ /// <param name="withoutFolder">Whether to append a folder to the built-in type.</param>
66+ /// <returns>Name of the built-in type.</returns>
67+ /// <example><code>
68+ /// string intName = typeof(System.Int32).FullName;
69+ /// if (TryReplaceWithBuiltInName(intName))
70+ /// Debug.Log(intName); // prints "Built-in.int"
71+ ///
72+ /// string intName = typeof(System.Int32).FullName;
73+ /// if (TryReplaceWithBuiltInName(intName), true)
74+ /// Debug.Log(intName); // prints "int"
75+ /// </code></example>
76+ public static bool TryReplaceWithBuiltInName ( ref string fullTypeName , bool withoutFolder = false )
77+ {
78+ if ( ! BuiltInTypes . TryGetValue ( fullTypeName , out string builtInName ) )
79+ return false ;
80+
81+ fullTypeName = withoutFolder ? builtInName : BuiltInTypesPrefix + builtInName ;
82+ return true ;
83+ }
84+
85+ /// <summary>Gets the name of the type without its namespace.</summary>
86+ /// <param name="fullTypeName">Full name of the type including its namespace.</param>
87+ /// <returns>The type name without namespaces.</returns>
88+ public static string GetShortName ( string fullTypeName ) => fullTypeName . Split ( '.' ) . Last ( ) ;
89+
3890 private static string FormatByNamespace ( string name )
3991 {
4092 return name . Replace ( '.' , '/' ) ;
0 commit comments