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

FormatOps: fix leading infix before fewer braces #3859

Merged
merged 1 commit into from Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -508,7 +508,9 @@ class FormatOps(
val mod = getMod(ft)
val modOrNoSplit =
if (mod != Space || isBeforeOp || useSpace) mod else NoSplit
Seq(InfixSplits.withNLIndent(Split(modOrNoSplit, 0))(app, ft))
val split = Split(modOrNoSplit, 0)
if (isBeforeOp && isFewerBracesRhs(app.arg)) Seq(split)
else Seq(InfixSplits.withNLIndent(split)(app, ft))
}
}

Expand Down
Expand Up @@ -1083,6 +1083,17 @@ object TreeOps {
case _ => false
})

@tailrec
def isFewerBracesRhs(
tree: Tree
)(implicit dialect: Dialect, ftoks: FormatTokens): Boolean =
!ftoks.isEnclosedInMatching(tree) && (tree match {
case t: Term.Apply => isFewerBraces(t)
case t: Term.ApplyInfix => isFewerBracesRhs(t.lhs)
case Term.ArgClause(arg :: Nil, _) => isFewerBracesRhs(arg)
case _ => false
})

def isParentAnApply(t: Tree): Boolean =
t.parent.exists(_.is[Term.Apply])

Expand Down
25 changes: 12 additions & 13 deletions scalafmt-tests/src/test/resources/scala3/FewerBraces.stat
Expand Up @@ -2271,16 +2271,15 @@ object a:
x + 3
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
x + 2
- + mtd3 { x =>
- x + 1
- x + 2
- x + 3
- }
+ + mtd3 { x =>
+ x + 1
+ x + 2
+ x + 3
+ }
object a:
mtd1 { x =>
x + 1
}
+ mtd2: x =>
x + 1
x + 2
+ mtd3 { x =>
x + 1
x + 2
x + 3
}
25 changes: 12 additions & 13 deletions scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat
Expand Up @@ -2248,16 +2248,15 @@ object a:
x + 3
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
x + 2
- + mtd3 { x =>
- x + 1
- x + 2
- x + 3
- }
+ + mtd3 { x =>
+ x + 1
+ x + 2
+ x + 3
+ }
object a:
mtd1 { x =>
x + 1
}
+ mtd2: x =>
x + 1
x + 2
+ mtd3 { x =>
x + 1
x + 2
x + 3
}