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
@@ -169,7 +169,7 @@ internal class AccessorsTestsService : IAccessorsTestsService
}
public FtpStyleUriParser? SymbolBinder { get; set; }
public FtpStyleUriParser SymbolBinder2 { get; set; } = default!;
public IEnumerable<FtpStyleUriParser> SymbolBinder3 { get; set; }= default!;
public IEnumerable<FtpStyleUriParser> SymbolBinder3 { get; set; } = default!;
public string PublicProperty { get; set; } = string.Empty;
@@ -1,6 +1,7 @@
// ReSharper disable CheckNamespace
using System;
using System.Collections.Generic;
using System.Diagnostics.SymbolStore;
using Speckle.InterfaceGenerator;
@@ -17,7 +18,12 @@ public class SameNameClass : ISameNameClass { }
public class SameNameClass2 : ISameNameClass2
{
public ISameNameClass Return() => throw new InvalidOperationException();
public SymbolToken Return2() => throw new InvalidOperationException();
public T GetRequiredService<T>() where T : class => throw new InvalidOperationException();
public SymbolToken Return2() => throw new InvalidOperationException();
public T GetRequiredService<T>()
where T : class => throw new InvalidOperationException();
public void TestGenericParameter(List<SameNameClass> x) =>
throw new InvalidOperationException();
}
@@ -313,11 +313,7 @@ public class AutoInterfaceGenerator : ISourceGenerator
if (propertySymbol.IsIndexer)
{
writer.Write("{0} this[", propertySymbol.Type.GetNamespaceAndType());
writer.WriteJoin(
", ",
propertySymbol.Parameters,
(x, p) => WriteMethodParam(x, p, false)
);
writer.WriteJoin(", ", propertySymbol.Parameters, WriteMethodParam);
writer.Write("] ");
}
else
@@ -372,22 +368,15 @@ public class AutoInterfaceGenerator : ISourceGenerator
if (methodSymbol.IsGenericMethod)
{
writer.Write("<");
writer.WriteJoin(", ", methodSymbol.TypeParameters.Select(x => x.Name));
writer.WriteJoin(
", ",
methodSymbol.TypeParameters.Select(x => x.GetNamespaceAndType())
);
writer.Write(">");
}
writer.Write("(");
writer.WriteJoin(
", ",
methodSymbol.Parameters,
(x, p) =>
WriteMethodParam(
x,
p,
owner.TypeParameters.Any(t => t.Name == p.Type.Name)
|| methodSymbol.TypeParameters.Any(t => t.Name == p.Type.Name)
)
);
writer.WriteJoin(", ", methodSymbol.Parameters, WriteMethodParam);
writer.Write(")");
@@ -399,7 +388,7 @@ public class AutoInterfaceGenerator : ISourceGenerator
writer.WriteLine(";");
}
private static void WriteMethodParam(TextWriter writer, IParameterSymbol param, bool isGeneric)
private static void WriteMethodParam(TextWriter writer, IParameterSymbol param)
{
if (param.IsParams)
{
@@ -419,7 +408,7 @@ public class AutoInterfaceGenerator : ISourceGenerator
break;
}
writer.Write(isGeneric ? param.Type : param.Type.GetNamespaceAndType());
writer.Write(param.Type.GetNamespaceAndType());
writer.Write(" ");
if (StringExtensions.IsCSharpKeyword(param.Name))
+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,