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 7413af4
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 259 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
85 changes: 45 additions & 40 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,47 +3847,50 @@ class test:
>>>
class test:
bar:
2 + 2
.baz.qux:
3 + 3
<<< #3489 1
2 + 2
.baz.qux:
3 + 3
<<< #3489 1 fewerBraces = never
class test:
bar:
2 + 2
>>>
class test:
bar:
2 + 2
<<< #3489 2
2 + 2
<<< #3489 2 fewerBraces = never
class test:
baz.qux:
2 + 2
>>>
class test:
baz.qux:
2 + 2
<<< #3489 3
2 + 2
<<< #3489 3 fewerBraces = never
class test:
foo.bar:
2 + 2
.baz
.qux
>>>
class test:
foo
.bar:
2 + 2
.baz
<<< #3489 4
.qux
<<< #3489 4 fewerBraces = never
class test:
bar:
2 + 2
.baz
.qux
>>>
class test:
bar:
2 + 2
.baz
<<< #3489 5
2 + 2
.baz.qux
<<< #3489 5 fewerBraces = never
class test:
bar:
2 + 2
Expand All @@ -3896,22 +3899,24 @@ class test:
>>>
class test:
bar:
2 + 2
.baz:
3 + 3
<<< #3489 6
2 + 2
.baz:
3 + 3
<<< #3489 6 fewerBraces = never
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
<<< #3489 7 fewerBraces = never
class test:
bar (
2 + 2
Expand All @@ -3923,8 +3928,8 @@ class test:
bar(
2 + 2
).baz.qux:
3 + 3
<<< #3489 8
3 + 3
<<< #3489 8 fewerBraces = never
class test:
bar.baz:
2 + 2
Expand All @@ -3937,17 +3942,17 @@ class test:
2 + 2
.qux:
3 + 3
<<< #3489 9
<<< #3489 9 fewerBraces = never
class test:
foo.match
case bar => ""
case baz => ""
>>>
class test:
foo.match
case bar => ""
case baz => ""
<<< #3489 10
case bar => ""
case baz => ""
<<< #3489 10 fewerBraces = never
class test:
foo.match
case bar => ""
Expand All @@ -3960,7 +3965,7 @@ class test:
case bar => ""
case baz => ""
.qux
<<< #3489 11
<<< #3489 11 fewerBraces = never
class test:
foo.match
case bar => ""
Expand Down
Loading

0 comments on commit 7413af4

Please sign in to comment.