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

Rewrite: replace delimiters with space #2121

Merged
merged 2 commits into from
Jul 25, 2020
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 @@ -78,17 +78,13 @@ class RedundantBraces(implicit ctx: RewriteCtx) extends RewriteSession {
arg.value.head == '_' || isLiteralIdentifier(arg)

t.parts.tail.zip(t.args).foreach {
case (Lit(value: String), arg @ Term.Name(_))
case (Lit.String(value), arg: Term.Name)
if !isIdentifierAtStart(value) && !shouldTermBeEscaped(arg) =>
val openBrace = prevToken(arg.tokens.head)
val closeBrace = nextToken(arg.tokens.head)
(openBrace, closeBrace) match {
case (LeftBrace(), RightBrace()) =>
ctx.addPatchSet(
TokenPatch.Remove(openBrace),
TokenPatch.Remove(closeBrace)
)
case _ =>
val open = prevToken(arg.tokens.head)
if (open.is[LeftBrace]) {
val close = nextToken(arg.tokens.head)
if (close.is[RightBrace])
ctx.addPatchSet(TokenPatch.Remove(open), TokenPatch.Remove(close))
}
case _ =>
}
Expand Down Expand Up @@ -140,8 +136,7 @@ class RedundantBraces(implicit ctx: RewriteCtx) extends RewriteSession {
val lparen = ctx.getMatching(rparen)
implicit val builder = Seq.newBuilder[TokenPatch]
builder += TokenPatch.Replace(lparen, lbrace.text)
builder += TokenPatch.Remove(lbrace)
builder += TokenPatch.Remove(rparen)
removeBraces(lbrace, rparen)
ctx.removeLFToAvoidEmptyLine(rparen)
ctx.addPatchSet(builder.result(): _*)
}
Expand All @@ -161,8 +156,7 @@ class RedundantBraces(implicit ctx: RewriteCtx) extends RewriteSession {
val lbrace = ctx.getMatching(rbrace)
if (lbrace.start <= body.tokens.head.start) {
implicit val builder = Seq.newBuilder[TokenPatch]
builder += TokenPatch.Remove(lbrace)
builder += TokenPatch.Remove(rbrace)
removeBraces(lbrace, rbrace)
ctx.removeLFToAvoidEmptyLine(rbrace)
ctx.addPatchSet(builder.result(): _*)
}
Expand Down Expand Up @@ -207,8 +201,7 @@ class RedundantBraces(implicit ctx: RewriteCtx) extends RewriteSession {
true
}
if (ok) {
builder += TokenPatch.Remove(open)
builder += TokenPatch.Remove(close)
removeBraces(open, close)
ctx.addPatchSet(builder.result(): _*)
}
}
Expand Down Expand Up @@ -343,4 +336,11 @@ class RedundantBraces(implicit ctx: RewriteCtx) extends RewriteSession {
private def getSingleStatIfLineSpanOk(b: Term.Block): Option[Stat] =
getBlockSingleStat(b).filter(getTermLineSpan(_) <= settings.maxLines)

private def removeBraces(lbrace: Token, rbrace: Token)(implicit
builder: Rewrite.PatchBuilder
): Unit = {
builder += TokenPatch.AddLeft(lbrace, " ", keepTok = false)
builder += TokenPatch.AddRight(rbrace, " ", keepTok = false)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,15 @@ class RedundantParens(implicit ctx: RewriteCtx) extends RewriteSession {
val offBeg = minToKeep
if (offBeg <= offEnd) {
implicit val builder = Seq.newBuilder[TokenPatch]
(offBeg to offEnd).foreach { x =>
// replace outer with space, to avoid joining with an adjacent keyword
builder += TokenPatch.AddLeft(toks(beg + offBeg), " ", keepTok = false)
builder += TokenPatch.AddRight(toks(end - offBeg), " ", keepTok = false)
((offBeg + 1) to offEnd).foreach { x =>
builder += TokenPatch.Remove(toks(beg + x))
builder += TokenPatch.Remove(toks(end - x))
}
ctx.removeLFToAvoidEmptyLine(toks(beg + offBeg), toks(beg + offEnd))
ctx.removeLFToAvoidEmptyLine(toks(end - offEnd), toks(end - offBeg))

if (beg > 0) {
toks(beg - 1) match {
case t: Token.KwYield =>
builder += TokenPatch.AddRight(t, " ", keepTok = true)
case _ => ()
}
}

ctx.addPatchSet(builder.result(): _*)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,15 @@ val a = b.c(d =>
val a = b.c(d => {e}, f =>g match { case h =>})
>>>
val a = b.c(d => e, f => g match { case h => })
<<< #2117 if without a space
object a {
a match {
case a if{a} =>
}
}
>>>
object a {
a match {
case a if a =>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -851,3 +851,9 @@ object Issue {
val _ = f()
}
}
<<< #2117 maintain delimiter after removing newline in infix
1 op1{
2
}op2 3
>>>
1 op1 2 op2 3
14 changes: 13 additions & 1 deletion scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,16 @@ object a {
val x = for {
a <- b
} yield a
}
}
<<< #2117 if without a space
object a {
a match {
case a if(((a))) =>
}
}
>>>
object a {
a match {
case a if a =>
}
}