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

TreeOps: fix statement start for foo(func) #3217

Merged
merged 2 commits into from
May 16, 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 @@ -93,7 +93,7 @@ class FormatOps(

private[internal] val soft = new SoftKeywordClasses(dialect)
private[internal] val statementStarts =
getStatementStarts(topSourceTree, tokens.after(_).left, soft)
getStatementStarts(topSourceTree, tokens, soft)
// Maps token to number of non-whitespace bytes before the token's position.
private final val nonWhitespaceOffset: Map[T, Int] = {
val resultB = Map.newBuilder[T, Int]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import scala.reflect.ClassTag

import org.scalafmt.Error
import org.scalafmt.config.{DanglingParentheses, ScalafmtConfig}
import org.scalafmt.internal.FormatToken
import org.scalafmt.internal.{FormatToken, FormatTokens}

/** Stateless helper functions on `scala.meta.Tree`.
*/
Expand Down Expand Up @@ -98,14 +98,14 @@ object TreeOps {

def getStatementStarts(
tree: Tree,
replacedWith: Token => Token,
ftoks: FormatTokens,
soft: SoftKeywordClasses
): Map[TokenHash, Tree] = {
val ret = Map.newBuilder[TokenHash, Tree]
ret.sizeHint(tree.tokens.length)

def addTok(token: Token, tree: Tree) =
ret += hash(replacedWith(token)) -> tree
ret += hash(ftoks.after(token).left) -> tree
def addTree(t: Tree, tree: Tree) =
t.tokens.find(!_.is[Trivia]).foreach(addTok(_, tree))
def addAll(trees: Seq[Tree]) = trees.foreach(x => addTree(x, x))
Expand Down Expand Up @@ -168,7 +168,7 @@ object TreeOps {
// special handling for rewritten blocks
case t @ Term.Block(List(arg)) // single-stat block
if t.tokens.headOption // see if opening brace was removed
.exists(x => x.is[Token.LeftBrace] && replacedWith(x).ne(x)) =>
.exists(x => x.is[Token.LeftBrace] && ftoks(x).left.ne(x)) =>
if (arg.is[Term.Function]) {
// handle rewritten apply { { x => b } } to a { x => b }
val parentApply = findTreeWithParent(t) {
Expand All @@ -180,8 +180,10 @@ object TreeOps {
}
// special handling for rewritten apply(x => { b }) to a { x => b }
case Term.Apply(_, List(f: Term.Function))
if subtree.tokens.lastOption // see if closing paren was moved
.exists(x => x.is[Token.RightParen] && replacedWith(x).ne(x)) =>
if subtree.tokens.lastOption.exists { x =>
x.is[Token.RightParen] && // see if closing paren is now brace
ftoks.prevNonComment(ftoks(x)).left.is[Token.RightBrace]
} =>
addAll(Seq(f))
case t => // Nothing
addAll(extractStatementsIfAny(t))
Expand Down
12 changes: 12 additions & 0 deletions scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,33 @@ object a {
object a {
b match { case _ if a || a => b }
}
<<< single-arg apply of a function not in block
object a {
(a(b => c))
}
>>>
object a {
a(b => c)
}
<<< single-arg apply of a block 1
object a {
a({ b => c })
(a({ b => c }))
}
>>>
object a {
a { b => c }
a { b => c }
}
<<< single-arg apply of a block 2
object a {
a({ b; c })
(a({ b; c }))
}
>>>
object a {
a { b; c }
a { b; c }
}
<<< single-arg apply of a block 3
object a {
Expand Down