Skip to content

Commit

Permalink
scalameta#916 Add option to space context bounds only when multiple. …
Browse files Browse the repository at this point in the history
…ReaderUtil.oneOf always lowercases
  • Loading branch information
hejfelix committed May 31, 2017
1 parent 4a0d15f commit 6c8b857
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import metaconfig.Configured._
object ReaderUtil {
// Poor mans coproduct reader
def oneOf[T: ClassTag](options: sourcecode.Text[T]*): ConfDecoder[T] = {
val m = options.map(x => x.source -> x.value).toMap
val m = options.map(x => x.source.toLowerCase() -> x.value).toMap
ConfDecoder.instance[T] {
case Conf.Str(x) =>
m.get(x) match {
m.get(x.toLowerCase()) match {
case Some(y) =>
Ok(y)
case None =>
val available = m.keys.mkString(", ")
val available = options.map(_.source).mkString(", ")
val msg = s"Unknown input '$x'. Expected one of $available"
ConfError.msg(msg).notOk
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.scalafmt.config

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

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)
}

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
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.scalafmt.config

import metaconfig._
import org.scalafmt.config.SpaceBeforeContextBound.Never

/**
*
Expand All @@ -16,9 +17,9 @@ import metaconfig._
*/
@DeriveConfDecoder
case class Spaces(
beforeContextBoundColon: Boolean = false,
afterTripleEquals: Boolean = false,
inImportCurlyBraces: Boolean = false,
inParentheses: Boolean = false,
neverAroundInfixTypes: Seq[String] = Nil
beforeContextBoundColon: SpaceBeforeContextBound = Never,
afterTripleEquals: Boolean = false,
inImportCurlyBraces: Boolean = false,
inParentheses: Boolean = false,
neverAroundInfixTypes: Seq[String] = Nil
)
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,10 @@ class Router(formatOps: FormatOps) {
)
case FormatToken(left, Colon(), _) =>
val mod: Modification = rightOwner match {
case _: Type.Param =>
if (style.spaces.beforeContextBoundColon) Space else NoSplit
case tp: Type.Param =>
val contextOption = style.spaces.beforeContextBoundColon
if (contextOption.isIfMultipleBounds && tp.cbounds.size > 1 || contextOption.isAlways) Space else NoSplit

case _ =>
left match {
case ident: Ident => identModification(ident)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
spaces.beforeContextBoundColon = ifMultipleBounds
<<< space before colon #180
def map[T: Class](f: N => T): S[T]
>>>
def map[T: Class](f: N => T): S[T]
<<< space before colon 2 #180
def orderLaws[A:Eq:Arbitrary] = O
>>>
def orderLaws[A : Eq : Arbitrary] = O
<<< non-ident before colon
def myMethod[F[_]:Functor](): Foo
>>>
def myMethod[F[_]: Functor](): Foo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spaces.beforeContextBoundColon = true
<<< space before colon #180
<<< space before colon #180
def map[T: Class](f: N => T): S[T]
>>>
def map[T : Class](f: N => T): S[T]
Expand Down

0 comments on commit 6c8b857

Please sign in to comment.