Skip to content

Commit a7e54c7

Browse files
Copilotstephentoub
andcommitted
Fix nested type indentation calculation
Co-authored-by: stephentoub <[email protected]>
1 parent 3cf7601 commit a7e54c7

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/ModelContextProtocol.SourceGenerators/XmlToDescriptionGenerator.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,22 @@ private static string GenerateSourceFile(
266266
continue;
267267
}
268268

269+
// Calculate nesting depth for proper indentation
270+
var nestingDepth = 1;
271+
var temp = containingType;
272+
while (temp is not null)
273+
{
274+
nestingDepth++;
275+
temp = temp.ContainingType;
276+
}
277+
269278
// Handle nested types by building the full type hierarchy
270279
AppendNestedTypeDeclarations(sb, containingType, 1, () =>
271280
{
272281
// Generate methods for this type
273282
foreach (var (methodSymbol, methodDeclaration, xmlDocs) in typeGroup)
274283
{
275-
AppendMethodDeclaration(sb, methodSymbol, methodDeclaration, xmlDocs, descriptionAttribute, 2);
284+
AppendMethodDeclaration(sb, methodSymbol, methodDeclaration, xmlDocs, descriptionAttribute, nestingDepth);
276285
}
277286
});
278287

@@ -287,17 +296,19 @@ private static string GenerateSourceFile(
287296

288297
private static void AppendNestedTypeDeclarations(StringBuilder sb, INamedTypeSymbol typeSymbol, int indentLevel, System.Action appendBody)
289298
{
290-
var indent = new string(' ', indentLevel * 4);
291299
var types = new Stack<INamedTypeSymbol>();
292300

293-
// Build stack of nested types
301+
// Build stack of nested types from innermost to outermost
294302
var current = typeSymbol;
295303
while (current is not null)
296304
{
297305
types.Push(current);
298306
current = current.ContainingType;
299307
}
300308

309+
var startIndentLevel = indentLevel;
310+
var nestingCount = types.Count;
311+
301312
// Generate type declarations from outermost to innermost
302313
while (types.Count > 0)
303314
{
@@ -353,8 +364,7 @@ private static void AppendNestedTypeDeclarations(StringBuilder sb, INamedTypeSym
353364
appendBody();
354365

355366
// Close all type declarations
356-
indentLevel = types.Count + 1;
357-
while (indentLevel > 1)
367+
for (int i = 0; i < nestingCount; i++)
358368
{
359369
indentLevel--;
360370
var typeIndent = new string(' ', indentLevel * 4);

0 commit comments

Comments
 (0)