Skip to content
New issue

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

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

Already on GitHub? # to your account

FormatWriter: mind rewritten tokens for {} -> () #3822

Merged
merged 2 commits into from
Mar 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class FormatWriter(formatOps: FormatOps) {
val ok = tok.meta.leftOwner match {
case b: Term.Block =>
checkApply(b) && RedundantBraces.canRewriteBlockWithParens(b) &&
b.parent.exists(_.tokens.last.start == rb.start)
b.parent.exists(tokens.getLast(_) eq tok)
case f: Term.FunctionTerm =>
checkApply(f) && RedundantBraces.canRewriteFuncWithParens(f)
case t @ TreeOps.SingleArgInBraces(arg) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory {
case _ => false
}

def canRewriteBlockWithParens(b: Term.Block): Boolean =
def canRewriteBlockWithParens(b: Term.Block)(implicit
ftoks: FormatTokens
): Boolean =
getBlockSingleStat(b).exists(canRewriteStatWithParens)

def canRewriteStatWithParens(t: Stat): Boolean =
def canRewriteStatWithParens(t: Stat)(implicit ftoks: FormatTokens): Boolean =
t match {
case f: Term.FunctionTerm => canRewriteFuncWithParens(f)
case _: Term.Assign => false // disallowed in 2.13
case _: Defn => false
case _: Term.PartialFunction => false
case b @ Term.Block(s :: Nil) if !ftoks.isEnclosedInMatching(b) =>
canRewriteStatWithParens(s)
case _ => true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1404,3 +1404,9 @@ object A {
Option(""),
).map(identity)
}
<<< func in parens and braces, single line
rewrite.rules = [RedundantBraces, RedundantParens]
===
foo.mtd({ x => x + 1 })
>>>
foo.mtd(x => x + 1)
Loading