diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ProjectFiles.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ProjectFiles.scala index 03d227039b..617a0f84d9 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ProjectFiles.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ProjectFiles.scala @@ -92,9 +92,11 @@ object ProjectFiles { sealed abstract class Layout { def getLang(path: AbsoluteFile): Option[String] - def getDialectByLang(lang: String)(implicit + protected[config] def getDialectByLang(lang: String)(implicit dialect: Dialect ): Option[NamedDialect] + final def withLang(lang: String, style: ScalafmtConfig): ScalafmtConfig = + style.withDialect(getDialectByLang(lang)(style.dialect)) } object Layout { @@ -136,7 +138,7 @@ object ProjectFiles { private[config] val s213 = nd(NamedDialect.scala213) private[config] val s3 = nd(NamedDialect.scala3) - override def getDialectByLang(lang: String)(implicit + protected[config] override def getDialectByLang(lang: String)(implicit dialect: Dialect ): Option[NamedDialect] = lang match { case "scala-2.10" if is211 => s210 diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala index 64aa1048f2..c35ac8ec9e 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala @@ -191,10 +191,10 @@ case class ScalafmtConfig( val cfg = conf match { case x: Conf.Str => withDialect(NamedDialect.codec.read(None, x).get) case x => - val dialectOpt = eitherPat.left.toOption.flatMap { lang => - project.layout.flatMap(_.getDialectByLang(lang)(dialect)) + val styleOpt = eitherPat.left.toOption.flatMap { lang => + project.layout.map(_.withLang(lang, this)) } - decoder.read(Some(withDialect(dialectOpt)), x).get + decoder.read(styleOpt.orElse(Some(this)), x).get } eitherPat -> cfg } @@ -216,15 +216,12 @@ case class ScalafmtConfig( expandedFileOverride.map { case (langStyles, pmStyles) => def langStyle = onLang { (layout, lang) => val style = langStyles.collectFirst { case (`lang`, x) => x } - style.getOrElse(withDialect(layout.getDialectByLang(lang)(dialect))) + style.getOrElse(layout.withLang(lang, this)) } val pmStyle = pmStyles.collectFirst { case (pm, style) if pm.matches(absfile.path) => if (!style.dialect.isEquivalentTo(dialect)) style - else - style.withDialect(onLang { - _.getDialectByLang(_)(style.dialect) - }.flatten) + else onLang(_.withLang(_, style)).getOrElse(style) } pmStyle.orElse(langStyle).getOrElse(this) }