Skip to content

Wrong formatting of code blocks with partial functions returning tuples #3844

@pawelsadlo

Description

@pawelsadlo

Please paste the contents of your .scalafmt.conf file here:

version = 3.8.0
runner.dialect = scala3

When I run scalafmt via CLI like this: scalafmt File.scala

Steps

Given code like this:

class Sfmt {
  val pr: PartialFunction[Int, (String, Int)] = {
    case e: Int =>
      val a = ""
      ("first", 2)
  }

  try {
    ()
  } catch {
    case e: Throwable =>
      println("")
      (1, false)
  }


  1 match {
    case 2 =>
      println("")
      (1, false)
  }
  
  List.empty[Matchable].collect{
    case x:Int =>
      val a = 1
      (a)
  }
}

Problem

Scalafmt formats code like this:

class Sfmt {
  val pr: PartialFunction[Int, (String, Int)] = { case e: Int =>
    val a = "" ("first", 2)
  }

  try {
    ()
  } catch {
    case e: Throwable =>
      println("")(1, false)
  }

  1 match {
    case 2 =>
      println("")(1, false)
  }

  List.empty[Matchable].collect { case x: Int =>
    val a = 1 (a)
  }
}

Expectation

I would like the formatted output to look like this:

class Sfmt {
  val pr: PartialFunction[Int, (String, Int)] = { case e: Int =>
    val a = ""
    ("first", 2)
  }

  try {
    ()
  } catch {
    case e: Throwable =>
      println("")
      (1, false)
  }

  1 match {
    case 2 =>
      println("")
      (1, false)
  }

  List.empty[Matchable].collect { case x: Int =>
    val a = 1
    (a)
  }
}

Notes

I gave several examples where the bug occurs.

The pattern is that we have code block with case starting from new line, followed by any line (or lines), followed by line containing code in parenthesis (for example tuples).

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