Skip to content

Commit

Permalink
Rewrite: replace delimiters with space
Browse files Browse the repository at this point in the history
Parens/braces serve as delimiters (such as in "if(x"), so when we remove
them, we must ensure that tokens before and after continue to be clearly
separated. For that, let's just replace these delimiters with spaces, as
formatter will remove any redundant space later.
  • Loading branch information
kitbellew committed Jul 25, 2020
1 parent af06ddc commit fdc6ca3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
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 @@ -96,8 +96,8 @@ object a {
}
}
>>>
test does not parse
object a {
a match {
case a ifa =>
^
case a if a =>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -856,4 +856,4 @@ object Issue {
2
}op2 3
>>>
1 op12op2 3
1 op1 2 op2 3
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ object a {
}
}
>>>
test does not parse
object a {
a match {
case a ifa =>
^
case a if a =>
}
}

0 comments on commit fdc6ca3

Please sign in to comment.