diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index d5f1ed6603..c32e9bbecb 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -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) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index b2f038596e..ef00764c04 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -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) @@ -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)) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenOps.scala index 0e42e0a3b0..04fcd53d50 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TokenOps.scala @@ -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