Skip to content

Commit

Permalink
FormatOps: handle if as if no else when keep
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 31, 2022
1 parent fb9752c commit 11abd73
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 198 deletions.
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
25 changes: 11 additions & 14 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5862,23 +5862,20 @@ class Foo {
}
>>>
class Foo {
val foo =
if (true) {
""
} else {
"a"
}
val foo =
if (true)
""
else
"a"
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"
val foo = if (true) "aaaaa"
else "bbbbb"
}
<<< #3173 beforeMultiline = fold
newlines.beforeMultiline = fold
Expand Down
212 changes: 94 additions & 118 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,10 @@ object a {
>>>
object a {
val ok1 = if (a > 10) Some(a) else None
val ok2 =
if (a > 10) { Some(a) }
else { None }
val ok3 =
if (aaaa > 10000) { Some(aaaa) }
else { None }
val ok2 = if (a > 10) { Some(a) }
else { None }
val ok3 = if (aaaa > 10000) { Some(aaaa) }
else { None }
}
<<< 2.7 #1747: one line without else
maxColumn = 60
Expand Down Expand Up @@ -274,23 +272,20 @@ object a {
}
>>>
object a {
val ok1 =
if (a > 10)
Some(a)
else
None
val ok2 =
if (a > 10) {
Some(a)
} else {
None
}
val ok3 =
if (aaaa > 10000) {
Some(aaaa)
} else {
None
}
val ok1 = if (a > 10)
Some(a)
else
None
val ok2 = if (a > 10) {
Some(a)
} else {
None
}
val ok3 = if (aaaa > 10000) {
Some(aaaa)
} else {
None
}
}
<<< 2.9 #1747: split on else
maxColumn = 60
Expand All @@ -307,20 +302,17 @@ object a {
}
>>>
object a {
val ok1 =
if (a > 10) Some(a)
else
None
val ok2 =
if (a > 10) { Some(a) }
else {
None
}
val ok3 =
if (aaaa > 10000) { Some(aaaa) }
else {
None
}
val ok1 = if (a > 10) Some(a)
else
None
val ok2 = if (a > 10) { Some(a) }
else {
None
}
val ok3 = if (aaaa > 10000) { Some(aaaa) }
else {
None
}
}
<<< 2.10 #1747: split on then without else
maxColumn = 60
Expand Down Expand Up @@ -2399,15 +2391,14 @@ val ret = (if (dailyStats.isEmpty)
yestNav) / nav - 1
})
>>>
val ret =
(if (dailyStats.isEmpty)
0
else {
val yestStats = dailyStats.last
val yestNav = yestStats.nav
(nav -
yestNav) / nav - 1
})
val ret = (if (dailyStats.isEmpty)
0
else {
val yestStats = dailyStats.last
val yestNav = yestStats.nav
(nav -
yestNav) / nav - 1
})
<<< 8.24
maxColumn = 80
===
Expand Down Expand Up @@ -5324,35 +5315,29 @@ object a {
}
>>>
object a {
def foo = {
if (a) {
a +
b
} else {
a +
b
}
def foo = if (a) {
a +
b
} else {
a +
b
}
val foo = {
if (a) {
a +
b
} else if (b) {
a +
b
} else if (c) {
a +
b
}
val foo = if (a) {
a +
b
} else if (b) {
a +
b
} else if (c) {
a +
b
}
var foo = {
if (a) {
a +
b
} else {
a +
b
}
var foo = if (a) {
a +
b
} else {
a +
b
}
}
<<< #2019 if-else alwaysBeforeElseAfterCurlyIf
Expand Down Expand Up @@ -5385,39 +5370,33 @@ object a {
}
>>>
object a {
def foo = {
if (a) {
a +
b
}
else {
a +
b
}
def foo = if (a) {
a +
b
}
val foo = {
if (a) {
a +
b
}
else if (b) {
a +
b
}
else if (c) {
a +
b
}
else {
a +
b
}
var foo = {
if (a) {
a +
b
}
else {
a +
b
}
val foo = if (a) {
a +
b
}
else if (b) {
a +
b
}
else if (c) {
a +
b
}
var foo = if (a) {
a +
b
}
else {
a +
b
}
}
<<< #2019 try !alwaysBeforeElseAfterCurlyIf !allBlocks
Expand Down Expand Up @@ -5889,23 +5868,20 @@ class Foo {
}
>>>
class Foo {
val foo =
if (true) {
""
} else {
"a"
}
val foo =
if (true)
""
else
"a"
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"
val foo = if (true) "aaaaa"
else "bbbbb"
}
<<< #3173 beforeMultiline = fold
newlines.beforeMultiline = fold
Expand Down
Loading

0 comments on commit 11abd73

Please sign in to comment.