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: extract template to handle extends #3582

Merged
merged 1 commit into from
Jul 10, 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 @@ -381,6 +381,13 @@ class FormatOps(
}
)

val TemplateOnRight = new ExtractFromMeta(ft =>
ft.meta.rightOwner match {
case ro: Template => Some(ro)
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 @@ -1978,22 +1978,17 @@ class Router(formatOps: FormatOps) {
)

// Template
case FormatToken(_, soft.ExtendsOrDerives(), _) =>
val template = defnTemplate(rightOwner)
def lastToken = template.fold(getLastNonTrivialToken(rightOwner)) { x =>
templateDerivesOrCurlyOrLastNonTrivial(x)
}
case FormatToken(_, soft.ExtendsOrDerives(), TemplateOnRight(template)) =>
def lastToken = templateDerivesOrCurlyOrLastNonTrivial(template)

binPackParentConstructorSplits(
true,
template.toSet,
template.flatMap(findTemplateGroupOnRight(_.superType)),
Set(template),
findTemplateGroupOnRight(_.superType)(template),
lastToken,
style.indent.extendSite,
template.exists(x =>
if (x.early.nonEmpty) x.inits.nonEmpty
else x.inits.lengthCompare(1) > 0
)
if (template.early.nonEmpty) template.inits.nonEmpty
else template.inits.lengthCompare(1) > 0
)

// trait A extends B, C, D, E
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scalafmt.util

import scala.meta.{Defn, Pkg, Source, Template, Term, Tree}
import scala.meta._
import scala.meta.tokens.Token
import scala.meta.tokens.Token._
import scala.meta.tokens.Tokens
Expand Down Expand Up @@ -112,17 +112,6 @@ object TokenOps {
@inline
def getMod(ft: FormatToken): Modification = Space.orNL(ft.newlinesBetween)

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

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