Skip to content

Commit

Permalink
TreeOps: fix statement start for foo(func)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 15, 2022
1 parent 99d8a92 commit 0b40ad6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ object a {
(a(b => c))
}
>>>
Idempotency violated
object a {
a(
b => c)
a(b => c)
}
<<< single-arg apply of a block 1
object a {
Expand Down

0 comments on commit 0b40ad6

Please sign in to comment.