From 3c592151787c9f2ccc8c55c86107c4d68371999f Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 30 Jul 2024 15:00:48 +0100 Subject: [PATCH 1/2] Use Global namespaces for some types --- .../AccessorsGenerationTests.cs | 5 ++ .../SameName/SameNameClass.1.cs | 12 ++++- .../AutoInterfaceGenerator.cs | 52 ++++++++++++------- .../SymbolExtensions.cs | 36 +++++++++++++ 4 files changed, 85 insertions(+), 20 deletions(-) diff --git a/Speckle.InterfaceGenerator.Tests/AccessorsGenerationTests.cs b/Speckle.InterfaceGenerator.Tests/AccessorsGenerationTests.cs index ebdeb51..ccc2476 100644 --- a/Speckle.InterfaceGenerator.Tests/AccessorsGenerationTests.cs +++ b/Speckle.InterfaceGenerator.Tests/AccessorsGenerationTests.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Diagnostics.SymbolStore; using System.Linq; using System.Runtime.CompilerServices; using FluentAssertions; @@ -165,6 +167,9 @@ internal class AccessorsTestsService : IAccessorsTestsService get => 0; set { } } + public FtpStyleUriParser? SymbolBinder { get; set; } + public FtpStyleUriParser SymbolBinder2 { 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 bba5998..2a751d8 100644 --- a/Speckle.InterfaceGenerator.Tests/SameName/SameNameClass.1.cs +++ b/Speckle.InterfaceGenerator.Tests/SameName/SameNameClass.1.cs @@ -1,5 +1,7 @@ // ReSharper disable CheckNamespace +using System; +using System.Diagnostics.SymbolStore; using Speckle.InterfaceGenerator; namespace InterfaceGenerator.Tests.SameName_1; @@ -9,4 +11,12 @@ namespace InterfaceGenerator.Tests.SameName_1; /// qualified names. /// [GenerateAutoInterface] -internal class SameNameClass : ISameNameClass { } +public class SameNameClass : ISameNameClass { } + +[GenerateAutoInterface] +public class SameNameClass2 : ISameNameClass2 +{ + public ISameNameClass Return() => throw new InvalidOperationException(); + public SymbolToken Return2() => throw new InvalidOperationException(); + +} diff --git a/Speckle.InterfaceGenerator/AutoInterfaceGenerator.cs b/Speckle.InterfaceGenerator/AutoInterfaceGenerator.cs index deb577f..5152cca 100644 --- a/Speckle.InterfaceGenerator/AutoInterfaceGenerator.cs +++ b/Speckle.InterfaceGenerator/AutoInterfaceGenerator.cs @@ -22,14 +22,6 @@ public class AutoInterfaceGenerator : ISourceGenerator public void Initialize(GeneratorInitializationContext context) { context.RegisterForSyntaxNotifications(() => new SyntaxReceiver()); - -#if DEBUG - if (!Debugger.IsAttached) - { - // sadly this is Windows only so as of now :( - Debugger.Launch(); - } -#endif } public void Execute(GeneratorExecutionContext context) @@ -223,7 +215,7 @@ public class AutoInterfaceGenerator : ISourceGenerator private void GenerateInterfaceMemberDefinitions( TextWriter writer, - INamespaceOrTypeSymbol implTypeSymbol + INamedTypeSymbol implTypeSymbol ) { foreach (var member in implTypeSymbol.GetMembers()) @@ -238,11 +230,15 @@ public class AutoInterfaceGenerator : ISourceGenerator continue; } - GenerateInterfaceMemberDefinition(writer, member); + GenerateInterfaceMemberDefinition(writer, implTypeSymbol, member); } } - private static void GenerateInterfaceMemberDefinition(TextWriter writer, ISymbol member) + private static void GenerateInterfaceMemberDefinition( + TextWriter writer, + INamedTypeSymbol owner, + ISymbol member + ) { switch (member) { @@ -250,7 +246,7 @@ public class AutoInterfaceGenerator : ISourceGenerator GeneratePropertyDefinition(writer, propertySymbol); break; case IMethodSymbol methodSymbol: - GenerateMethodDefinition(writer, methodSymbol); + GenerateMethodDefinition(writer, owner, methodSymbol); break; } } @@ -316,8 +312,12 @@ public class AutoInterfaceGenerator : ISourceGenerator if (propertySymbol.IsIndexer) { - writer.Write("{0} this[", propertySymbol.Type); - writer.WriteJoin(", ", propertySymbol.Parameters, WriteMethodParam); + writer.Write("{0} this[", propertySymbol.Type.GetNamespaceAndType()); + writer.WriteJoin( + ", ", + propertySymbol.Parameters, + (x, p) => WriteMethodParam(x, p, false) + ); writer.Write("] "); } else @@ -347,7 +347,11 @@ public class AutoInterfaceGenerator : ISourceGenerator writer.WriteLine("}"); } - private static void GenerateMethodDefinition(TextWriter writer, IMethodSymbol methodSymbol) + private static void GenerateMethodDefinition( + TextWriter writer, + INamedTypeSymbol owner, + IMethodSymbol methodSymbol + ) { if (methodSymbol.MethodKind != MethodKind.Ordinary || methodSymbol.IsStatic) { @@ -363,7 +367,7 @@ public class AutoInterfaceGenerator : ISourceGenerator WriteSymbolDocsIfPresent(writer, methodSymbol); - writer.Write("{0} {1}", methodSymbol.ReturnType, methodSymbol.Name); // ex. int Foo + writer.Write("{0} {1}", methodSymbol.ReturnType.GetNamespaceAndType(), methodSymbol.Name); // ex. int Foo if (methodSymbol.IsGenericMethod) { @@ -373,7 +377,17 @@ public class AutoInterfaceGenerator : ISourceGenerator } writer.Write("("); - writer.WriteJoin(", ", methodSymbol.Parameters, WriteMethodParam); + 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.Write(")"); @@ -385,7 +399,7 @@ public class AutoInterfaceGenerator : ISourceGenerator writer.WriteLine(";"); } - private static void WriteMethodParam(TextWriter writer, IParameterSymbol param) + private static void WriteMethodParam(TextWriter writer, IParameterSymbol param, bool isGeneric) { if (param.IsParams) { @@ -405,7 +419,7 @@ public class AutoInterfaceGenerator : ISourceGenerator break; } - writer.Write(param.Type); + writer.Write(isGeneric ? param.Type : param.Type.GetNamespaceAndType()); writer.Write(" "); if (StringExtensions.IsCSharpKeyword(param.Name)) diff --git a/Speckle.InterfaceGenerator/SymbolExtensions.cs b/Speckle.InterfaceGenerator/SymbolExtensions.cs index 0c65890..5518ff8 100644 --- a/Speckle.InterfaceGenerator/SymbolExtensions.cs +++ b/Speckle.InterfaceGenerator/SymbolExtensions.cs @@ -7,6 +7,42 @@ namespace Speckle.InterfaceGenerator; internal static class SymbolExtensions { + private static readonly HashSet _defaults = new() { "System", "Microsoft" }; + public static string GetNamespaceAndType(this ITypeSymbol typeSymbol) + { + 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(); + } public static bool TryGetAttribute( this ISymbol symbol, INamedTypeSymbol attributeType, From f6688fa5a3c8ba8d91aa5faae195308cec334652 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 30 Jul 2024 15:01:19 +0100 Subject: [PATCH 2/2] 0.9.6 --- Speckle.InterfaceGenerator/Speckle.InterfaceGenerator.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Speckle.InterfaceGenerator/Speckle.InterfaceGenerator.csproj b/Speckle.InterfaceGenerator/Speckle.InterfaceGenerator.csproj index 69ec099..d49b068 100644 --- a/Speckle.InterfaceGenerator/Speckle.InterfaceGenerator.csproj +++ b/Speckle.InterfaceGenerator/Speckle.InterfaceGenerator.csproj @@ -3,7 +3,7 @@ netstandard2.0 Latest enable - 0.9.5 + 0.9.6 true false false