Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With newlines.source = keep linebreaks are not preserved in lists with non-numeric entries #3894

Closed
OndrejSpanel opened this issue Apr 3, 2024 · 2 comments · Fixed by #3906

Comments

@OndrejSpanel
Copy link

OndrejSpanel commented Apr 3, 2024

Configuration (required)

version = 3.8.1
maxColumn = 120
runner.dialect = scala3
newlines.source = keep

Command-line parameters (required)

When I run scalafmt via CLI like this: scalafmt

Steps

Given code like this:

object Main {
  val a = Set(
    0,
    1, 2, 3,
    4, 5, 6, 7, 8, 9, 10 + 0
  )
  def main(args: Array[String]): Unit = {}
}

Problem

Scalafmt formats code like this:

object Main {
  val a = Set(
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10 + 0
  )
  def main(args: Array[String]): Unit = {}
}

Expectation

I would like the formatted output to look like this:

object Main {
  val a = Set(
    0,
    1, 2, 3,
    4, 5, 6, 7, 8, 9, 10 + 0
  )
  def main(args: Array[String]): Unit = {}
}

Notes

When all values are numeric literals. it works fine. As soon as any single value is anything else, line-breaks are inserted.

@kitbellew
Copy link
Collaborator

this type of formatting is called binpacking, and there's a separate parameter for that. source=keep doesn't guarantee all breaks will be preserved, only when it doesn't contradict other parameters or other constraints.

@OndrejSpanel
Copy link
Author

Good to know, but even with binPack.literalArgumentLists = false this is still formatted, as the last value is not a literal.

I have tried following:

binPack.literalArgumentLists = true
binPack.literalsInclude = [".*"]
binPack.literalsIncludeSimpleExpr = true

No matter what I have tried, I am unable to preserve my newlines in such lists. I can use // format: off, but as I prefer this behaviour everywhere in my sources, that does not look very nice.

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 a pull request may close this issue.

2 participants