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

FormatOps: use unapply match for optional braces 2 #2802

Merged
merged 1 commit into from
Oct 18, 2021
Merged
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 @@ -2104,12 +2104,12 @@ class FormatOps(
style: ScalafmtConfig
): Option[OptionalBracesRegion] =
ft.meta.leftOwner match {
case g @ Defn.ExtensionGroup(_, _, t: Term.Block)
if isBlockStart(t, nft) =>
case t @ Defn.ExtensionGroup(_, _, b: Term.Block)
if isBlockStart(b, nft) =>
Some(new OptionalBracesRegion {
def owner = Some(g)
def splits = Some(getSplitsMaybeBlock(ft, nft, t))
def rightBrace = if (isMultiStatBlock(t)) treeLast(t) else None
def owner = Some(t)
def splits = Some(getSplitsMaybeBlock(ft, nft, b))
def rightBrace = blockLast(b)
})
case t @ Term.If(_, thenp, _) if !nft.right.is[T.KwThen] && {
isTreeMultiStatBlock(thenp) || isElsePWithOptionalBraces(t) ||
Expand All @@ -2120,19 +2120,19 @@ class FormatOps(
def splits = Some(getSplitsForIf(ft, nft, t))
def rightBrace = blockLast(thenp)
})
case t: Term.For if !nft.right.is[T.KwDo] =>
case t @ Term.For(_, b) if !nft.right.is[T.KwDo] =>
// unsupported except for right brace
Some(new OptionalBracesRegion {
def owner = None
def splits = None
def rightBrace = blockLast(t.body)
def rightBrace = blockLast(b)
})
case t: Term.While if !nft.right.is[T.KwDo] =>
case t @ Term.While(_, b) if !nft.right.is[T.KwDo] =>
// unsupported except for right brace
Some(new OptionalBracesRegion {
def owner = None
def splits = None
def rightBrace = blockLast(t.body)
def rightBrace = blockLast(b)
})
case _ => None
}
Expand Down Expand Up @@ -2495,6 +2495,8 @@ class FormatOps(
tree.tokens.lastOption.map(tokens(_).left)
@inline private def blockLast(tree: Tree): Option[T] =
if (isTreeMultiStatBlock(tree)) treeLast(tree) else None
@inline private def blockLast(tree: Term.Block): Option[T] =
if (isMultiStatBlock(tree)) treeLast(tree) else None
@inline private def seqLast(seq: Seq[Tree]): Option[T] =
if (isSeqMulti(seq)) treeLast(seq.last) else None

Expand Down