Skip to content

Commit

Permalink
FormatOps: Ctrl body can be tuple in removed block
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Aug 7, 2024
1 parent a7de473 commit dfe1047
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1819,18 +1819,20 @@ class FormatOps(
val treeTokens = tree.tokens
val end = getLast(treeTokens, tree)
val slbExpire = nextNonCommentSameLine(end).left
val closeOpt =
if (isTuple(tree)) None
else {
def head = getHead(treeTokens, tree)
val close = (tree match {
case _: Member.Tuple => None
case Term.Block((_: Member.Tuple) :: Nil)
if !head.left.is[T.LeftBrace] => None
case _ =>
val maybeClose = prevNonComment(end)
tokens.getClosingIfInParens(maybeClose)(getHead(treeTokens, tree))
tokens.getClosingIfInParens(maybeClose)(head)
.map(prevNonComment(_).left)
}
}).getOrElse(slbExpire)
def nlPolicy(implicit fileLine: FileLine) = Policy ? danglingKeyword &&
decideNewlinesOnlyAfterClose(closeOpt.getOrElse(slbExpire))
decideNewlinesOnlyAfterClose(close)
val indentLen = indentOpt.getOrElse(style.indent.getSignificant)
val indent =
Indent(Num(indentLen), closeOpt.getOrElse(slbExpire), ExpiresOn.After)
val indent = Indent(Num(indentLen), close, ExpiresOn.After)
if (ft.hasBlankLine)
Seq(Split(Newline2x, 0).withIndent(indent).withPolicy(nlPolicy))
else if (forceNL)
Expand Down
45 changes: 11 additions & 34 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5596,39 +5596,16 @@ object a {
}
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
"only in packlib classpath"
- )
+ )
else
"only in hashbang classpath"
- )
+ )
}
<<< convert to new if-then syntax, remove redundant braces, longer
maxColumn = 77
rewrite {
rules = [RedundantBraces]
scala3.convertToNewSyntax = true
redundantBraces.ifElseExpressions = yes
}
===
object a {
val (diff: Set[File], msg: String) = if (packlibJars.size > hashbangClasspathJars.size) {
(packlibJars.toSet -- hashbangJars.toSet , "only in packlib classpath")
} else {
(hashbangJars.toSet -- packlibJars.toSet , "only in hashbang classpath")
}
val (diff: Set[File], msg: String) =
if packlibJars.size > hashbangClasspathJars.size then
(
packlibJars.toSet -- hashbangJars.toSet,
"only in packlib classpath"
)
else
(
hashbangJars.toSet -- packlibJars.toSet,
"only in hashbang classpath"
)
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
else
- (
- hashbangJars.toSet -- packlibJars.toSet,
- "only in hashbang classpath"
- )
+ (hashbangJars.toSet -- packlibJars.toSet, "only in hashbang classpath"
+ )
}
23 changes: 13 additions & 10 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5352,13 +5352,16 @@ object a {
}
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
"only in packlib classpath"
- )
+ )
else
"only in hashbang classpath"
- )
+ )
}
object a {
val (diff: Set[File], msg: String) =
if packlibJars.size > hashbangClasspathJars.size then
(
packlibJars.toSet -- hashbangJars.toSet,
"only in packlib classpath"
)
else
(
hashbangJars.toSet -- packlibJars.toSet,
"only in hashbang classpath"
)
}
23 changes: 13 additions & 10 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5637,13 +5637,16 @@ object a {
}
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
"only in packlib classpath"
- )
+ )
else
"only in hashbang classpath"
- )
+ )
}
object a {
val (diff: Set[File], msg: String) =
if packlibJars.size > hashbangClasspathJars.size then
(
packlibJars.toSet -- hashbangJars.toSet,
"only in packlib classpath"
)
else
(
hashbangJars.toSet -- packlibJars.toSet,
"only in hashbang classpath"
)
}
23 changes: 13 additions & 10 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5780,13 +5780,16 @@ object a {
}
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
"only in packlib classpath"
- )
+ )
else
"only in hashbang classpath"
- )
+ )
}
object a {
val (diff: Set[File], msg: String) =
if packlibJars.size > hashbangClasspathJars.size then
(
packlibJars.toSet -- hashbangJars.toSet,
"only in packlib classpath"
)
else
(
hashbangJars.toSet -- packlibJars.toSet,
"only in hashbang classpath"
)
}

0 comments on commit dfe1047

Please sign in to comment.