Properly convert type to globals with namespace
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
@@ -7,46 +8,15 @@ namespace Speckle.InterfaceGenerator;
|
||||
|
||||
internal static class SymbolExtensions
|
||||
{
|
||||
private static readonly HashSet<string> _defaults = new() { "System", "Microsoft" };
|
||||
public static string GetNamespaceAndType(this ITypeSymbol typeSymbol)
|
||||
{
|
||||
if (typeSymbol is ITypeParameterSymbol t)
|
||||
{
|
||||
return t.Name;
|
||||
}
|
||||
if (typeSymbol.SpecialType != SpecialType.None)
|
||||
{
|
||||
return typeSymbol.ToString();
|
||||
}
|
||||
|
||||
if (typeSymbol.NullableAnnotation == NullableAnnotation.Annotated)
|
||||
{
|
||||
return typeSymbol.ToString();
|
||||
}
|
||||
var namespacez = new List<string>();
|
||||
var ns = typeSymbol.ContainingNamespace;
|
||||
while (ns is not null && !ns.IsGlobalNamespace)
|
||||
{
|
||||
namespacez.Insert(0, ns.Name);
|
||||
ns = ns.ContainingNamespace;
|
||||
}
|
||||
|
||||
if (namespacez.Any())
|
||||
{
|
||||
if (!_defaults.Contains(namespacez.First()))
|
||||
{
|
||||
var candidate = string.Join(".", namespacez);
|
||||
var name = typeSymbol.ToString();
|
||||
if (!name.StartsWith(candidate))
|
||||
{
|
||||
name += candidate + "." + name;
|
||||
}
|
||||
return "global::" + name;
|
||||
}
|
||||
}
|
||||
|
||||
return typeSymbol.ToString();
|
||||
return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
|
||||
}
|
||||
|
||||
public static bool TryGetAttribute(
|
||||
this ISymbol symbol,
|
||||
INamedTypeSymbol attributeType,
|
||||
|
||||
Reference in New Issue
Block a user