Skip to content

Commit

Permalink
Fixed some namespace logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Aug 6, 2023
1 parent 8efcbbe commit 1de1b39
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Schema/src/binary/BinarySchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void Generate_(IBinarySchemaStructure structure) {
&& structure.TypeSymbol.MemberNames.All(member => member != "Read")) {
var readerCode = this.readerImpl_.Generate(structure);
this.context_.Value.AddSource(
SymbolTypeUtil.GetQualifiedName(structure.TypeSymbol) + "_reader.g",
SymbolTypeUtil.GetFullyQualifiedName(structure.TypeSymbol) + "_reader.g",
readerCode);
}

Expand All @@ -35,7 +35,7 @@ private void Generate_(IBinarySchemaStructure structure) {
member => member != "Write")) {
var writerCode = this.writerImpl_.Generate(structure);
this.context_.Value.AddSource(
SymbolTypeUtil.GetQualifiedName(structure.TypeSymbol) + "_writer.g",
SymbolTypeUtil.GetFullyQualifiedName(structure.TypeSymbol) + "_writer.g",
writerCode);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Schema/src/binary/text/BinarySchemaReaderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BinarySchemaReaderGenerator {
public string Generate(IBinarySchemaStructure structure) {
var typeSymbol = structure.TypeSymbol;

var typeNamespace = SymbolTypeUtil.MergeContainingNamespaces(typeSymbol);
var typeNamespace = typeSymbol.GetFullyQualifiedNamespace();

var declaringTypes =
SymbolTypeUtil.GetDeclaringTypesDownward(typeSymbol);
Expand Down
2 changes: 1 addition & 1 deletion Schema/src/binary/text/BinarySchemaWriterGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class BinarySchemaWriterGenerator {
public string Generate(IBinarySchemaStructure structure) {
var typeSymbol = structure.TypeSymbol;

var typeNamespace = SymbolTypeUtil.MergeContainingNamespaces(typeSymbol);
var typeNamespace = typeSymbol.GetFullyQualifiedNamespace();

var declaringTypes =
SymbolTypeUtil.GetDeclaringTypesDownward(typeSymbol);
Expand Down
3 changes: 1 addition & 2 deletions Schema/src/util/NameofUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public static string GetChainedAccessFromCallerArgumentExpression(
new TypeAndNamespace {
Defined = true,
Name = parent.Name,
NamespaceParts = parent.GetContainingNamespaces() ??
Array.Empty<string>(),
NamespaceParts = parent.GetContainingNamespaces().ToArray(),
},
text);

Expand Down
69 changes: 26 additions & 43 deletions Schema/src/util/symbols/SymbolTypeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,44 +139,28 @@ internal static bool IsInNamespace(
currentNamespace.ContainingNamespace.Name.Length == 0;
}

public static string[]? GetContainingNamespaces(this ISymbol symbol) {

public static string GetFullyQualifiedNamespace(this ISymbol symbol)
=> string.Join(".", symbol.GetContainingNamespaces());

public static IEnumerable<string> GetContainingNamespaces(
this ISymbol symbol)
=> symbol.GetContainingNamespacesReversed_().Reverse();

private static IEnumerable<string> GetContainingNamespacesReversed_(
this ISymbol symbol) {
var namespaceSymbol = symbol.ContainingNamespace;
if (namespaceSymbol == null) {
return null;
if (namespaceSymbol.IsGlobalNamespace) {
yield break;
}

var namespaces = new LinkedList<string>();
while (namespaceSymbol != null) {
while (!namespaceSymbol.IsGlobalNamespace) {
if (namespaceSymbol.Name.Length > 0) {
namespaces.AddFirst(namespaceSymbol.Name);
yield return namespaceSymbol.Name;
}

namespaceSymbol = namespaceSymbol.ContainingNamespace;
}

return namespaces.ToArray();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? MergeContainingNamespaces(ISymbol symbol)
=> MergeNamespaceParts(GetContainingNamespaces(symbol));

public static string? MergeNamespaceParts(IList<string>? namespaces) {
if ((namespaces?.Count ?? 0) == 0) {
return null;
}

var combined = new StringBuilder();
foreach (var space in namespaces) {
if (combined.Length == 0) {
combined.Append(space);
} else {
combined.Append(".");
combined.Append(space);
}
}

return combined.ToString();
}

public static bool HasEmptyConstructor(this INamedTypeSymbol symbol)
Expand All @@ -194,9 +178,8 @@ public static bool MatchesGeneric(this INamedTypeSymbol symbol,

var sameName = symbol.Name ==
expectedGenericType.Name.Substring(0, indexOfBacktick);
var sameNamespace =
SymbolTypeUtil.MergeContainingNamespaces(symbol) ==
expectedGenericType.Namespace;
var sameNamespace = symbol.GetFullyQualifiedNamespace() ==
expectedGenericType.Namespace;
var sameTypeArguments = symbol.TypeArguments.Length ==
expectedGenericType.GetTypeInfo()
.GenericTypeParameters.Length;
Expand Down Expand Up @@ -375,10 +358,9 @@ public static string AccessibilityToModifier(
null)
};

public static string GetQualifiedName(this ITypeSymbol typeSymbol) {
var mergedNamespace =
SymbolTypeUtil.MergeContainingNamespaces(typeSymbol);
var mergedNamespaceText = mergedNamespace == null
public static string GetFullyQualifiedName(this ITypeSymbol typeSymbol) {
var mergedNamespace = typeSymbol.GetFullyQualifiedNamespace();
var mergedNamespaceText = mergedNamespace.Length == 0
? ""
: $"{mergedNamespace}.";

Expand Down Expand Up @@ -411,15 +393,16 @@ or SpecialType.System_Boolean
return referencedSymbol.ToDisplayString();
}

var currentNamespace = sourceSymbol.GetContainingNamespaces();
var referencedNamespace = referencedSymbol.GetContainingNamespaces();
var currentNamespace = sourceSymbol.GetContainingNamespaces().ToArray();
var referencedNamespace =
referencedSymbol.GetContainingNamespaces().ToArray();

string mergedNamespaceText;
if (currentNamespace == null && referencedNamespace == null) {
if (currentNamespace.Length == 0 && referencedNamespace.Length == 0) {
mergedNamespaceText = "";
} else if (currentNamespace == null) {
} else if (currentNamespace.Length == 0) {
mergedNamespaceText = $"{referencedNamespace!}.";
} else if (referencedNamespace == null) {
} else if (referencedNamespace.Length == 0) {
mergedNamespaceText = $"{currentNamespace}.";
} else {
var namespaces = new List<string>();
Expand All @@ -436,7 +419,7 @@ or SpecialType.System_Boolean
}

mergedNamespaceText = namespaces.Count > 0
? $"{MergeNamespaceParts(namespaces)}."
? $"{string.Join(".", namespaces)}."
: "";
}

Expand Down

0 comments on commit 1de1b39

Please sign in to comment.