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

Router: don't add extra indent for fewer braces #3518

Merged
merged 1 commit into from
Mar 28, 2023
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 @@ -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