diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala index 8f3c9800d5..bd4b5baea7 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala @@ -122,9 +122,13 @@ object TreeOps { val ret = Map.newBuilder[Int, Tree] ret.sizeHint(tree.tokens.length) - def addFT(ft: FormatToken, tree: Tree): Unit = ret += ft.meta.idx -> tree - def addTok(token: Token, tree: Tree) = addFT(ftoks.after(token), tree) - def addTree(t: Tree, o: Tree) = ftoks.getHeadOpt(t).foreach(addFT(_, o)) + def addFT(stmt: Tree)(ft: FormatToken): Unit = { + val isComment = ft.left.is[Token.Comment] + val nft = if (isComment) ftoks.nextAfterNonComment(ft) else ft + ret += nft.meta.idx -> stmt + } + def addTok(stmt: Tree)(token: Token) = addFT(stmt)(ftoks.after(token)) + def addTree(t: Tree, stmt: Tree) = ftoks.getHeadOpt(t).foreach(addFT(stmt)) def addOne(t: Tree) = addTree(t, t) def addAll(trees: Seq[Tree]) = trees.foreach(addOne) @@ -142,10 +146,8 @@ object TreeOps { case Some(x) => addTree(x, tree) case _ => // No non-annotation modifier exists, fallback to keyword like `object` - tree.tokens.find(isMatch) match { - case Some(x) => addTok(x, tree) - case None => throw Error.CantFindDefnToken(what, tree) - } + tree.tokens.find(isMatch) + .fold(throw Error.CantFindDefnToken(what, tree))(addTok(tree)) } } def addDefn[T](mods: Seq[Mod], tree: Tree)(implicit diff --git a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-ParenLambdas.stat b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-ParenLambdas.stat index ea6d0d3996..5d06efd527 100644 --- a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-ParenLambdas.stat +++ b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-ParenLambdas.stat @@ -267,23 +267,21 @@ object a { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - base.info.parents.tail -- .forall { -- p => // - every mixin parent is unrelated to (not a subclass of) low and high, i.e., -- val psym = -- p.typeSymbol // we're not mixing in high or low, both are coming from the superclass -- !psym.isNonBottomSubClass(lowClass) && !psym.isNonBottomSubClass( -- highClass -- ) -+ .forall { p => // - every mixin parent is unrelated to (not a subclass of) low and high, i.e., -+ val psym = -+ p.typeSymbol // we're not mixing in high or low, both are coming from the superclass -+ !psym.isNonBottomSubClass(lowClass) && !psym.isNonBottomSubClass( -+ highClass -+ ) - } +object a { + lowClass.isJavaDefined && highClass.isJavaDefined && { // skip if both are java-defined, and + lowClass + .isNonBottomSubClass(highClass) || { // - low <:< high, which means they are overrides in Java and javac is doing the check; or + base.info.parents.tail.forall { + p => // - every mixin parent is unrelated to (not a subclass of) low and high, i.e., + val psym = + p.typeSymbol // we're not mixing in high or low, both are coming from the superclass + !psym.isNonBottomSubClass(lowClass) && !psym.isNonBottomSubClass( + highClass + ) + } + } + } +} <<< #4133 lambda body without a comment rewrite.redundantBraces.maxLines = 100 ===