Skip to content

A braced BodyFactory body keeps its leading/trailing blank lines: a blank line before {, and two blank lines between members #180

Open

Description

@matt-edmondson

What's wrong

TemplateRendering.WriteBody (CodeBlocker/Templates/TemplateRendering.cs:280-307) ignores blank lines only when deciding the body's shape (the contentLines filter). For a braced body it then splices the unfiltered body, so a factory's leading and trailing blank lines end up in the output.

var c = new ClassTemplate { Name = "C" };
c.Members.Add(new MethodTemplate { Type = "void", Name = "A",
    BodyFactory = b => { using (new Scope(b)) b.WriteLine("x();"); b.NewLine(); } });
c.Members.Add(new MethodTemplate { Type = "void", Name = "B",
    BodyFactory = b => { b.NewLine(); b.WriteLine("{"); b.WriteLine("}"); } });

Actual (→ = tab):

→}


→void B()

→{
→}

That gives two blank lines between A and B, and a blank line between void B() and its {.

Expected: one blank line between members, and { directly under the signature.

Why it matters

The output compiles, but it doesn't match the library's own rules. WriteExpressionBody already trims leading and trailing blank lines, and #119/#133 established that blank lines should not shape a body. A trailing NewLine() after a loop, which is the pattern #133 had in mind, now shows up as extra spacing in every generated file. That means formatting churn in generated diffs and failures in any whitespace-sensitive snapshot test.

Suggested fix

In WriteBody, trim leading and trailing empty entries from lines and splice only that range. Blank lines inside the body stay.

Acceptance: the repro above produces exactly one blank line between members and no blank line between a signature and its {. Interior blank lines in a body are preserved.

Activity

  1. matt-edmondson commented on Nov 3, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Next steps: trim leading and trailing empty entries from lines, splice only that range, and keep interior blank lines. Add the repro from the issue as a test (one blank line between members, { directly under the signature), plus a test that interior blank lines survive.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions