Scalafmt: keep empty lines in a List

208 views Asked by At

I want to keep a single empty line in the List definition using the scalafmt formatter:

List(
  "some-random-string1"
    .trim
    .stripMargin,

  "some-random-string2"
    .intern
    .dropRight(3),

  "some-random-string3"
    .takeRight(4)
    .substring(10)
)

The formatter removes the empty lines and breaks the readability of such code. My .scalafmt.conf configuration:

version = "3.7.3"
runner.dialect = scala3
align.preset = most
newlines.source = keep

I want to find a way to configure the formatter to keep the empty lines.

1

There are 1 answers

7
stefanobaghino On

If this is a one-time occurrence in your code, you might want to use // format: off and // format: on around the relevant code to ask scalafmt to preserve its current formatting (documentation):

// format: off
List(
  "some-random-string1"
    .trim
    .stripMargin,

  "some-random-string2"
    .intern
    .dropRight(3),

  "some-random-string3"
    .takeRight(4)
    .substring(10)
)
// format: on