Skip to content

Commit

Permalink
FormatOps: handle fewer braces and refined types
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 22, 2023
1 parent 86669cd commit e5788a0
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,36 @@ class FormatOps(
def splits = Some(getSplits(ft, t, forceNL = true))
def rightBrace = if (isSeqMulti(t.stats)) treeLast(t) else None
})
case t: Type.Refine =>
Some(new OptionalBracesRegion {
def owner = Some(t)
def splits = Some(getSplits(ft, t, forceNL = true))
def rightBrace = treeLast(t)
})
case t: Term.ArgClause if tokens.getHead(t) eq ft =>
def funcSplit(arg: Term.FunctionTerm)(implicit fl: FileLine) = {
val end = tokens.getLast(arg)
val opt = getOptimalTokenFor(getFuncArrow(arg).getOrElse(end))
val indent = Num(style.indent.getSignificant - style.indent.main)
Split(Space, 0)
.withSingleLine(opt)
.andPolicy(decideNewlinesOnlyAfterToken(opt))
.withIndent(indent, end.left, ExpiresOn.After)
}
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) match {
case s +: rs if !s.isNL => funcSplit(tf)(s.fileLine) +: rs
case ss => ss
}
case _ => getSplits(ft, t, forceNL = true)
})
def rightBrace = treeLast(t)
})
case _ => None
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1844,13 +1844,28 @@ class Router(formatOps: FormatOps) {
}

case Newlines.fold =>
val end =
nextSelect.fold(expire)(x => getLastNonTrivialToken(x.qual))
def exclude = insideBracesBlock(t, end, true)
Seq(
Split(NoSplit, 0).withSingleLine(end, exclude),
Split(NewlineT(alt = Some(NoSplit)), 1)
)
nextSelect match {
case Some(ns) if (ns.qual match {
case p: Term.Apply if dialect.allowFewerBraces =>
p.argClause.tokens.head.is[T.Colon]
case p: Term.Match =>
!tokenBefore(p.cases).left.is[T.LeftBrace]
case _ => false
}) =>
val nextDot = tokenBefore(ns.nameToken).left
Seq(
Split(Newline, 0)
.withPolicy(decideNewlinesOnlyBeforeClose(nextDot))
)
case _ =>
val end =
nextSelect.fold(expire)(x => getLastNonTrivialToken(x.qual))
def exclude = insideBracesBlock(t, end, true)
Seq(
Split(NoSplit, 0).withSingleLine(end, exclude),
Split(NewlineT(alt = Some(NoSplit)), 1)
)
}
}

val delayedBreakPolicyOpt = nextSelect.map { selectLike =>
Expand Down
32 changes: 15 additions & 17 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3606,12 +3606,10 @@ object a:
List(1, 2, 3).foldLeft(1):
case (a, b) => a + b
>>>
test does not parse
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 All @@ -3622,12 +3620,13 @@ object a:
.apply:
12 + 3
>>>
test does not parse
object a:
def f(): Unit =
List(1, 2, 3)
.foo: a => a + 2
^
.foo: a =>
a + 2
.apply:
12 + 3
<<< coloneol in fewer braces 3
maxColumn = 80
===
Expand All @@ -3638,12 +3637,12 @@ object a:
case _ =>
otherTerm()
>>>
test does not parse
object a:
def f(): Unit =
List(1, 2, 3).foo:
case a: Int =>
^
case a: Int =>
case _ =>
otherTerm()
<<< match with eol
maxColumn = 80
===
Expand All @@ -3654,30 +3653,29 @@ object a:
.apply:
12 + 3
>>>
UNABLE TO FORMAT,
tok=2∙.[77:86]
object a:
def f(): Unit =
List(1, 2, 3).match
case _ => a + 2
^
.apply:
12 + 3
<<< coloneol in refined types
maxColumn = 80
===
object a:
type T = String:
type U = Int
>>>
test does not parse
object a:
type T = String:
^
type U = Int
<<< with in refined types
maxColumn = 80
===
object a:
type T = String with
type U = Int
>>>
test does not parse
object a:
type T = String with
type U = Int
^
type U = Int
38 changes: 19 additions & 19 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3432,9 +3432,8 @@ object a:
case (a, b) => a + b
>>>
object a:
def f(): Unit = List(1, 2, 3)
.foldLeft(1):
case (a, b) => a + b
def f(): Unit = List(1, 2, 3).foldLeft(1):
case (a, b) => a + b
<<< coloneol in fewer braces 2
maxColumn = 80
===
Expand All @@ -3445,10 +3444,12 @@ object a:
.apply:
12 + 3
>>>
test does not parse
object a:
def f(): Unit = List(1, 2, 3).foo: a => a + 2.apply: 12 + 2
^
def f(): Unit = List(1, 2, 3)
.foo: a =>
a + 2
.apply:
12 + 3
<<< coloneol in fewer braces 3
maxColumn = 80
===
Expand All @@ -3459,12 +3460,12 @@ object a:
case _ =>
otherTerm()
>>>
Idempotency violated
object a:
def f(): Unit = List(1, 2, 3)
.foo:
case a: Int =>
case _ => otherTerm()
def f(): Unit =
List(1, 2, 3).foo:
case a: Int =>
case _ =>
otherTerm()
<<< match with eol
maxColumn = 80
===
Expand All @@ -3476,29 +3477,28 @@ object a:
12 + 3
>>>
object a:
def f(): Unit = List(1, 2, 3).match
case _ => a + 2
.apply: 12 + 3
def f(): Unit = List(1, 2, 3)
.match
case _ => a + 2
.apply:
12 + 3
<<< coloneol in refined types
maxColumn = 80
===
object a:
type T = String:
type U = Int
>>>
test does not parse
object a:
type T = String:
^
type U = Int
<<< with in refined types
maxColumn = 80
===
object a:
type T = String with
type U = Int
>>>
test does not parse
object a:
type T = String with
type U = Int
^
type U = Int
26 changes: 11 additions & 15 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3588,12 +3588,10 @@ object a:
List(1, 2, 3).foldLeft(1):
case (a, b) => a + b
>>>
test does not parse
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 All @@ -3604,12 +3602,12 @@ object a:
.apply:
12 + 3
>>>
Idempotency violated
object a:
def f(): Unit =
List(1, 2, 3).foo: a =>
a + 2
.apply: 12 + 2
a + 2
.apply:
12 + 3
<<< coloneol in fewer braces 3
maxColumn = 80
===
Expand All @@ -3620,12 +3618,12 @@ object a:
case _ =>
otherTerm()
>>>
test does not parse
object a:
def f(): Unit =
List(1, 2, 3).foo:
case a: Int =>
^
case a: Int =>
case _ =>
otherTerm()
<<< match with eol
maxColumn = 80
===
Expand All @@ -3640,27 +3638,25 @@ object a:
def f(): Unit =
List(1, 2, 3).match
case _ => a + 2
.apply: 12 + 3
.apply:
12 + 3
<<< coloneol in refined types
maxColumn = 80
===
object a:
type T = String:
type U = Int
>>>
test does not parse
object a:
type T = String:
^
type U = Int
<<< with in refined types
maxColumn = 80
===
object a:
type T = String with
type U = Int
>>>
test does not parse
object a:
type T = String with
type U = Int
^
type U = Int
Loading

0 comments on commit e5788a0

Please sign in to comment.