2

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 Answers1

3

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
stefanobaghino
  • 11,253
  • 4
  • 35
  • 63
  • Well, it's an option for a one-time occurrence, but I need a configurable solution. – Anton Filimonov Apr 25 '23 at 15:15
  • What would you like to happen? Preserve all the line breaks? Insert line breaks in non-well-formatted code? In which contexts? – stefanobaghino Apr 25 '23 at 15:19
  • The format off option completely disables the formatting of the code, but I want to have formatting enabled with keeping the line breaks inside a List – Anton Filimonov Apr 26 '23 at 08:20
  • Unfortunately I don't think there is an option specifically to only keep line breaks specifically when running the format on a specific constructor. Since this is a function application (constructing the list), the point of view of the `scalafmt` is most likely that you either say that all function applications need to always have a line break in between arguments or none of them should. – stefanobaghino Apr 26 '23 at 08:47
  • 1
    Fair enough. It would be nice to have some config option to keep the newlines in all places (at least). I thought `newlines.source = keep` may help but... – Anton Filimonov Apr 26 '23 at 13:32
  • Looking at the FAQs it looks like this is almost an anti-goal for `scalafmt`. You might want to have a look at `scalariform` perhaps. https://scalameta.org/scalafmt/docs/faq.html#why-not-scalariform – stefanobaghino Apr 26 '23 at 13:59
  • 1
    Although `scalariform` looks abandoned... – stefanobaghino Apr 26 '23 at 14:00