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

FormatOps: find stmt starts after redundant parens #2715

Merged
merged 2 commits into from
Aug 31, 2021
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 @@ -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
}
}