Skip to content

Commit 902aeff

Browse files
authored
Merge empty nested namespaces (#1924)
1 parent 9621e88 commit 902aeff

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/Generator/Generators/CLI/CLIHeaders.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,12 @@ public void GenerateForwardRefs()
144144
public void GenerateDeclContext(DeclarationContext decl)
145145
{
146146
// Generate all the type references for the module.
147+
PushBlock(BlockKind.ForwardReferences);
147148
foreach (var typeRef in decl.TypeReferences)
148149
{
149150
WriteLine(typeRef.FowardReference);
150151
}
152+
PopBlock(NewLineKind.BeforeNextBlock);
151153

152154
// Generate all the enum declarations for the module.
153155
foreach (var @enum in decl.Enums)
@@ -188,21 +190,29 @@ public void GenerateNamespace(Namespace @namespace)
188190
var generateNamespace = !isTopLevel ||
189191
!string.IsNullOrEmpty(@namespace.TranslationUnit.Module.OutputNamespace);
190192

191-
if (generateNamespace)
193+
194+
var names = new List<string>
192195
{
193-
PushBlock(BlockKind.Namespace, @namespace);
194-
WriteLine("namespace {0}", isTopLevel
195-
? @namespace.TranslationUnit.Module.OutputNamespace
196-
: @namespace.Name);
197-
WriteOpenBraceAndIndent();
196+
isTopLevel
197+
? @namespace.TranslationUnit.Module.OutputNamespace
198+
: @namespace.Name
199+
};
200+
201+
// Merge nested namespaces into the parent namespace.
202+
while (@namespace.Declarations.Count == 1 &&
203+
@namespace.Declarations[0] is Namespace { IsInline: false } childNamespace)
204+
{
205+
@namespace = childNamespace;
206+
names.Add(@namespace.Name);
198207
}
199208

200-
GenerateDeclContext(@namespace);
209+
var namespaceName = string.Join("::", names);
201210

202-
if (generateNamespace)
211+
using (generateNamespace
212+
? PushWriteBlock(BlockKind.Namespace, $"namespace {namespaceName}", NewLineKind.BeforeNextBlock)
213+
: default)
203214
{
204-
UnindentAndWriteCloseBrace();
205-
PopBlock(NewLineKind.BeforeNextBlock);
215+
GenerateDeclContext(@namespace);
206216
}
207217
}
208218

0 commit comments

Comments
 (0)