Skip to content

Commit f011509

Browse files
committed
fix: namespaces
1 parent baa1443 commit f011509

4 files changed

Lines changed: 40 additions & 26 deletions

File tree

samples/generators/SourceKit.Generators.Grpc.Samples/protos/duplicate_root_namespace.proto

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ option csharp_namespace = "SourceKit.Sample.SourceKit.Models";
33

44
message DuplicateRootNamespace {
55
int32 value = 1;
6+
DuplicateRootNamespace2 two = 2;
7+
}
8+
9+
message DuplicateRootNamespace2 {
10+
int32 value = 1;
611
}

src/SourceKit/Extensions/NamedTypeSymbolExtensions.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ namespace SourceKit.Extensions;
88

99
public static class NamedTypeSymbolExtensions
1010
{
11-
private static SymbolDisplayFormat ConfigureDisplayFormat(SymbolDisplayFormat format) => format
12-
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)
11+
private static SymbolDisplayFormat ConfigureDisplayFormat(SymbolDisplayFormat format, bool includeGlobal) => format
12+
.WithGlobalNamespaceStyle(includeGlobal
13+
? SymbolDisplayGlobalNamespaceStyle.Included
14+
: SymbolDisplayGlobalNamespaceStyle.Omitted)
1315
.WithGenericsOptions(SymbolDisplayGenericsOptions.None);
1416

15-
private static readonly SymbolDisplayFormat FullyQualifiedSymbolFormat =
16-
ConfigureDisplayFormat(SymbolDisplayFormat.FullyQualifiedFormat);
17+
private static SymbolDisplayFormat FullyQualifiedSymbolFormat(bool includeGlobal)
18+
=> ConfigureDisplayFormat(SymbolDisplayFormat.FullyQualifiedFormat, includeGlobal);
1719

18-
private static readonly SymbolDisplayFormat ShortSymbolFormat =
19-
ConfigureDisplayFormat(SymbolDisplayFormat.MinimallyQualifiedFormat);
20+
private static SymbolDisplayFormat ShortSymbolFormat(bool includeGlobal)
21+
=> ConfigureDisplayFormat(SymbolDisplayFormat.MinimallyQualifiedFormat, includeGlobal);
2022

2123
private static bool TryGetTypeArgumentSyntax(
2224
this INamespaceOrTypeSymbol symbol,
@@ -39,15 +41,18 @@ private static bool TryGetTypeArgumentSyntax(
3941
return false;
4042
}
4143

42-
public static string GetFullyQualifiedName(this INamespaceOrTypeSymbol symbol)
43-
=> symbol.ToDisplayString(FullyQualifiedSymbolFormat);
44+
public static string GetFullyQualifiedName(this INamespaceOrTypeSymbol symbol, bool includeGlobal = false)
45+
=> symbol.ToDisplayString(FullyQualifiedSymbolFormat(includeGlobal));
4446

45-
public static string GetShortName(this INamespaceOrTypeSymbol symbol)
46-
=> symbol.ToDisplayString(ShortSymbolFormat);
47+
public static string GetShortName(this INamespaceOrTypeSymbol symbol, bool includeGlobal = false)
48+
=> symbol.ToDisplayString(ShortSymbolFormat(includeGlobal));
4749

48-
public static TypeSyntax ToNameSyntax(this INamespaceOrTypeSymbol symbol, bool fullyQualified = true)
50+
public static TypeSyntax ToNameSyntax(
51+
this INamespaceOrTypeSymbol symbol,
52+
bool fullyQualified = true,
53+
bool includeGlobal = false)
4954
{
50-
string name = fullyQualified ? symbol.GetFullyQualifiedName() : symbol.GetShortName();
55+
string name = fullyQualified ? symbol.GetFullyQualifiedName(includeGlobal) : symbol.GetShortName(includeGlobal);
5156

5257
TypeSyntax type = TryGetTypeArgumentSyntax(symbol, fullyQualified, out TypeArgumentListSyntax? typeArguments)
5358
? GenericName(Identifier(name), typeArguments)
@@ -77,7 +82,7 @@ public static TypeSyntax ToNameSyntax(this INamespaceOrTypeSymbol symbol, bool f
7782
public static IEnumerable<INamedTypeSymbol> GetBaseTypes(this INamedTypeSymbol symbol)
7883
{
7984
return symbol.BaseType is null
80-
? Enumerable.Empty<INamedTypeSymbol>()
85+
? []
8186
: Enumerable.Repeat(symbol.BaseType, 1).Concat(symbol.BaseType.GetBaseTypes());
8287
}
8388

@@ -139,4 +144,4 @@ public static TypeDeclarationSyntax ToSyntax(this INamedTypeSymbol symbol)
139144
_ => throw new ArgumentOutOfRangeException(),
140145
};
141146
}
142-
}
147+
}

src/SourceKit/Extensions/TypeSymbolExtensions.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ namespace SourceKit.Extensions;
66

77
public static class TypeSymbolExtensions
88
{
9-
public static IEnumerable<IdentifierNameSyntax> ToTypeArgumentSyntax(this IEnumerable<ITypeSymbol> symbols)
10-
=> symbols.Select(ToTypeArgumentSyntax);
9+
public static IEnumerable<IdentifierNameSyntax> ToTypeArgumentSyntax(
10+
this IEnumerable<ITypeSymbol> symbols,
11+
bool includeGlobal = false)
12+
{
13+
return symbols.Select(x => x.ToTypeArgumentSyntax(includeGlobal));
14+
}
1115

12-
public static IdentifierNameSyntax ToTypeArgumentSyntax(this ITypeSymbol symbol)
13-
=> IdentifierName(symbol.GetFullyQualifiedName());
16+
public static IdentifierNameSyntax ToTypeArgumentSyntax(this ITypeSymbol symbol, bool includeGlobal = false)
17+
=> IdentifierName(symbol.GetFullyQualifiedName(includeGlobal));
1418

1519
public static bool IsAssignableTo(this ITypeSymbol source, ITypeSymbol destination)
1620
{
@@ -81,4 +85,4 @@ public static ITypeSymbol GetEnumerableTypeArgument(this ITypeSymbol enumerableT
8185

8286
return constructedFrom.TypeArguments.Single();
8387
}
84-
}
88+
}

src/generators/SourceKit.Generators.Grpc/Builders/TypeBuilders/ConstructorTypeBuilder.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ private static IEnumerable<ParameterSyntax> MapParameters(TypeBuildingCommand co
5050
if (property is RepeatableProtoProperty repeatableProperty)
5151
{
5252
var typeArguments = TypeArgumentList(
53-
SingletonSeparatedList(repeatableProperty.ElementType.ToNameSyntax(fullyQualified: true)));
53+
SingletonSeparatedList(repeatableProperty.ElementType.ToNameSyntax(includeGlobal: true)));
5454

5555
typeName = GenericName(Identifier("IEnumerable"), typeArguments);
5656
}
5757
else if (property is MapProtoProperty mapProperty)
5858
{
5959
var typeArguments = TypeArgumentList(SeparatedList(new[]
6060
{
61-
mapProperty.Key.ToNameSyntax(fullyQualified: true),
62-
mapProperty.Value.ToNameSyntax(fullyQualified: true),
61+
mapProperty.Key.ToNameSyntax(includeGlobal: true),
62+
mapProperty.Value.ToNameSyntax(includeGlobal: true),
6363
}));
6464

6565
var keyValuePair = GenericName(Identifier("KeyValuePair"), typeArguments);
@@ -70,7 +70,7 @@ private static IEnumerable<ParameterSyntax> MapParameters(TypeBuildingCommand co
7070
}
7171
else if (property is ValueProtoProperty valueProperty)
7272
{
73-
typeName = valueProperty.Type.ToNameSyntax(fullyQualified: true);
73+
typeName = valueProperty.Type.ToNameSyntax(includeGlobal: true);
7474
}
7575
else
7676
{
@@ -132,8 +132,8 @@ private static StatementSyntax CreateMapAssignment(
132132

133133
var typeArguments = TypeArgumentList(SeparatedList<TypeSyntax>(new[]
134134
{
135-
mapProperty.Key.ToTypeArgumentSyntax(),
136-
mapProperty.Value.ToTypeArgumentSyntax(),
135+
mapProperty.Key.ToTypeArgumentSyntax(includeGlobal: true),
136+
mapProperty.Value.ToTypeArgumentSyntax(includeGlobal: true),
137137
}));
138138

139139
var keyValuePair = GenericName(Identifier("KeyValuePair"), typeArguments);
@@ -191,4 +191,4 @@ private static StatementSyntax CreateOneOfAssignment(
191191

192192
return IfStatement(condition, Block(statement));
193193
}
194-
}
194+
}

0 commit comments

Comments
 (0)