Skip to content

Commit

Permalink
Router: don't add extra indent for fewer braces
Browse files Browse the repository at this point in the history
And don't indent select after them, as that's not yet supported by the
compiler.
  • Loading branch information
kitbellew committed Mar 28, 2023
1 parent 37b916a commit ed9f1fa
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2076,28 +2076,17 @@ class FormatOps(
.withSingleLine(opt)
.andPolicy(decideNewlinesOnlyAfterToken(opt))
}
val indent = t.parent match {
case Some(p: Term.Apply) =>
@tailrec
def isSelect(ma: Member.Apply): Boolean = ma.fun match {
case x: Member.Apply => isSelect(x)
case x => x.is[Term.Select]
}
if (isSelect(p)) None // select is taken care off elsewhere
else Some(style.indent.main + style.indent.getSignificant)
case _ => None
}
Some(new OptionalBracesRegion {
def owner = t.parent
def splits = Some(t.values match {
case (tf: Term.FunctionTerm) :: Nil
if !style.newlines.alwaysBeforeCurlyLambdaParams &&
t.parent.exists(_.is[Term.Apply]) =>
getSplits(ft, t, forceNL = false, indentOpt = indent) match {
getSplits(ft, t, forceNL = false) match {
case s +: rs if !s.isNL => funcSplit(tf)(s.fileLine) +: rs
case ss => ss
}
case _ => getSplits(ft, t, forceNL = true, indentOpt = indent)
case _ => getSplits(ft, t, forceNL = true)
})
def rightBrace = treeLast(t)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1899,21 +1899,20 @@ class Router(formatOps: FormatOps) {
}

// trigger indent only on the first newline
val nlIndent = Indent(indentLen, expire, After)
val noIndent =
checkFewerBraces(thisSelect.qual)
val nlIndent =
if (noIndent) Indent.Empty else Indent(indentLen, expire, After)
val spcPolicy = delayedBreakPolicyOpt
val nlPolicy = if (noIndent) spcPolicy else None
val splits =
if (nextNonCommentSameLine(tokens(t, 2)).right.is[T.Comment])
baseSplits.map(_.withIndent(nlIndent)) // will break
// will break
baseSplits.map(_.withIndent(nlIndent).andFirstPolicyOpt(nlPolicy))
else {
val spcIndent = nextDotIfSig.fold {
val ok = nextSelect.isEmpty && checkFewerBraces(expireTree)
if (ok) nlIndent else Indent.empty
} { x =>
Indent(indentLen, x.left, ExpiresOn.Before)
}
baseSplits.map { s =>
if (s.isNL) s.withIndent(nlIndent)
else
s.andFirstPolicyOpt(delayedBreakPolicyOpt).withIndent(spcIndent)
if (s.isNL) s.withIndent(nlIndent).andFirstPolicyOpt(nlPolicy)
else s.andFirstPolicyOpt(spcPolicy)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,12 @@ object a {
>>>
object a {
val b = c + (d.match
case true => true
case false => false
case true => true
case false => false
)
val b = c + (d.match
case true => true
case false => false
case true => true
case false => false
) + e
val b = c + (d match
case true => true
Expand Down
63 changes: 34 additions & 29 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3609,7 +3609,7 @@ object a:
object a:
def f(): Unit =
List(1, 2, 3).foldLeft(1):
case (a, b) => a + b
case (a, b) => a + b
<<< coloneol in fewer braces 1, main > sig
maxColumn = 80
indent.main = 4
Expand All @@ -3622,7 +3622,7 @@ object a:
object a:
def f(): Unit =
List(1, 2, 3).foldLeft(1):
case (a, b) => a + b
case (a, b) => a + b
<<< coloneol in fewer braces 2
maxColumn = 80
===
Expand Down Expand Up @@ -3679,8 +3679,8 @@ object a:
object a:
def f(): Unit =
List(1, 2, 3).foo:
case a: Int =>
case _ =>
case a: Int =>
case _ =>
otherTerm()
<<< coloneol in fewer braces 3, main > sig
maxColumn = 80
Expand All @@ -3696,8 +3696,8 @@ object a:
object a:
def f(): Unit =
List(1, 2, 3).foo:
case a: Int =>
case _ =>
case a: Int =>
case _ =>
otherTerm()
<<< match with eol
maxColumn = 80
Expand Down Expand Up @@ -3792,8 +3792,8 @@ def test(): String =
>>>
def test(): String =
Block:
2 + 2
.toString
2 + 2
.toString
<<< #3448 2
indent.significant = 1
===
Expand All @@ -3804,8 +3804,8 @@ def test(): String =
>>>
def test(): String =
Block:
2 + 2
.toString
2 + 2
.toString
<<< #3448 3
indent.significant = 1
===
Expand Down Expand Up @@ -3833,9 +3833,9 @@ def test(): String =
>>>
def test(): String =
bar:
2 + 2
.baz.qux:
3 + 3
2 + 2
.baz.qux:
3 + 3
<<< #3448 5
indent.significant = 1
===
Expand All @@ -3847,46 +3847,49 @@ class test:
>>>
class test:
bar:
2 + 2
.baz.qux:
3 + 3
2 + 2
.baz.qux:
3 + 3
<<< #3489 1
class test:
bar:
2 + 2
>>>
class test:
bar:
2 + 2
2 + 2
<<< #3489 2
class test:
baz.qux:
2 + 2
>>>
class test:
baz.qux:
2 + 2
2 + 2
<<< #3489 3
class test:
foo.bar:
2 + 2
.baz
.qux
>>>
class test:
foo
.bar:
2 + 2
.baz
.qux
<<< #3489 4
class test:
bar:
2 + 2
.baz
.qux
>>>
class test:
bar:
2 + 2
.baz
2 + 2
.baz.qux
<<< #3489 5
class test:
bar:
Expand All @@ -3896,20 +3899,22 @@ class test:
>>>
class test:
bar:
2 + 2
.baz:
3 + 3
2 + 2
.baz:
3 + 3
<<< #3489 6
class test:
bar:
2 + 2
.baz.qux:
.baz
.qux:
3 + 3
>>>
class test:
bar:
2 + 2
.baz.qux:
2 + 2
.baz
.qux:
3 + 3
<<< #3489 7
class test:
Expand All @@ -3923,7 +3928,7 @@ class test:
bar(
2 + 2
).baz.qux:
3 + 3
3 + 3
<<< #3489 8
class test:
bar.baz:
Expand All @@ -3945,8 +3950,8 @@ class test:
>>>
class test:
foo.match
case bar => ""
case baz => ""
case bar => ""
case baz => ""
<<< #3489 10
class test:
foo.match
Expand Down
Loading

0 comments on commit ed9f1fa

Please sign in to comment.