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: handle if as if no else when keep #3176

Merged
merged 2 commits into from
Mar 31, 2022
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 @@ -1689,6 +1689,7 @@ class FormatOps(
def foldedNonEmptyNonComment(
body: Tree,
nlSplitFunc: Int => Split,
isKeep: Boolean,
spaceIndents: Seq[Indent] = Seq.empty
): Seq[Split] = {
def bheadFT = tokens.getHead(body)
Expand Down Expand Up @@ -1748,7 +1749,7 @@ class FormatOps(
}
val adjustedBody = getBlockStat(body)
val (spaceSplit, nlSplit) = adjustedBody match {
case t: Term.If if ifWithoutElse(t) || hasStateColumn =>
case t: Term.If if isKeep || ifWithoutElse(t) || hasStateColumn =>
val thenBeg = tokens.getHead(t.thenp)
val thenHasLB = thenBeg.left.is[T.LeftBrace]
val end = if (thenHasLB) thenBeg else prevNonComment(prev(thenBeg))
Expand Down Expand Up @@ -1789,10 +1790,11 @@ class FormatOps(
private def foldedNonComment(
body: Tree,
nlSplitFunc: Int => Split,
isKeep: Boolean,
spaceIndents: Seq[Indent]
): Seq[Split] =
if (body.tokens.isEmpty) Seq(Split(Space, 0))
else foldedNonEmptyNonComment(body, nlSplitFunc, spaceIndents)
else foldedNonEmptyNonComment(body, nlSplitFunc, isKeep, spaceIndents)

private def unfoldedSpaceNonEmptyNonComment(
body: Tree,
Expand Down Expand Up @@ -1848,10 +1850,11 @@ class FormatOps(
def folded(
ft: FormatToken,
body: Tree,
isKeep: Boolean,
spaceIndents: Seq[Indent] = Seq.empty
)(nlSplitFunc: Int => Split): Seq[Split] =
checkComment(ft, nlSplitFunc) { _ =>
foldedNonComment(body, nlSplitFunc, spaceIndents)
foldedNonComment(body, nlSplitFunc, isKeep, spaceIndents)
}

def slbOnly(
Expand All @@ -1878,14 +1881,15 @@ class FormatOps(
Seq(nlSplitFunc(0).forThisLine)
case Newlines.classic =>
Option(classicNoBreakFunc).fold {
foldedNonComment(body, nlSplitFunc, spaceIndents)
foldedNonComment(body, nlSplitFunc, isKeep = true, spaceIndents)
} { func =>
val spcSplit = func.forThisLine
val nlSplit = nlSplitFunc(spcSplit.getCost(_ + 1, 0)).forThisLine
Seq(spcSplit, nlSplit)
}
case _ => // fold or keep without break
foldedNonComment(body, nlSplitFunc, spaceIndents)
case x => // fold or keep without break
val isKeep = x eq Newlines.keep
foldedNonComment(body, nlSplitFunc, isKeep, spaceIndents)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,10 @@ class Router(formatOps: FormatOps) {
beforeMultiline.eq(Newlines.classic) ||
getSingleStatExceptEndMarker(body).isEmpty
) withSlbSplit
else CtrlBodySplits.foldedNonEmptyNonComment(body, nlSplit(ft))
else {
val isKeep = beforeMultiline.eq(Newlines.keep)
CtrlBodySplits.foldedNonEmptyNonComment(body, nlSplit(ft), isKeep)
}
}
// New statement
case tok @ FormatToken(_: T.Semicolon, _, StartsStatementRight(stmt))
Expand Down Expand Up @@ -2648,7 +2651,8 @@ class Router(formatOps: FormatOps) {
case Newlines.unfold =>
Seq(baseSplit.withSingleLine(expire), newlineSplit(1))

case _ => CtrlBodySplits.folded(ft, body)(newlineSplit)
case x =>
CtrlBodySplits.folded(ft, body, x eq Newlines.keep)(newlineSplit)
}
}

Expand Down
71 changes: 71 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5842,3 +5842,74 @@ def allMatching(versionString: String,
partialFunctions: PartialFunction[options.Common, List[String]]*)
(partialFunctions: PartialFunction[options.Common, List[String]]*)
: List[String] = {}
<<< #3173 beforeMultiline = keep
newlines.beforeMultiline = keep
===
class Foo {
val foo = if (true) {
""
} else {
"a"
}
val foo = if (true)
""
else
"a"
val foo = if (true) "" else "a"
val foo = if (true) "aaaaa" else "bbbbb"
val foo = if (true) "aaaaa"
else "bbbbb"
}
>>>
class Foo {
val foo = if (true) {
""
} else {
"a"
}
val foo = if (true)
""
else
"a"
val foo = if (true) "" else "a"
val foo =
if (true) "aaaaa" else "bbbbb"
val foo = if (true) "aaaaa"
else "bbbbb"
}
<<< #3173 beforeMultiline = fold
newlines.beforeMultiline = fold
===
class Foo {
val foo = if (true) {
""
} else {
"a"
}
val foo = if (true)
""
else
"a"
val foo = if (true) "" else "a"
val foo = if (true) "aaaaa" else "bbbbb"
val foo = if (true) "aaaaa"
else "bbbbb"
}
>>>
class Foo {
val foo =
if (true) {
""
} else {
"a"
}
val foo =
if (true) ""
else "a"
val foo = if (true) "" else "a"
val foo =
if (true) "aaaaa" else "bbbbb"
val foo =
if (true) "aaaaa"
else "bbbbb"
}
30 changes: 30 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5602,3 +5602,33 @@ def allMatching(versionString: String,
partialFunctions: PartialFunction[options.Common, List[String]]*)
(partialFunctions: PartialFunction[options.Common, List[String]]*)
: List[String] = {}
<<< #3173 beforeMultiline = fold
newlines.beforeMultiline = fold
===
class Foo {
val foo = if (true) {
""
} else {
"a"
}
val foo = if (true)
""
else
"a"
val foo = if (true) "" else "a"
val foo = if (true) "aaaaa" else "bbbbb"
val foo = if (true) "aaaaa"
else "bbbbb"
}
>>>
class Foo {
val foo =
if (true) { "" }
else { "a" }
val foo = if (true) "" else "a"
val foo = if (true) "" else "a"
val foo =
if (true) "aaaaa" else "bbbbb"
val foo =
if (true) "aaaaa" else "bbbbb"
}
Loading