From 657fb078a81985ecc7fac2bbb8073c5adfe14362 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 3 Sep 2024 09:19:40 +0100 Subject: [PATCH] Properly convert type to globals with namespace --- .../AccessorsGenerationTests.cs | 2 +- .../SameName/SameNameClass.1.cs | 10 ++++- .../AutoInterfaceGenerator.cs | 27 ++++--------- .../SymbolExtensions.cs | 38 ++----------------- 4 files changed, 21 insertions(+), 56 deletions(-) diff --git a/Speckle.InterfaceGenerator.Tests/AccessorsGenerationTests.cs b/Speckle.InterfaceGenerator.Tests/AccessorsGenerationTests.cs index ccc2476..f3bca43 100644 --- a/Speckle.InterfaceGenerator.Tests/AccessorsGenerationTests.cs +++ b/Speckle.InterfaceGenerator.Tests/AccessorsGenerationTests.cs @@ -169,7 +169,7 @@ internal class AccessorsTestsService : IAccessorsTestsService } public FtpStyleUriParser? SymbolBinder { get; set; } public FtpStyleUriParser SymbolBinder2 { get; set; } = default!; - public IEnumerable SymbolBinder3 { get; set; }= default!; + public IEnumerable SymbolBinder3 { get; set; } = default!; public string PublicProperty { get; set; } = string.Empty; diff --git a/Speckle.InterfaceGenerator.Tests/SameName/SameNameClass.1.cs b/Speckle.InterfaceGenerator.Tests/SameName/SameNameClass.1.cs index 9c855f5..e134043 100644 --- a/Speckle.InterfaceGenerator.Tests/SameName/SameNameClass.1.cs +++ b/Speckle.InterfaceGenerator.Tests/SameName/SameNameClass.1.cs @@ -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() where T : class => throw new InvalidOperationException(); + public SymbolToken Return2() => throw new InvalidOperationException(); + + public T GetRequiredService() + where T : class => throw new InvalidOperationException(); + + public void TestGenericParameter(List x) => + throw new InvalidOperationException(); } diff --git a/Speckle.InterfaceGenerator/AutoInterfaceGenerator.cs b/Speckle.InterfaceGenerator/AutoInterfaceGenerator.cs index 5152cca..2fc3394 100644 --- a/Speckle.InterfaceGenerator/AutoInterfaceGenerator.cs +++ b/Speckle.InterfaceGenerator/AutoInterfaceGenerator.cs @@ -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)) diff --git a/Speckle.InterfaceGenerator/SymbolExtensions.cs b/Speckle.InterfaceGenerator/SymbolExtensions.cs index 889d8bd..dc733ea 100644 --- a/Speckle.InterfaceGenerator/SymbolExtensions.cs +++ b/Speckle.InterfaceGenerator/SymbolExtensions.cs @@ -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 _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(); - 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,