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

AvoidInfix: handle targs and multi-arg case #3201

Merged
merged 2 commits into from
May 5, 2022
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 @@ -38,28 +38,48 @@ class AvoidInfix(implicit ctx: RewriteCtx) extends RewriteSession {

override def rewrite(tree: Tree): Unit =
tree match {
case x @ Term.ApplyInfix(lhs, op, _, args) if checkMatchingInfix(x) =>
case x @ Term.ApplyInfix(lhs, op, targs, args) if checkMatchingInfix(x) =>
val builder = Seq.newBuilder[TokenPatch]

val opHead = op.tokens.head
builder += TokenPatch.AddLeft(opHead, ".", keepTok = true)

args match {
case rhs :: Nil =>
rhs.tokens.headOption.foreach { head =>
val last = rhs.tokens.last
val opLast = op.tokens.last
if (!ctx.isMatching(head, last)) {
builder += TokenPatch.AddRight(opLast, "(", keepTok = true)
builder += TokenPatch.AddRight(last, ")", keepTok = true)
} else {
// move delimiter (before comment or newline)
builder +=
TokenPatch.AddRight(opLast, head.syntax, keepTok = true)
builder += TokenPatch.Remove(head)
val opLast = op.tokens.last
val opNextOpt = ctx.tokenTraverser.nextNonTrivialToken(opLast)

def moveOpenDelim(prev: Token, open: Token): Unit = {
// move delimiter (before comment or newline)
builder += TokenPatch.AddRight(prev, open.syntax, keepTok = true)
builder += TokenPatch.Remove(open)
}

// move the left bracket if targs
val argTuple =
if (targs.isEmpty)
for {
opNext <- opNextOpt
} yield (opLast, opNext)
else
for {
lb <- opNextOpt
rb <- ctx.getMatchingOpt(lb)
rbNext <- {
moveOpenDelim(opLast, lb)
ctx.tokenTraverser.nextNonTrivialToken(rb)
}
} yield (rb, rbNext)
// move the left paren if enclosed, else enclose
argTuple.foreach { case (prev, lp) =>
val isEnclosed = args.lastOption.forall { arg =>
val last = arg.tokens.last
val isEnclosed = ctx.getMatchingOpt(lp).exists(last.end <= _.end)
if (!isEnclosed) {
builder += TokenPatch.AddRight(prev, "(", keepTok = true)
builder += TokenPatch.AddRight(last, ")", keepTok = true)
}
case _ => // otherwise, definitely enclosed
isEnclosed
}
if (isEnclosed) moveOpenDelim(prev, lp)
}

val shouldWrapLhs = !isWrapped(lhs) && (lhs match {
Expand Down
99 changes: 99 additions & 0 deletions scalafmt-tests/src/test/resources/rewrite/AvoidInfix3.stat
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,102 @@ object a {
)
)(Location.apply _)
}
<<< args with parens
object a {
b c
()
b c
(())
b c
(d, e)
b c
((d, e))
b c
(c + d).foo
}
>>>
object a {
b.c(
)
b.c(())
b.c(d, e)
b.c((d, e))
b.c((c + d).foo)
}
<<< args with parens and comments
object a {
b c /* c1 */
( /* c2 */ )
b c /* c1 */
( /* c2 */ ( /* c3 */ ))
b c /* c1 */
( /* c2 */ d, e)
b c /* c1 */
( /* c2 */ ( /* c3 */ d, e))
b c /* c1 */
( /* c2 */ c + d /* c3 */ ).foo
}
>>>
object a {
b.c( /* c1 */
/* c2 */
)
b.c( /* c1 */
/* c2 */ (/* c3 */ )
)
b.c( /* c1 */
/* c2 */ d,
e
)
b.c( /* c1 */
/* c2 */ ( /* c3 */ d, e)
)
b.c( /* c1 */
(/* c2 */ c + d /* c3 */ ).foo
)
}
<<< args with parens, targs and comments
object a {
b c /* c1 */
[A] /* c2 */
( /* c3 */ )
b c /* c1 */
[A] /* c2 */
( /* c3 */ d, e)
}
>>>
object a {
b.c[ /* c1 */
A
]( /* c2 */
/* c3 */
)
b.c[ /* c1 */
A
]( /* c2 */
/* c3 */ d,
e
)
}
<<< args with unit, targs and comments
object a {
b c [A] /* c1 */
( /* c2 */ ( /* c3 */ ))
}
>>>
object a {
b.c[A]( /* c1 */
/* c2 */ (/* c3 */ )
)
}
<<< args with tuple, targs and comments
object a {
b c [A] /* c1 */
( /* c2 */ ( /* c3 */ d, e))
}
>>>
object a {
b.c[A]( /* c1 */
/* c2 */ ( /* c3 */ d, e)
)
}
52 changes: 52 additions & 0 deletions scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,55 @@ foo >>= (_.http(registry, port).map(Option.apply(_)).catchSome {
foo >>= (_.http(registry, port).map(Option.apply(_)).catchSome {
bar
})
<<< infix with parens and comments
object a {
b c /* c1 */
( /* c2 */ )
b c /* c1 */
( /* c2 */ ( /* c3 */ ))
b c /* c1 */
( /* c2 */ ( /* c3 */ ( /* c4 */ ) ))
b c /* c1 */
( /* c2 */ d, e)
b c /* c1 */
( /* c2 */ ( /* c3 */ d, e))
b c /* c1 */
( /* c2 */ ( /* c3 */ ( /* c4 */ d, e)))
b c /* c1 */
( /* c2 */ c + d /* c3 */ ).foo
}
>>>
object a {
b c /* c1 */
(/* c2 */ )
b c /* c1 */
(/* c2 */ (/* c3 */ ))
b c /* c1 */
(/* c2 */ (/* c3 */ (/* c4 */ )))
b c /* c1 */
(/* c2 */ d, e)
b c /* c1 */
(/* c2 */ ( /* c3 */ d, e))
b c /* c1 */
(/* c2 */ ( /* c3 */ ( /* c4 */ d, e)))
b c /* c1 */
(/* c2 */ c + d /* c3 */ ).foo
}
<<< infix with parens, targs and comments
object a {
b c /* c1 */
[A] /* c2 */
( /* c3 */ )
b c /* c1 */
[A] /* c2 */
( /* c3 */ d, e)
}
>>>
object a {
b c /* c1 */
[A] /* c2 */
(/* c3 */ )
b c /* c1 */
[A] /* c2 */
(/* c3 */ d, e)
}