Skip to content

Avoid chopping single trailing lambda#1876

Draft
mattwzawislak wants to merge 4 commits into
belav:mainfrom
mattwzawislak:feature/1865-dont-chop-lambda
Draft

Avoid chopping single trailing lambda#1876
mattwzawislak wants to merge 4 commits into
belav:mainfrom
mattwzawislak:feature/1865-dont-chop-lambda

Conversation

@mattwzawislak

@mattwzawislak mattwzawislak commented Jun 13, 2026

Copy link
Copy Markdown

Description

Adds in the ability to avoid chopping single trailing lambdas when the argument list up to and including the "=>" symbol doesn't need to be chopped.

This would take a snippet of code like

logging.AddFile(
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "server{0}.log"),
    o =>
    {
        o.MaxRollingFiles = 1;
        // ...
    }
);

And turn it into this (assuming configured line length isn't exceeded):

logging.AddFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "server{0}.log"), o =>
{
    o.MaxRollingFiles = 1;
    // ...
});

Without breaking directives or comments:

logging.AddFile(
#if DEBUG
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "server{0}.log"),
#else
    "server{0}.log",
#endif
    o =>
    {
        o.MaxRollingFiles = 1;
        // ...
    }
);

And avoids chopping when the lambda is not the last argument or there are multiple arguments:

logging.AddFile(
    "server{0}.log",
    cfg => cfg.Section("logging"),
    o =>
    {
        o.MaxRollingFiles = 1;
        // ...
    }
);

Related Issue

Closes #1865

Checklist

  • My code follows the project's code style
    • always var
    • follow existing naming conventions
    • always this.
    • no pointless comments
  • I will not force push after a code review of my PR has started
  • I have added tests that cover my changes

@belav belav left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good but I do disagree with the simple lambdas breaking. If you can make that change I'll redo the csharpier-repos PR to see if there are any edgecases that need to be handled.

Thanks for taking this on!

bodyIsBraced ? Doc.Null : Doc.SoftLine
);

return Doc.ConditionalGroup(wrap, wrap, chop);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just be return Doc.ConditionalGroup(wrap, chop);

Passing the same doc twice just ends up doing more work in the case where chop is what is used.

Comment on lines +107 to +109
CallMethod(CallAnotherMethod_________________(), () =>
CallAnotherMethod_________________()
);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree with this breaking this way, and prettier also avoids breaking this.

// input & expected output
        var antiforgeryMiddleware = new AntiforgeryMiddleware(
            antiforgeryService.Object,
            hc => Task.CompletedTask
        );

// this pr
        var antiforgeryMiddleware = new AntiforgeryMiddleware(antiforgeryService.Object, hc =>
            Task.CompletedTask
        );

This is what I think should be breaking, and prettier does it this way. The pr works this way currently but I don't see a test for it.

// input & expected output
        Assert.Collection(receiveAuthStateDiff.Edits, edit =>
        {
            Assert.Equal(RenderTreeEditType.PrependFrame, edit.Type);
            AssertFrame.Text(
                batch.ReferenceFrames[edit.ReferenceFrameIndex],
                "Authenticated: True; Name: Bert; Pending: False; Renders: 1"
            );
        });

I just did a quick scan of https://github.com/belav/csharpier-repos/pull/172/changes and ran into that first case right away.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Avoid chopping single trailing lambda

2 participants