Skip to content

Commit

Permalink
Router: extract template to handle extends
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jul 10, 2023
1 parent e0dafe0 commit 114c883
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
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

0 comments on commit 114c883

Please sign in to comment.