Skip to content

Commit

Permalink
TreeOps: fix ownership of enclosed trees
Browse files Browse the repository at this point in the history
- exclude only last right paren (control expressions like if/while/for)
- ignore empty tokens (Xml.End, Interpolation.End)
- allow procedure-syntax Decl.Def (no type)
- detect top-level invocation directly, not via elemIdx
  • Loading branch information
kitbellew committed Dec 11, 2022
1 parent e365aab commit 63c8734
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
33 changes: 20 additions & 13 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ object TreeOps {
if (idx == allTokens.length) idx
else {
val tok = allTokens(idx)
if (tok.start >= endPos && elemIdx != 0) idx
if (elem != topSourceTree && tok.start >= endPos) idx
else {
setOwner(tok, elem)
tokenAt(idx + 1)
Expand All @@ -991,14 +991,16 @@ object TreeOps {
if (idx == allTokens.length) idx
else {
val tok = allTokens(idx)
if (nextChild == null && elemIdx != 0 && tok.start >= endPos) idx
else if (nextChild != null && tok.end > nextChildStart) {
val tokStart = tok.start
if (elem != topSourceTree && tokStart >= endPos) idx
else if (nextChild != null && tokStart >= nextChildStart) {
if (prevChild != null) prevLPs = 0
prevChild = nextChild
val nextIdx = treeAt(idx, nextChild, prevLPs)
children match {
case Nil =>
nextChild = null
nextChildStart = endPos
case (head, start) :: rest =>
nextChild = head
children = rest
Expand All @@ -1007,33 +1009,38 @@ object TreeOps {
tokenAt(nextIdx)
} else {
def excludeRightParen: Boolean = elem match {
case t: Term.If => prevChild eq t.cond // `expr` after `mods`
case t: Term.If =>
prevLPs == 1 && prevChild == t.cond // `expr` after `mods`
case _: Term.While | _: Term.For | _: Term.ForYield =>
prevChild eq firstChild // `expr` is first
prevLPs == 1 && prevChild == firstChild // `expr` is first
case _: Term.Tuple | _: Type.Tuple | _: Pat.Tuple |
_: Term.ApplyInfix | _: Pat.ExtractInfix | _: Term.Apply |
_: Pat.Extract | _: Term.Do | _: Term.AnonymousFunction =>
nextChild == null
case _: Decl.Def | _: Decl.Given | _: Type.FunctionType |
_: Defn.Def | _: Defn.Macro | _: Defn.Given |
_: Defn.GivenAlias | _: Defn.ExtensionGroup =>
nextChild != null // body is next
case _: Init | _: Ctor => true
case t: Decl.Def => // only body can have owners modified
nextChild != null || t.decltpe != prevChild
case t: Decl.Given => // only body can have owners modified
nextChild != null || t.decltpe != prevChild
case _: Type.FunctionType | _: Defn.Def | _: Defn.GivenAlias |
_: Defn.Given | _: Defn.Macro | _: Defn.ExtensionGroup =>
nextChild != null // only body can have owners modified
case _: Init => prevChild ne firstChild // include tpe
case _: Ctor => true
case _ => false
}

if (prevParens.nonEmpty && tok.is[RightParen]) {
if (prevChild == null || prevLPs == 0 || excludeRightParen)
if (prevChild == null || prevLPs <= 0 || excludeRightParen)
setOwner(tok, elem)
else {
prevLPs -= 1
setOwner(tok, prevChild)
setOwner(prevParens.head, prevChild)
}
prevLPs -= 1
prevParens = prevParens.tail
} else {
setOwner(tok, elem)
if (!tok.is[Trivia] && nextChild != null) {
if (!tok.is[Trivia] && tokStart != tok.end) {
prevChild = null
if (tok.is[LeftParen]) {
prevLPs += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6484,7 +6484,7 @@ object a {
}
>>>
object a {
info (
info(
"ZKConsumerConnector shutdown completed in " +
(System
.nanoTime() - startTime) / 1000000 + " ms"
Expand Down
8 changes: 5 additions & 3 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -6199,9 +6199,11 @@ object a {
}
>>>
object a {
info ("ZKConsumerConnector shutdown completed in " +
(System.nanoTime() -
startTime) / 1000000 + " ms")
info(
"ZKConsumerConnector shutdown completed in " +
(System.nanoTime() - startTime) /
1000000 + " ms"
)
}
<<< SM 4.7.0: 18
maxColumn = 80
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6492,7 +6492,7 @@ object a {
}
>>>
object a {
info (
info(
"ZKConsumerConnector shutdown completed in " +
(System.nanoTime() - startTime) / 1000000 + " ms"
)
Expand Down
8 changes: 5 additions & 3 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -6665,9 +6665,11 @@ object a {
}
>>>
object a {
info ("ZKConsumerConnector shutdown completed in " +
(System.nanoTime() -
startTime) / 1000000 + " ms")
info(
"ZKConsumerConnector shutdown completed in " +
(System.nanoTime() - startTime) /
1000000 + " ms"
)
}
<<< SM 4.7.0: 18
maxColumn = 80
Expand Down

0 comments on commit 63c8734

Please sign in to comment.