Properly convert type to globals with namespace

This commit is contained in:
Adam Hathcock
2024-09-03 09:19:40 +01:00
parent 80c3145257
commit 657fb078a8
4 changed files with 21 additions and 56 deletions
+4 -34
View File
@@ -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,