Skip to content

Add braces to multi-line lambda body #1222

@iantabolt

Description

@iantabolt

This issue occurs when you have a single lambda argument with a single statement multi-line body. Scalafmt will drop the lambda to a newline and indent the block. Please correct me if this is working as intended or if a configuration already exists.

With default configuration,

// Before scalafmt (and expected output)
foos.map(foo =>
  foo
    .withBar("bar")
    .toString)

// After scalafmt
foos.map(
  foo =>
    foo
      .withBar("bar")
      .toString)

Meanwhile these similar scenarios are unchanged as expected:

foos.map(foo => {
  // ...
})

foos.map { foo =>
  // ...
}

P.S. for our style guide, I would actually prefer a rewrite rule to add curly braces around multiline lambda bodies:

// Before 
foos.map(foo =>
  foo
    .withBar("bar")
    .toString)

// After hypothetical rewrite rule
foos.map(foo => {
  foo
    .withBar("bar")
    .toString
})

Do you think this is doable/worthwhile? I'm always happy to contribute if you think it is a good addition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions