Skip to content

Commit

Permalink
Add config flags for extra spaces, fixes #116.
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Apr 19, 2016
1 parent 2b07938 commit dd537bf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
9 changes: 9 additions & 0 deletions cli/src/main/scala/org/scalafmt/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ object Cli {
opt[Boolean]("alignStripMarginStrings") action { (bool, c) =>
c.copy(style = c.style.copy(alignStripMarginStrings = bool))
} text s"See ScalafmtConfig scaladoc."
opt[Boolean]("spacesInParentheses") action { (bool, c) =>
c.copy(style = c.style.copy(spacesInParentheses = bool))
} text s"See ScalafmtConfig scaladoc."
opt[Boolean]("spacesInSquareBrackets") action { (bool, c) =>
c.copy(style = c.style.copy(spacesInSquareBrackets = bool))
} text s"See ScalafmtConfig scaladoc."
opt[Boolean]("spacesInImportCurlyBraces") action { (bool, c) =>
c.copy(style = c.style.copy(spacesInImportCurlyBrackets = bool))
} text s"See ScalafmtConfig scaladoc."
opt[Seq[String]]("alignTokens") action { (tokens, c) =>
val alignsTokens = tokens.map { token =>
val splitted = token.split(";", 2)
Expand Down
11 changes: 9 additions & 2 deletions core/src/main/scala/org/scalafmt/ScalafmtStyle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ case class ScalafmtStyle(maxColumn: Int,
noNewlinesBeforeJsNative: Boolean,
continuationIndentCallSite: Int,
continuationIndentDefnSite: Int,
alignTokens: Set[AlignToken]) {
alignTokens: Set[AlignToken],
spacesInParentheses: Boolean,
spacesInSquareBrackets: Boolean,
spacesInImportCurlyBrackets: Boolean) {
lazy val alignMap: Map[String, Regex] =
alignTokens.map(x => x.code -> x.owner.r).toMap
ValidationOps.assertNonNegative(
Expand All @@ -64,8 +67,12 @@ object ScalafmtStyle {
noNewlinesBeforeJsNative = false,
continuationIndentCallSite = 4,
continuationIndentDefnSite = 4,
alignTokens = Set.empty[AlignToken]
alignTokens = Set.empty[AlignToken],
spacesInParentheses = false,
spacesInSquareBrackets = false,
spacesInImportCurlyBrackets = false
)

val defaultWithAlign = default.copy(alignTokens = AlignToken.default)

val default40 = default.copy(maxColumn = 40)
Expand Down
20 changes: 14 additions & 6 deletions core/src/main/scala/org/scalafmt/internal/Router.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ class Router(formatOps: FormatOps) {
if (leftOwner.isInstanceOf[Term.Interpolate]) NoPolicy
else SingleLineBlock(matchingParentheses(hash(open)))
Seq(
Split(NoSplit, 0).withPolicy(policy)
Split(if (style.spacesInImportCurlyBrackets) Space else NoSplit, 0)
.withPolicy(policy)
)
case FormatToken(_, close: `}`, _)
if parents(rightOwner).exists(_.isInstanceOf[Import]) ||
rightOwner.isInstanceOf[Term.Interpolate] =>
Seq(
Split(NoSplit, 0)
Split(if (style.spacesInImportCurlyBrackets) Space else NoSplit, 0)
)
case FormatToken(_: `.`, underscore: `_ `, _)
if parents(rightOwner).exists(_.isInstanceOf[Import]) =>
Expand Down Expand Up @@ -426,6 +427,9 @@ class Router(formatOps: FormatOps) {

val modification =
if (right.isInstanceOf[Comment]) newlines2Modification(between)
else if (args.isEmpty) NoSplit
else if (isBracket && style.spacesInSquareBrackets) Space
else if (!isBracket && style.spacesInParentheses) Space
else NoSplit

val newlineModification: Modification =
Expand Down Expand Up @@ -611,9 +615,9 @@ class Router(formatOps: FormatOps) {
)
case FormatToken(bind: `@`, _, _) if leftOwner.isInstanceOf[Pat.Bind] =>
Seq(
Split(Space, 0)
Split(Space, 0)
)
case FormatToken(_: `@`, right: Delim, _)=>
case FormatToken(_: `@`, right: Delim, _) =>
Seq(Split(NoSplit, 0))
case FormatToken(_: `@`, right: Ident, _) =>
// Add space if right starts with a symbol
Expand Down Expand Up @@ -922,9 +926,13 @@ class Router(formatOps: FormatOps) {
Seq(
Split(NoSplit, 0)
)
case FormatToken(_, _: `)` | _: `]`, _) =>
case FormatToken(_, _: `)`, _) =>
Seq(
Split(NoSplit, 0)
Split(if (style.spacesInParentheses) Space else NoSplit, 0)
)
case FormatToken(_, _: `]`, _) =>
Seq(
Split(if (style.spacesInSquareBrackets) Space else NoSplit, 0)
)
case FormatToken(_, _: Keyword, _) =>
Seq(
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/resources/spaces/Spaces.stat
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
80 columns |
<<< square bracket
function[A, B]
>>>
function[ A, B ]
<<< parens empty
function()
>>>
function()
<<< parens
function(a, b)
>>>
function( a, b )
<<< import
import a.b.{c => d}
>>>
import a.b.{ c => d }
6 changes: 6 additions & 0 deletions core/src/test/scala/org/scalafmt/util/HasTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ trait HasTests extends FunSuiteLike with FormatAssertions {
case "default" | "standard" | "scala" => ScalafmtStyle.unitTest80
case "scalajs" => ScalafmtStyle.scalaJs
case "stripMargin" => ScalafmtStyle.default
case "spaces" =>
ScalafmtStyle.default.copy(
spacesInImportCurlyBrackets = true,
spacesInParentheses = true,
spacesInSquareBrackets = true
)
case "align" =>
ScalafmtStyle.default.copy(alignTokens = AlignToken.default)
case style => throw UnknownStyle(style)
Expand Down

0 comments on commit dd537bf

Please sign in to comment.