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: use template to break before extends #3581

Merged
merged 1 commit into from
Jul 7, 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 @@ -374,6 +374,13 @@ class FormatOps(
new Policy.Delay(policy, Policy.End.Before(from))
}

val WithTemplateOnLeft = new ExtractFromMeta(ft =>
ft.meta.leftOwner match {
case lo: Stat.WithTemplate => Some(lo.templ)
case _ => None
}
)

def templateCurlyFt(template: Template): Option[FormatToken] =
getStartOfTemplateBody(template).map(tokenBefore)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,10 @@ class Router(formatOps: FormatOps) {
case FormatToken(
_: T.KwObject | _: T.KwClass | _: T.KwTrait | _: T.KwEnum,
_,
_
WithTemplateOnLeft(template)
) =>
def expire = defnTemplate(leftOwner)
.flatMap {
getTemplateGroups(_).flatMap(
_.lastOption.flatMap(_.headOption.flatMap(_.tokens.headOption))
)
}
def expire = getTemplateGroups(template)
.flatMap(_.last.headOption.flatMap(_.tokens.headOption))
.getOrElse(getLastToken(leftOwner))
def forceNewlineBeforeExtends = Policy.before(expire) {
case Decision(FormatToken(_, soft.ExtendsOrDerives(), m), s)
Expand All @@ -834,8 +830,8 @@ class Router(formatOps: FormatOps) {
val policy =
if (style.binPack.keepParentConstructors) None
else
defnBeforeTemplate(leftOwner).map { x =>
val policyEnd = Policy.End.On(x.tokens.last)
template.tokens.headOption.map { head =>
val policyEnd = Policy.End.Before(head)
delayedBreakPolicy(policyEnd)(forceNewlineBeforeExtends)
}
Seq(Split(Space, 0).withPolicyOpt(policy))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,6 @@ object TokenOps {
case _ => None
}

def defnBeforeTemplate(tree: Tree): Option[Tree] =
tree match {
case t: Defn.Object => Some(t.name)
case t: Defn.Class => Some(t.ctor)
case t: Defn.Trait => Some(t.ctor)
case t: Defn.Enum => Some(t.ctor)
case t: Pkg.Object => Some(t.name)
case _ => None
}

val formatOnCode = Set(
"@formatter:on", // IntelliJ
"format: on" // scalariform
Expand Down
Loading