Merge pull request #2 from specklesystems/global-namespace
Global namespacing
This commit is contained in:
@@ -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<FtpStyleUriParser> SymbolBinder3 { get; set; }= default!;
|
||||
|
||||
public string PublicProperty { get; set; } = string.Empty;
|
||||
|
||||
|
||||
@@ -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.
|
||||
/// </summary>
|
||||
[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();
|
||||
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>Latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<PackageVersion>0.9.5</PackageVersion>
|
||||
<PackageVersion>0.9.6</PackageVersion>
|
||||
<developmentDependency>true</developmentDependency>
|
||||
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
|
||||
<IncludeBuildOutput>false</IncludeBuildOutput>
|
||||
|
||||
@@ -7,6 +7,42 @@ 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.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();
|
||||
}
|
||||
public static bool TryGetAttribute(
|
||||
this ISymbol symbol,
|
||||
INamedTypeSymbol attributeType,
|
||||
|
||||
Reference in New Issue
Block a user