Skip to content

Commit

Permalink
Merge pull request #2715 from kitbellew/2712
Browse files Browse the repository at this point in the history
FormatOps: find stmt starts after redundant parens
  • Loading branch information
tgodzik authored Aug 31, 2021
2 parents 2c350ff + 4b0c782 commit 7b35bb3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class FormatOps(

private[internal] val soft = new SoftKeywordClasses(dialect)
private[internal] val statementStarts =
getStatementStarts(topSourceTree, tokens(_).left, soft)
getStatementStarts(topSourceTree, tokens.after(_).left, soft)
// Maps token to number of non-whitespace bytes before the token's position.
private final val nonWhitespaceOffset: Map[Token, Int] = {
val resultB = Map.newBuilder[Token, Int]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ object TreeOps {
val ret = Map.newBuilder[TokenHash, Tree]
ret.sizeHint(tree.tokens.length)

def addTok(token: Token, tree: Tree) = ret += hash(token) -> tree
def addTok(token: Token, tree: Tree) =
ret += hash(replacedWith(token)) -> 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
28 changes: 28 additions & 0 deletions scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat
Original file line number Diff line number Diff line change
Expand Up @@ -743,3 +743,31 @@ object Application {

def main(args: Array[String]): Unit = app.run(args)
}
<<< #2712 literal
class Toto() {
private def myScalaFmtTest(): String = {
val test = new Toto
("test")
}
}
>>>
class Toto() {
private def myScalaFmtTest(): String = {
val test = new Toto
"test"
}
}
<<< #2712 name
class Toto() {
private def myScalaFmtTest(): String = {
val test = new Toto
(a)
}
}
>>>
class Toto() {
private def myScalaFmtTest(): String = {
val test = new Toto
a
}
}

0 comments on commit 7b35bb3

Please sign in to comment.