From 593547514721af8ef8b12e03970dc5a9e08b2331 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 8 Dec 2021 20:30:47 -0800 Subject: [PATCH] FormatWriter: add braces if span exceeds threshold --- .../org/scalafmt/internal/FormatOps.scala | 161 ++++++++++++++++++ .../org/scalafmt/internal/FormatWriter.scala | 97 ++++++++++- .../resources/newlines/source_classic.stat | 140 +++++++++------ .../test/resources/newlines/source_fold.stat | 81 +++++---- .../test/resources/newlines/source_keep.stat | 140 +++++++++------ .../resources/newlines/source_unfold.stat | 152 +++++++++++------ .../test/resources/scala3/OptionalBraces.stat | 140 +++++++++------ .../resources/scala3/OptionalBraces_fold.stat | 81 +++++---- .../resources/scala3/OptionalBraces_keep.stat | 140 +++++++++------ .../scala3/OptionalBraces_unfold.stat | 152 +++++++++++------ 10 files changed, 917 insertions(+), 367 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index e7ec0218f1..99731c8218 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -2527,6 +2527,167 @@ class FormatOps( } + object MissingBraces { + + type Ranges = Seq[(Tree, Tree)] + type Result = Option[(Tree, Ranges)] + + private trait Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result + } + + def getBlocks(ft: FormatToken, all: Boolean): Result = { + val nft = nextNonComment(ft) + if (nft.right.is[T.LeftBrace]) None + else { + val impl = ft.left match { + case _: T.RightArrow => RightArrowImpl + case _: T.RightParen => RightParenImpl + case _: T.RightBrace => RightBraceImpl + case _: T.KwDo => DoImpl + case _: T.Equals => EqualsImpl + case _: T.KwTry => TryImpl + case _: T.KwCatch => CatchImpl + case _: T.KwFinally => FinallyImpl + case _: T.KwElse => ElseImpl + case _: T.KwYield => YieldImpl + case _ => null + } + Option(impl).flatMap(_.getBlocks(ft, nft, all)) + } + } + + private def seq(all: Boolean, t: Tree): Ranges = + if (all) Seq(t -> t) else Nil + + private def seq(all: Boolean, t: Option[Tree]): Ranges = + t.map(seq(all, _)).getOrElse(Nil) + + private def seq(all: Boolean, t: Seq[Tree]): Ranges = + if (all && t.nonEmpty) Seq(t.head -> t.last) else Nil + + private def seq(all: Boolean, t: Tree, ts: Seq[Tree]): Ranges = + if (all) Seq(t -> ts.lastOption.getOrElse(t)) else Nil + + private object BlockImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = { + def ok(stat: Tree): Boolean = stat.tokens.headOption.contains(nft.right) + val leftOwner = ft.meta.leftOwner + findTreeWithParentSimple(nft.meta.rightOwner)(_ eq leftOwner) match { + case Some(t: Term.Block) => + if (t.stats.headOption.exists(ok)) Some((t, Nil)) else None + case x => x.filter(ok).map((_, Nil)) + } + } + } + + private object RightArrowImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = + ft.meta.leftOwner match { + case t @ Term.Function(p, b) => + val skip = t.parent.exists(_.is[Term.Block]) + if (skip) None else Some((b, seq(all, p))) + case _ => None + } + } + + private object RightParenImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = + ft.meta.leftOwner match { + case x @ Term.If(c, t, e) if !nft.right.is[T.KwThen] => + Some((t, seq(all && !ifWithoutElse(x), e) ++ seq(all, c))) + case Term.For(s, b) if !nft.right.is[T.KwDo] => + Some(b, seq(all, s)) + case Term.While(c, b) if !nft.right.is[T.KwDo] => + Some(b, seq(all, c)) + case _ => None + } + } + + private object RightBraceImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = + ft.meta.leftOwner match { + case t @ Term.For(s, b) + if !nft.right.is[T.KwDo] && !isLastToken(ft.left, t) => + Some(b, seq(all, s)) + case _ => None + } + } + + private object DoImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = + ft.meta.leftOwner match { + case Term.Do(b, c) => Some(b, seq(all, c)) + case _ => None + } + } + + private object EqualsImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = + ft.meta.leftOwner match { + case t: Ctor.Secondary => Some(t, seq(all, t.init, t.stats)) + case t: Defn.Def => Some(t.body, Nil) + case t: Defn.Macro => Some(t.body, Nil) + case t: Term.Assign => Some(t.rhs, Nil) + case t: Defn.Type => Some(t.body, Nil) + case t: Defn.Val => Some(t.rhs, Nil) + case t: Defn.Var => t.rhs.map(_ -> Nil) + case _ => BlockImpl.getBlocks(ft, nft, all) + } + } + + private object TryImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = + ft.meta.leftOwner match { + case t: Term.Try => + Some(t.expr, seq(all, t.catchp) ++ seq(all, t.finallyp)) + case t: Term.TryWithHandler => + Some(t.expr, seq(all, t.catchp) ++ seq(all, t.finallyp)) + case _ => None + } + } + + private object CatchImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = + ft.meta.leftOwner match { + case t: Term.Try => + Some(t.catchp.last, seq(all, t.expr) ++ seq(all, t.finallyp)) + case t: Term.TryWithHandler => + Some(t.catchp, seq(all, t.expr) ++ seq(all, t.finallyp)) + case _ => None + } + } + + private object FinallyImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = + ft.meta.leftOwner match { + case t: Term.Try => + t.finallyp.map(x => (x, seq(all, t.expr) ++ seq(all, t.catchp))) + case t: Term.TryWithHandler => + t.finallyp.map(x => (x, seq(all, t.expr) ++ seq(all, t.catchp))) + case _ => None + } + } + + private object ElseImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = + ft.meta.leftOwner match { + case Term.If(c, t, e) if !e.is[Term.If] => + Some((e, seq(all, t) ++ seq(all, c))) + case _ => None + } + } + + private object YieldImpl extends Factory { + def getBlocks(ft: FormatToken, nft: FormatToken, all: Boolean): Result = + ft.meta.leftOwner match { + case Term.ForYield(s, b) => Some((b, seq(all, s))) + case _ => None + } + } + + } + def isBlockWithoutOptionalBraces(t: Term.Block): Boolean = isSingleStatBlock(t) && ( t.tokens.head match { diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala index 4802bb676b..fc276508e6 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala @@ -51,6 +51,7 @@ class FormatWriter(formatOps: FormatOps) { val location = entry.curr implicit val style: ScalafmtConfig = location.style val formatToken = location.formatToken + var skipWs = false formatToken.left match { case _ if entry.previous.formatToken.meta.formatOff => @@ -96,7 +97,20 @@ class FormatWriter(formatOps: FormatOps) { } } - entry.formatWhitespace + // missing braces + if (location.missingBracesIndent.nonEmpty) { + location.missingBracesIndent.toSeq + .sorted(Ordering.Int.reverse) + .foreach(i => sb.append('\n').append(getIndentation(i)).append("}")) + if (location.missingBracesOpenOrTuck) { + skipWs = true + sb.append(" ") + } else if (formatToken.right.is[T.RightParen]) + skipWs = true + } else if (location.missingBracesOpenOrTuck) + sb.append(" {") + + if (!skipWs) entry.formatWhitespace } sb.toString() @@ -129,6 +143,8 @@ class FormatWriter(formatOps: FormatOps) { if (initStyle.rewrite.scala3.insertEndMarkerMinLines > 0) checkInsertEndMarkers(result) } + if (initStyle.rewrite.insertBraces.minLines > 0) + checkInsertBraces(result) if ( initStyle.rewrite.rules.contains(RedundantBraces) && !initStyle.rewrite.redundantBraces.parensForOneLineApply.contains(false) @@ -338,6 +354,82 @@ class FormatWriter(formatOps: FormatOps) { } } + private def checkInsertBraces(locations: Array[FormatLocation]): Unit = { + def checkInfix(tree: Tree): Boolean = tree match { + case ai @ Term.ApplyInfix(lhs, op, _, rhs) => { + isEnclosedInMatching(ai) || + tokens.prevNonCommentSameLine(tokens.tokenJustBefore(op)).noBreak && + checkInfix(lhs) && (rhs.lengthCompare(1) != 0 || checkInfix(rhs.head)) + } + case _ => true + } + var addedLines = 0 + val willAddLines = new ListBuffer[Int] + locations.foreach { x => + val idx = x.formatToken.meta.idx + val floc = if (addedLines > 0 && x.isNotRemoved) { + val floc = x.copy(leftLineId = x.leftLineId - addedLines) + locations(idx) = floc + floc + } else x + if (willAddLines.nonEmpty && willAddLines(0) == idx) { + addedLines += 1 + willAddLines.remove(0) + } + @tailrec + def hasBreakAfter(i: Int): Boolean = i < locations.length && { + val x = locations(i) + if (!x.isNotRemoved) hasBreakAfter(i + 1) + else if (x.hasBreakAfter) true + else if (!x.formatToken.right.is[T.Comment]) false + else hasBreakAfter(i + 1) + } + val style = floc.style + val ib = style.rewrite.insertBraces + val ft = floc.formatToken + val ok = !ft.meta.formatOff && ib.minLines > 0 && + floc.missingBracesIndent.isEmpty + val mb = + if (ok) formatOps.MissingBraces.getBlocks(ft, ib.allBlocks).filter { + case (mb, _) => checkInfix(mb) && hasBreakAfter(idx) + } + else None + mb.foreach { case (owner, otherBlocks) => + val endFt = tokens.nextNonCommentSameLine(tokens.getLast(owner)) + val end = endFt.meta.idx + val eLoc = locations(end) + val begIndent = floc.state.prev.indentation + def checkSpan: Boolean = + getLineDiff(floc, eLoc) + addedLines >= ib.minLines || + otherBlocks.exists { case (b, e) => + val bIdx = tokens.tokenJustBefore(b).meta.idx + val eIdx = tokens.getLast(e).meta.idx + val span = getLineDiff(locations(bIdx), locations(eIdx)) + ib.minLines <= + (if (bIdx <= idx && eIdx > idx) span + addedLines else span) + } + if ( + !endFt.meta.formatOff && eLoc.hasBreakAfter && + !eLoc.missingBracesIndent.contains(begIndent) && checkSpan + ) { + val addLine = style.newlines.alwaysBeforeElseAfterCurlyIf || + (endFt.right match { + case _: T.KwElse | _: T.KwCatch | _: T.KwFinally => + !owner.parent.contains(endFt.meta.rightOwner) + case _ => true + }) + if (addLine) willAddLines.prepend(end) + locations(idx) = floc.copy(missingBracesOpenOrTuck = true) + locations(end) = eLoc.copy( + missingBracesOpenOrTuck = !addLine && + (eLoc.missingBracesIndent.isEmpty || eLoc.missingBracesOpenOrTuck), + missingBracesIndent = eLoc.missingBracesIndent + begIndent + ) + } + } + } + } + class FormatLocations(val locations: Array[FormatLocation]) { val tokenAligns: Map[Int, Int] = alignmentTokens @@ -1494,6 +1586,9 @@ object FormatWriter { leftLineId: Int, // counts back from the end of the file shift: Int = 0, optionalBraces: Map[Int, Tree] = Map.empty, + // if indent is empty, indicates open; otherwise, whether to tuck + missingBracesOpenOrTuck: Boolean = false, + missingBracesIndent: Set[Int] = Set.empty, replace: String = null ) { def hasBreakAfter: Boolean = state.split.isNL diff --git a/scalafmt-tests/src/test/resources/newlines/source_classic.stat b/scalafmt-tests/src/test/resources/newlines/source_classic.stat index 86f91d8180..6a73250dcc 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_classic.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_classic.stat @@ -4792,15 +4792,18 @@ object a { } >>> object a { - def foo = + def foo = { a + b - val foo = + } + val foo = { a + b - var foo = + } + var foo = { a + b + } } <<< #2019 if-else !alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -4832,29 +4835,34 @@ object a { } >>> object a { - def foo = if (a) + def foo = if (a) { a + b - else + } else { a + b - val foo = - if (a) + } + val foo = { + if (a) { a + b - else if (b) + } else if (b) { a + b - else if (c) + } else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } else { a + b + } + } } <<< #2019 if-else alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -4886,29 +4894,38 @@ object a { } >>> object a { - def foo = if (a) + def foo = if (a) { a + b - else + } + else { a + b - val foo = - if (a) + } + val foo = { + if (a) { a + b - else if (b) + } + else if (b) { a + b - else if (c) + } + else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } + else { a + b + } + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -4927,14 +4944,15 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch bar finally a + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -4954,14 +4972,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch { bar - finally + } finally { a + } + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -4980,14 +5000,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b + } catch bar finally a + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -5007,14 +5029,18 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } + catch { bar - finally + } + finally { a + } + } } <<< #2019 function maxColumn = 12 @@ -5037,10 +5063,10 @@ object a { } >>> object a { - foo(x => + foo(x => { a + b - ) + }) foo(x => a + b @@ -5092,9 +5118,10 @@ object a { a for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-yield allBlocks maxColumn = 12 @@ -5117,13 +5144,15 @@ object a { for { a <- b c <- d - } yield // c + } yield { // c a + } for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-do !allBlocks maxColumn = 12 @@ -5162,29 +5191,34 @@ object a { a for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b } a + @@ -5224,33 +5258,39 @@ object a { for { a <- b c <- d - } // c + } { // c a + } for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b } a + diff --git a/scalafmt-tests/src/test/resources/newlines/source_fold.stat b/scalafmt-tests/src/test/resources/newlines/source_fold.stat index 805891daf4..818d452dc2 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_fold.stat @@ -4620,23 +4620,26 @@ object a { } >>> object a { - def foo = + def foo = { if (a) a + b else a + b - val foo = + } + val foo = { if (a) a + b else if (b) a + b else if (c) a + b - var foo = + } + var foo = { if (a) a + b else a + b + } } <<< #2019 if-else alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -4668,23 +4671,26 @@ object a { } >>> object a { - def foo = + def foo = { if (a) a + b else a + b - val foo = + } + val foo = { if (a) a + b else if (b) a + b else if (c) a + b - var foo = + } + var foo = { if (a) a + b else a + b + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 13 @@ -4703,14 +4709,15 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { aaaa + b - catch + } catch barbar finally aaaaa + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 13 @@ -4730,14 +4737,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { aaaa + b - catch + } catch { barbar - finally + } finally { aaaaa + } + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 13 @@ -4756,14 +4765,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { aaaa + b + } catch barbar finally aaaaa + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 13 @@ -4783,14 +4794,18 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { aaaa + b - catch + } + catch { barbar - finally + } + finally { aaaaa + } + } } <<< #2019 function maxColumn = 12 @@ -4813,10 +4828,10 @@ object a { } >>> object a { - foo(x => + foo(x => { a + b - ) + }) foo(x => a + b @@ -4868,9 +4883,10 @@ object a { a for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-yield allBlocks maxColumn = 12 @@ -4893,13 +4909,15 @@ object a { for { a <- b c <- d - } yield // c + } yield { // c a + } for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-do !allBlocks maxColumn = 12 @@ -4938,14 +4956,16 @@ object a { a for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b ) a + @@ -4997,18 +5017,21 @@ object a { for { a <- b c <- d - } // c + } { // c a + } for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b ) a + diff --git a/scalafmt-tests/src/test/resources/newlines/source_keep.stat b/scalafmt-tests/src/test/resources/newlines/source_keep.stat index 6a851df00f..5338b1bfe1 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_keep.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_keep.stat @@ -4823,15 +4823,18 @@ object a { } >>> object a { - def foo = + def foo = { a + b - val foo = + } + val foo = { a + b - var foo = + } + var foo = { a + b + } } <<< #2019 if-else !alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -4863,30 +4866,36 @@ object a { } >>> object a { - def foo = - if (a) + def foo = { + if (a) { a + b - else + } else { a + b - val foo = - if (a) + } + } + val foo = { + if (a) { a + b - else if (b) + } else if (b) { a + b - else if (c) + } else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } else { a + b + } + } } <<< #2019 if-else alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -4918,30 +4927,40 @@ object a { } >>> object a { - def foo = - if (a) + def foo = { + if (a) { a + b - else + } + else { a + b - val foo = - if (a) + } + } + val foo = { + if (a) { a + b - else if (b) + } + else if (b) { a + b - else if (c) + } + else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } + else { a + b + } + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -4960,14 +4979,15 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch bar finally a + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -4987,14 +5007,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch { bar - finally + } finally { a + } + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -5013,14 +5035,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b + } catch bar finally a + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -5040,14 +5064,18 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } + catch { bar - finally + } + finally { a + } + } } <<< #2019 function maxColumn = 12 @@ -5070,10 +5098,10 @@ object a { } >>> object a { - foo(x => + foo(x => { a + b - ) + }) foo(x => a + b @@ -5125,9 +5153,10 @@ object a { a for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-yield allBlocks maxColumn = 12 @@ -5150,13 +5179,15 @@ object a { for { a <- b c <- d - } yield // c + } yield { // c a + } for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-do !allBlocks maxColumn = 12 @@ -5195,24 +5226,28 @@ object a { a for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b ) a + @@ -5256,28 +5291,33 @@ object a { for { a <- b c <- d - } // c + } { // c a + } for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b ) a + diff --git a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat index 5c3a77efe7..5eef7d178d 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat @@ -5033,15 +5033,18 @@ object a { } >>> object a { - def foo = + def foo = { a + b - val foo = + } + val foo = { a + b - var foo = + } + var foo = { a + b + } } <<< #2019 if-else !alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -5073,30 +5076,36 @@ object a { } >>> object a { - def foo = - if (a) + def foo = { + if (a) { a + b - else + } else { a + b - val foo = - if (a) + } + } + val foo = { + if (a) { a + b - else if (b) + } else if (b) { a + b - else if (c) + } else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } else { a + b + } + } } <<< #2019 if-else alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -5128,30 +5137,40 @@ object a { } >>> object a { - def foo = - if (a) + def foo = { + if (a) { a + b - else + } + else { a + b - val foo = - if (a) + } + } + val foo = { + if (a) { a + b - else if (b) + } + else if (b) { a + b - else if (c) + } + else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } + else { a + b + } + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -5170,14 +5189,15 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch bar finally a + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -5197,14 +5217,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch { bar - finally + } finally { a + } + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -5223,14 +5245,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b + } catch bar finally a + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -5250,14 +5274,18 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } + catch { bar - finally + } + finally { a + } + } } <<< #2019 function maxColumn = 12 @@ -5280,10 +5308,10 @@ object a { } >>> object a { - foo(x => + foo(x => { a + b - ) + }) foo(x => a + b @@ -5335,9 +5363,10 @@ object a { a for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-yield allBlocks maxColumn = 12 @@ -5360,13 +5389,15 @@ object a { for { a <- b c <- d - } yield // c + } yield { // c a + } for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-do !allBlocks maxColumn = 12 @@ -5405,34 +5436,40 @@ object a { a for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } } <<< #2019 for-do allBlocks maxColumn = 12 @@ -5468,36 +5505,43 @@ object a { for { a <- b c <- d - } // c + } { // c a + } for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } } diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat index 897ee529a5..27fa252072 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat @@ -2466,15 +2466,18 @@ object a { } >>> object a { - def foo = + def foo = { a + b - val foo = + } + val foo = { a + b - var foo = + } + var foo = { a + b + } } <<< #2019 if-else !alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -2506,29 +2509,34 @@ object a { } >>> object a { - def foo = if (a) + def foo = if (a) { a + b - else + } else { a + b - val foo = - if (a) + } + val foo = { + if (a) { a + b - else if (b) + } else if (b) { a + b - else if (c) + } else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } else { a + b + } + } } <<< #2019 if-else alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -2560,29 +2568,38 @@ object a { } >>> object a { - def foo = if (a) + def foo = if (a) { a + b - else + } + else { a + b - val foo = - if (a) + } + val foo = { + if (a) { a + b - else if (b) + } + else if (b) { a + b - else if (c) + } + else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } + else { a + b + } + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -2601,14 +2618,15 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch bar finally a + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -2628,14 +2646,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch { bar - finally + } finally { a + } + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -2654,14 +2674,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b + } catch bar finally a + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -2681,14 +2703,18 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } + catch { bar - finally + } + finally { a + } + } } <<< #2019 function maxColumn = 12 @@ -2711,10 +2737,10 @@ object a { } >>> object a { - foo(x => + foo(x => { a + b - ) + }) foo(x => a + b @@ -2766,9 +2792,10 @@ object a { a for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-yield allBlocks maxColumn = 12 @@ -2791,13 +2818,15 @@ object a { for { a <- b c <- d - } yield // c + } yield { // c a + } for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-do !allBlocks maxColumn = 12 @@ -2836,29 +2865,34 @@ object a { a for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b } a + @@ -2898,33 +2932,39 @@ object a { for { a <- b c <- d - } // c + } { // c a + } for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b } a + diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat index beba8c43d5..47a30c72ce 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat @@ -2387,23 +2387,26 @@ object a { } >>> object a { - def foo = + def foo = { if (a) a + b else a + b - val foo = + } + val foo = { if (a) a + b else if (b) a + b else if (c) a + b - var foo = + } + var foo = { if (a) a + b else a + b + } } <<< #2019 if-else alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -2435,23 +2438,26 @@ object a { } >>> object a { - def foo = + def foo = { if (a) a + b else a + b - val foo = + } + val foo = { if (a) a + b else if (b) a + b else if (c) a + b - var foo = + } + var foo = { if (a) a + b else a + b + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 13 @@ -2470,14 +2476,15 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { aaaa + b - catch + } catch barbar finally aaaaa + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 13 @@ -2497,14 +2504,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { aaaa + b - catch + } catch { barbar - finally + } finally { aaaaa + } + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 13 @@ -2523,14 +2532,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { aaaa + b + } catch barbar finally aaaaa + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 13 @@ -2550,14 +2561,18 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { aaaa + b - catch + } + catch { barbar - finally + } + finally { aaaaa + } + } } <<< #2019 function maxColumn = 12 @@ -2580,10 +2595,10 @@ object a { } >>> object a { - foo(x => + foo(x => { a + b - ) + }) foo(x => a + b @@ -2635,9 +2650,10 @@ object a { a for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-yield allBlocks maxColumn = 12 @@ -2660,13 +2676,15 @@ object a { for { a <- b c <- d - } yield // c + } yield { // c a + } for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-do !allBlocks maxColumn = 12 @@ -2705,14 +2723,16 @@ object a { a for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b ) a + @@ -2764,18 +2784,21 @@ object a { for { a <- b c <- d - } // c + } { // c a + } for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b ) a + diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat index 92ebbceb95..4ab73dbef3 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat @@ -2479,15 +2479,18 @@ object a { } >>> object a { - def foo = + def foo = { a + b - val foo = + } + val foo = { a + b - var foo = + } + var foo = { a + b + } } <<< #2019 if-else !alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -2519,30 +2522,36 @@ object a { } >>> object a { - def foo = - if (a) + def foo = { + if (a) { a + b - else + } else { a + b - val foo = - if (a) + } + } + val foo = { + if (a) { a + b - else if (b) + } else if (b) { a + b - else if (c) + } else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } else { a + b + } + } } <<< #2019 if-else alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -2574,30 +2583,40 @@ object a { } >>> object a { - def foo = - if (a) + def foo = { + if (a) { a + b - else + } + else { a + b - val foo = - if (a) + } + } + val foo = { + if (a) { a + b - else if (b) + } + else if (b) { a + b - else if (c) + } + else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } + else { a + b + } + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -2616,14 +2635,15 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch bar finally a + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -2643,14 +2663,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch { bar - finally + } finally { a + } + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -2669,14 +2691,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b + } catch bar finally a + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -2696,14 +2720,18 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } + catch { bar - finally + } + finally { a + } + } } <<< #2019 function maxColumn = 12 @@ -2726,10 +2754,10 @@ object a { } >>> object a { - foo(x => + foo(x => { a + b - ) + }) foo(x => a + b @@ -2781,9 +2809,10 @@ object a { a for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-yield allBlocks maxColumn = 12 @@ -2806,13 +2835,15 @@ object a { for { a <- b c <- d - } yield // c + } yield { // c a + } for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-do !allBlocks maxColumn = 12 @@ -2851,24 +2882,28 @@ object a { a for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b ) a + @@ -2912,28 +2947,33 @@ object a { for { a <- b c <- d - } // c + } { // c a + } for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b ) a + diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat index 21d337bc4b..eb3d5f127e 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat @@ -2571,15 +2571,18 @@ object a { } >>> object a { - def foo = + def foo = { a + b - val foo = + } + val foo = { a + b - var foo = + } + var foo = { a + b + } } <<< #2019 if-else !alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -2611,30 +2614,36 @@ object a { } >>> object a { - def foo = - if (a) + def foo = { + if (a) { a + b - else + } else { a + b - val foo = - if (a) + } + } + val foo = { + if (a) { a + b - else if (b) + } else if (b) { a + b - else if (c) + } else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } else { a + b + } + } } <<< #2019 if-else alwaysBeforeElseAfterCurlyIf newlines.afterInfix = keep @@ -2666,30 +2675,40 @@ object a { } >>> object a { - def foo = - if (a) + def foo = { + if (a) { a + b - else + } + else { a + b - val foo = - if (a) + } + } + val foo = { + if (a) { a + b - else if (b) + } + else if (b) { a + b - else if (c) + } + else if (c) { a + b - var foo = - if (a) + } + } + var foo = { + if (a) { a + b - else + } + else { a + b + } + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -2708,14 +2727,15 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch bar finally a + } } <<< #2019 try !alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -2735,14 +2755,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } catch { bar - finally + } finally { a + } + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf !allBlocks maxColumn = 12 @@ -2761,14 +2783,16 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b + } catch bar finally a + } } <<< #2019 try alwaysBeforeElseAfterCurlyIf allBlocks maxColumn = 12 @@ -2788,14 +2812,18 @@ object a { } >>> object a { - def foo = - try + def foo = { + try { a + b - catch + } + catch { bar - finally + } + finally { a + } + } } <<< #2019 function maxColumn = 12 @@ -2818,10 +2846,10 @@ object a { } >>> object a { - foo(x => + foo(x => { a + b - ) + }) foo(x => a + b @@ -2873,9 +2901,10 @@ object a { a for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-yield allBlocks maxColumn = 12 @@ -2898,13 +2927,15 @@ object a { for { a <- b c <- d - } yield // c + } yield { // c a + } for { a <- b - } yield // c + } yield { // c a + b + } } <<< #2019 for-do !allBlocks maxColumn = 12 @@ -2943,34 +2974,40 @@ object a { a for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } } <<< #2019 for-do allBlocks maxColumn = 12 @@ -3006,36 +3043,43 @@ object a { for { a <- b c <- d - } // c + } { // c a + } for ( a <- b - ) // c + ) { // c a + b + } for { a <- b - } // c + } { // c a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } for ( a <- b - ) + ) { a + b + } for { a <- b - } + } { a + b + } }