Skip to content

Commit

Permalink
Implement Encoders for all config options
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed May 9, 2018
1 parent bf3be64 commit c8212ed
Show file tree
Hide file tree
Showing 24 changed files with 77 additions and 38 deletions.
7 changes: 2 additions & 5 deletions readme/Configuration.scalatex
Expand Up @@ -910,8 +910,5 @@
there are more configuration options if you visited nested fields
like @code{spaces} and @code{newlines}.

@hl.ref(
wd / "scalafmt-core" / "shared" / "src" / "main" / "scala" / "org" / "scalafmt" / "config" / "ScalafmtConfig.scala",
start = "case class",
end = ") {"
)
@org.scalafmt.readme.Readme.allOptions

9 changes: 5 additions & 4 deletions readme/src/main/scala/org/scalafmt/readme/Readme.scala
Expand Up @@ -117,12 +117,13 @@ object Readme {
// a.b = 1
// a.c = 2
// which means a simple line-by-line string diffing is sufficient for now.
// val defaultStrs = Config.toHocon(ScalafmtConfig.default).toSet
// val configStrs = Config.toHocon(style).toSet
// configStrs.diff(defaultStrs).mkString("\n")
"???"
val defaultStrs = Config.toHocon(ScalafmtConfig.default).toSet
val configStrs = Config.toHocon(style).toSet
configStrs.diff(defaultStrs).mkString("\n")
}

def allOptions = hl.scala.apply(Config.toHocon(ScalafmtConfig.default))

def configurationBlock(style: ScalafmtConfig) = {
div(
span(
Expand Down
Expand Up @@ -94,6 +94,9 @@ object Align {
val default = some
val more: Align = some.copy(tokens = AlignToken.default)
implicit lazy val surface: Surface[Align] = generic.deriveSurface[Align]
implicit lazy val encoder: ConfEncoder[Align] = generic.deriveEncoder
// TODO: metaconfig should handle iterables
implicit def encoderSet[T:ConfEncoder]: ConfEncoder[Set[T]] = implicitly[ConfEncoder[Seq[T]]].contramap(_.toSeq)

// only for the truest vertical aligners, this setting is open for changes,
// please open PR addding more stuff to it if you like.
Expand Down
Expand Up @@ -18,6 +18,7 @@ case class AlignToken(code: String, owner: String) {
object AlignToken {
implicit lazy val surface: Surface[AlignToken] =
generic.deriveSurface[AlignToken]
implicit lazy val encoder: ConfEncoder[AlignToken] = generic.deriveEncoder
val applyInfix = "Term.ApplyInfix"
val caseArrow = AlignToken("=>", "Case")
protected[scalafmt] val fallbackAlign = new AlignToken("<empty>", ".*")
Expand Down
Expand Up @@ -45,4 +45,5 @@ case class BinPack(
object BinPack {
implicit lazy val surface: generic.Surface[BinPack] =
generic.deriveSurface[BinPack]
implicit lazy val encoder: ConfEncoder[BinPack] = generic.deriveEncoder
}
Expand Up @@ -19,4 +19,5 @@ case class ContinuationIndent(
object ContinuationIndent {
implicit lazy val surface: generic.Surface[ContinuationIndent] =
generic.deriveSurface
implicit lazy val encoder: ConfEncoder[ContinuationIndent] = generic.deriveEncoder
}
@@ -1,11 +1,11 @@
package org.scalafmt.config

import metaconfig.ConfDecoder
import metaconfig.ConfCodec

sealed abstract class Docstrings

object Docstrings {
implicit val reader: ConfDecoder[Docstrings] =
implicit val reader: ConfCodec[Docstrings] =
ReaderUtil.oneOf[Docstrings](JavaDoc, ScalaDoc, preserve)
case object JavaDoc extends Docstrings
case object ScalaDoc extends Docstrings
Expand Down
Expand Up @@ -45,9 +45,11 @@ sealed abstract class ImportSelectors

object ImportSelectors {

val reader: ConfDecoder[ImportSelectors] =
val reader: ConfCodec[ImportSelectors] =
ReaderUtil.oneOf[ImportSelectors](noBinPack, binPack, singleLine)

implicit val encoder: ConfEncoder[ImportSelectors] = reader

// This reader is backwards compatible with the old import selector
// configuration, which used the boolean flag binPackImportSelectors to
// decide between (what are now) the `binPack` and `noBinPack` strategies.
Expand All @@ -56,7 +58,7 @@ object ImportSelectors {
// limitations in the current version of scalameta/paradise, but these will
// likely be fixed in the future, at which point this reader could be moved
// to ScalafmtConfig
implicit val backwardsCompatibleReader =
implicit val backwardsCompatibleReader: ConfDecoder[ImportSelectors] =
ConfDecoder.instance[ImportSelectors] {
case Conf.Bool(true) => Ok(ImportSelectors.binPack)
case Conf.Bool(false) => Ok(ImportSelectors.noBinPack)
Expand Down
Expand Up @@ -43,6 +43,8 @@ case class IndentOperator(
object IndentOperator {
implicit lazy val surface: generic.Surface[IndentOperator] =
generic.deriveSurface
implicit lazy val encoder: ConfEncoder[IndentOperator] =
generic.deriveEncoder
val default = IndentOperator()
val akka = IndentOperator(
ScalafmtConfig.indentOperatorsIncludeAkka,
Expand Down
@@ -1,11 +1,11 @@
package org.scalafmt.config

import metaconfig.ConfDecoder
import metaconfig.ConfCodec

sealed abstract class LineEndings

object LineEndings {
implicit val reader: ConfDecoder[LineEndings] =
implicit val reader: ConfCodec[LineEndings] =
ReaderUtil.oneOf[LineEndings](unix, windows, preserve)
case object unix extends LineEndings
case object windows extends LineEndings
Expand Down
Expand Up @@ -98,19 +98,17 @@ case class Newlines(

object Newlines {
implicit lazy val surface: Surface[Newlines] = generic.deriveSurface
implicit lazy val encoder: ConfEncoder[Newlines] = generic.deriveEncoder
}

sealed abstract class NewlineCurlyLambda

object NewlineCurlyLambda {

implicit lazy val surface: generic.Surface[Newlines] =
generic.deriveSurface

case object preserve extends NewlineCurlyLambda
case object always extends NewlineCurlyLambda
case object never extends NewlineCurlyLambda

implicit val newlineCurlyLambdaReader: ConfDecoder[NewlineCurlyLambda] =
implicit val newlineCurlyLambdaReader: ConfCodec[NewlineCurlyLambda] =
ReaderUtil.oneOf[NewlineCurlyLambda](preserve, always, never)
}
Expand Up @@ -84,4 +84,5 @@ case class OptIn(

object OptIn {
implicit lazy val surface: Surface[OptIn] = generic.deriveSurface
implicit lazy val encoder: ConfEncoder[OptIn] = generic.deriveEncoder
}
Expand Up @@ -15,7 +15,10 @@ case class Pattern(
}

object Pattern {
implicit lazy val surface: Surface[Pattern] = generic.deriveSurface
implicit lazy val surface: Surface[Pattern] =
generic.deriveSurface
implicit lazy val encoder: ConfEncoder[Pattern] =
generic.deriveEncoder
val neverInfix = Pattern(
Seq("[\\w\\d_]+"),
Seq(
Expand Down
Expand Up @@ -18,4 +18,6 @@ case class ProjectFiles(
object ProjectFiles {
implicit lazy val surface: generic.Surface[ProjectFiles] =
generic.deriveSurface
implicit lazy val encoder: ConfEncoder[ProjectFiles] =
generic.deriveEncoder
}
Expand Up @@ -11,7 +11,7 @@ object ReaderUtil {
oneOfImpl((lowerCase _).compose(lowerCaseNoBackticks), options)

def oneOfIgnoreBackticks[T: ClassTag](
options: sourcecode.Text[T]*): ConfDecoder[T] =
options: sourcecode.Text[T]*): ConfCodec[T] =
oneOfImpl(lowerCaseNoBackticks, options)

private def lowerCase(s: String): String = s.toLowerCase
Expand Down
Expand Up @@ -18,4 +18,6 @@ case class RedundantBracesSettings(
object RedundantBracesSettings {
implicit lazy val surface: generic.Surface[RedundantBracesSettings] =
generic.deriveSurface
implicit lazy val encoder: ConfEncoder[RedundantBracesSettings] =
generic.deriveEncoder
}
Expand Up @@ -28,4 +28,7 @@ case class RewriteSettings(
object RewriteSettings {
implicit lazy val surface: generic.Surface[RewriteSettings] =
generic.deriveSurface
implicit lazy val encoder: ConfEncoder[RewriteSettings] =
generic.deriveEncoder

}
Expand Up @@ -165,6 +165,11 @@ case class ScalafmtConfig(
object ScalafmtConfig {
implicit lazy val surface: generic.Surface[ScalafmtConfig] =
generic.deriveSurface
implicit lazy val encoder: ConfEncoder[ScalafmtConfig] =
generic.deriveEncoder[ScalafmtConfig]

implicit lazy val codecEncoder: ConfEncoder[Codec] =
ConfEncoder.StringEncoder.contramap(_.name)

val indentOperatorsIncludeAkka = "^.*=$"
val indentOperatorsExcludeAkka = "^$"
Expand Down
Expand Up @@ -81,6 +81,8 @@ case class ScalafmtOptimizer(
object ScalafmtOptimizer {
implicit lazy val surface: generic.Surface[ScalafmtOptimizer] =
generic.deriveSurface
implicit lazy val encoder: ConfEncoder[ScalafmtOptimizer] =
generic.deriveEncoder
val default = ScalafmtOptimizer()

// TODO(olafur) uncomment once scala.meta converter supports default args.
Expand Down
Expand Up @@ -23,7 +23,7 @@ import scala.meta.dialects.Scala211
case class ScalafmtRunner(
debug: Boolean = false,
eventCallback: FormatEvent => Unit = _ => Unit,
parser: MetaParser = Parse.parseSource,
parser: Parse[_ <: Tree] = Parse.parseSource,
optimizer: ScalafmtOptimizer = ScalafmtOptimizer.default,
maxStateVisits: Int = 1000000,
dialect: Dialect = ScalafmtRunner.defaultDialect,
Expand All @@ -43,6 +43,14 @@ case class ScalafmtRunner(
object ScalafmtRunner {
implicit lazy val surface: generic.Surface[ScalafmtRunner] =
generic.deriveSurface
implicit lazy val encoder: ConfEncoder[ScalafmtRunner] =
generic.deriveEncoder
implicit lazy val formatEventEncoder: ConfEncoder[FormatEvent => Unit] =
ConfEncoder.StringEncoder.contramap(_ => "FormatEvent => Unit")
implicit lazy val parseEncoder: ConfEncoder[Parse[_ <: Tree]] =
ConfEncoder.StringEncoder.contramap(_ => "Parse[Tree]")
implicit lazy val dialectEncoder: ConfEncoder[Dialect] =
ConfEncoder.StringEncoder.contramap(_.toString())
val defaultDialect = Scala211.copy(
// Are `&` intersection types supported by this dialect?
allowAndTypes = true,
Expand Down
Expand Up @@ -8,7 +8,7 @@ case class SortSettings(

object SortSettings {

implicit val SortSettingsModKeyReader: ConfDecoder[ModKey] =
implicit val SortSettingsModKeyReader: ConfCodec[ModKey] =
ReaderUtil.oneOfIgnoreBackticks[ModKey](
`implicit`,
`final`,
Expand Down Expand Up @@ -36,6 +36,8 @@ object SortSettings {
)

implicit val surface: generic.Surface[SortSettings] = generic.deriveSurface
implicit lazy val encoder: ConfEncoder[SortSettings] =
generic.deriveEncoder
implicit val reader: ConfDecoder[SortSettings] =
generic.deriveDecoder(SortSettings(defaultOrder)).flatMap { result =>
if (result.order.distinct.length != 8) {
Expand Down
@@ -1,25 +1,30 @@
package org.scalafmt.config

import metaconfig.ConfCodec
import metaconfig.ConfEncoder
import metaconfig.Configured.Ok
import metaconfig.{Conf, ConfDecoder}
import org.scalafmt.config.SpaceBeforeContextBound.{Always, IfMultipleBounds}

sealed trait SpaceBeforeContextBound {
def isIfMultipleBounds = this == IfMultipleBounds
def isAlways = this == Always
}

object SpaceBeforeContextBound {

implicit val reader = ConfDecoder.instance[SpaceBeforeContextBound] {
case Conf.Bool(true) => Ok(Always)
case Conf.Bool(false) => Ok(Never)
case x =>
ReaderUtil
.oneOf[SpaceBeforeContextBound](Always, Never, IfMultipleBounds)
.read(x)
}
val codec: ConfCodec[SpaceBeforeContextBound] =
ReaderUtil.oneOf[SpaceBeforeContextBound](Always, Never, IfMultipleBounds)

implicit val encoder: ConfEncoder[SpaceBeforeContextBound] = codec
implicit val reader: ConfDecoder[SpaceBeforeContextBound] =
ConfDecoder.instance[SpaceBeforeContextBound] {
case Conf.Bool(true) => Ok(Always)
case Conf.Bool(false) => Ok(Never)
case x => codec.read(x)
}

case object Always extends SpaceBeforeContextBound
case object Never extends SpaceBeforeContextBound
case object IfMultipleBounds extends SpaceBeforeContextBound
}
sealed trait SpaceBeforeContextBound {
def isIfMultipleBounds = this == IfMultipleBounds
def isAlways = this == Always
}
Expand Up @@ -35,4 +35,5 @@ case class Spaces(
}
object Spaces {
implicit lazy val surface: generic.Surface[Spaces] = generic.deriveSurface
implicit lazy val encoder: ConfEncoder[Spaces] = generic.deriveEncoder
}
@@ -1,12 +1,11 @@
package org.scalafmt.rewrite

import metaconfig.ConfCodec
import scala.meta._
import metaconfig.ConfDecoder
import org.scalafmt.config.ReaderUtil
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.util.TokenOps.TokenHash
import org.scalafmt.util.{TokenOps, TokenTraverser, TreeOps}
import org.scalameta.logger

case class RewriteCtx(
style: ScalafmtConfig,
Expand All @@ -23,7 +22,7 @@ abstract class Rewrite {
}

object Rewrite {
implicit val reader: ConfDecoder[Rewrite] =
implicit val reader: ConfCodec[Rewrite] =
ReaderUtil.oneOf[Rewrite](
RedundantBraces,
RedundantParens,
Expand Down

0 comments on commit c8212ed

Please sign in to comment.