Skip to content

Commit

Permalink
Rename breakUpLongImportSelectors into binPackImportSelectors.
Browse files Browse the repository at this point in the history
cc/ #101
  • Loading branch information
olafurpg committed Aug 7, 2016
1 parent fd5e6eb commit 8f67462
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 49 deletions.
2 changes: 1 addition & 1 deletion cli/src/main/scala/org/scalafmt/cli/Cli.scala
Expand Up @@ -211,7 +211,7 @@ object Cli {
c.copy(style = c.style.copy(unindentTopLevelOperators = bool))
} text s"See ScalafmtConfig scaladoc."
opt[Boolean]("breakUpLongImportSelectors") action { (bool, c) =>
c.copy(style = c.style.copy(breakUpLongImportSelectors = bool))
c.copy(style = c.style.copy(binPackImportSelectors = bool))
} text s"See ScalafmtConfig scaladoc."
opt[String]("indentOperatorsIncludeFilter") action { (str, c) =>
c.copy(style = c.style.copy(indentOperatorsIncludeFilter = str.r))
Expand Down
4 changes: 2 additions & 2 deletions cli/src/test/scala/org/scalafmt/cli/CliTest.scala
Expand Up @@ -40,7 +40,7 @@ class CliTest extends FunSuite with DiffAssertions {
continuationIndentCallSite = 2,
continuationIndentDefnSite = 3,
scalaDocs = false,
breakUpLongImportSelectors = false,
binPackImportSelectors = false,
alignStripMarginStrings = false,
spaceBeforeContextBoundColon = true)
val expectedConfig = Cli.Config.default.copy(
Expand All @@ -53,7 +53,7 @@ class CliTest extends FunSuite with DiffAssertions {
val args = Array(
"--reformatComments",
"false",
"--breakUpLongImportSelectors",
"--binPackImportSelectors",
"false",
"--unindentTopLevelOperators",
"true",
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/scala/org/scalafmt/ScalafmtStyle.scala
Expand Up @@ -124,7 +124,7 @@ import sourcecode.Text
* @param alignTokens Documented in scalafmt --help page.
* @param spacesInImportCurlyBraces If true, formats `import a.b.{ c, d }`.
* If false, formats `import a.b.{c, d}`.
* @param breakUpLongImportSelectors If true, breaks up import selectors if
* @param binPackImportSelectors If true, breaks up import selectors if
* it overflows [[maxColumn]]. For example
* import a.b.{
* c,
Expand All @@ -151,7 +151,7 @@ case class ScalafmtStyle(
continuationIndentDefnSite: Int,
alignMixedOwners: Boolean,
alignTokens: Set[AlignToken],
breakUpLongImportSelectors: Boolean,
binPackImportSelectors: Boolean,
spacesInImportCurlyBraces: Boolean,
allowNewlineBeforeColonInMassiveReturnTypes: Boolean,
binPackParentConstructors: Boolean,
Expand Down Expand Up @@ -196,7 +196,7 @@ object ScalafmtStyle {
continuationIndentDefnSite = 4,
alignMixedOwners = false,
alignTokens = Set.empty[AlignToken],
breakUpLongImportSelectors = true,
binPackImportSelectors = false,
spacesInImportCurlyBraces = false,
allowNewlineBeforeColonInMassiveReturnTypes = true,
binPackParentConstructors = false,
Expand Down Expand Up @@ -235,7 +235,7 @@ object ScalafmtStyle {
binPackArguments = true,
binPackParameters = true,
superfluousParensIndent = 4,
breakUpLongImportSelectors = false,
binPackImportSelectors = true,
allowNewlineBeforeColonInMassiveReturnTypes = false,
scalaDocs = false,
binPackParentConstructors = true,
Expand Down
64 changes: 33 additions & 31 deletions core/src/main/scala/org/scalafmt/internal/Router.scala
Expand Up @@ -117,14 +117,14 @@ class Router(formatOps: FormatOps) {
val close = matchingParentheses(hash(open))
val newlineBeforeClosingCurly = newlineBeforeClosingCurlyPolicy(close)

val newlinePolicy =
if (style.binPackImportSelectors) newlineBeforeClosingCurly
else newlineBeforeClosingCurly.andThen(OneArgOneLineSplit(open))
Seq(
Split(if (style.spacesInImportCurlyBraces) Space else NoSplit, 0)
.withPolicy(policy),
Split(
Newline,
1,
ignoreIf = !style.breakUpLongImportSelectors || isInterpolate)
.withPolicy(newlineBeforeClosingCurly)
Split(Newline, 1, ignoreIf = isInterpolate)
.withPolicy(newlinePolicy)
.withIndent(2, close, Right)
)
case FormatToken(_, close @ RightBrace(), _)
Expand Down Expand Up @@ -211,7 +211,8 @@ class Router(formatOps: FormatOps) {
if leftOwner.is[Term.Function] =>
val endOfFunction = functionExpire(
leftOwner.asInstanceOf[Term.Function])
val hasBlock = nextNonComment(formatToken).right.isInstanceOf[LeftBrace]
val hasBlock =
nextNonComment(formatToken).right.isInstanceOf[LeftBrace]
Seq(
Split(Space, 0, ignoreIf = isInlineComment(right))
.withPolicy(SingleLineBlock(endOfFunction)),
Expand Down Expand Up @@ -292,9 +293,8 @@ class Router(formatOps: FormatOps) {
)
// Opening [ with no leading space.
// Opening ( with no leading space.
case FormatToken(KwSuper() |
KwThis() | Ident(_) | RightBracket() | RightBrace() |
RightParen() | Underscore(),
case FormatToken(KwSuper() | KwThis() | Ident(_) | RightBracket() |
RightBrace() | RightParen() | Underscore(),
LeftParen() | LeftBracket(),
_) if noSpaceBeforeOpeningParen(rightOwner) && {
leftOwner.parent.forall {
Expand Down Expand Up @@ -439,12 +439,12 @@ class Router(formatOps: FormatOps) {
val close = matchingParentheses(hash(open))
val indent = getApplyIndent(leftOwner)
val (lhs, args) = getApplyArgs(formatToken, leftOwner)
val optimal =
leftOwner.tokens.find(_.is[Comma]).orElse(Some(close))
val optimal = leftOwner.tokens.find(_.is[Comma]).orElse(Some(close))
val isBracket = open.is[LeftBracket]
// TODO(olafur) DRY. Same logic as in default.
val exclude =
if (isBracket) insideBlock(formatToken, close, _.isInstanceOf[LeftBracket])
if (isBracket)
insideBlock(formatToken, close, _.isInstanceOf[LeftBracket])
else insideBlock(formatToken, close, x => x.isInstanceOf[LeftBrace])
val excludeRanges = exclude.map(parensRange)
val unindent =
Expand Down Expand Up @@ -753,7 +753,8 @@ class Router(formatOps: FormatOps) {
if (isAttachedComment(right, between)) Space
else Newline

val exclude = insideBlock(formatToken, expire, _.isInstanceOf[LeftBrace])
val exclude =
insideBlock(formatToken, expire, _.isInstanceOf[LeftBrace])
rhs match {
case _: Term.ApplyInfix =>
val modification = newlines2Modification(between)
Expand Down Expand Up @@ -866,7 +867,8 @@ class Router(formatOps: FormatOps) {
else {
Policy({
// Force template to be multiline.
case d @ Decision(FormatToken(open @ LeftBrace(), right, _), splits)
case d @ Decision(FormatToken(open @ LeftBrace(), right, _),
splits)
if !hasSelfAnnotation &&
!right.is[RightBrace] && // corner case, body is {}
childOf(template, owners(open)) =>
Expand Down Expand Up @@ -902,15 +904,14 @@ class Router(formatOps: FormatOps) {
}
// If/For/While/For with (
case FormatToken(open @ LeftParen(), _, _) if {
val isSuperfluous =
isSuperfluousParenthesis(open, leftOwner)
leftOwner match {
case _: Term.If | _: Term.While | _: Term.For | _: Term.ForYield
if !isSuperfluous =>
true
case _ => false
}
} =>
val isSuperfluous = isSuperfluousParenthesis(open, leftOwner)
leftOwner match {
case _: Term.If | _: Term.While | _: Term.For | _: Term.ForYield
if !isSuperfluous =>
true
case _ => false
}
} =>
val close = matchingParentheses(hash(open))
val penalizeNewlines = penalizeNewlineByNesting(open, close)
val indent: Length =
Expand Down Expand Up @@ -938,7 +939,8 @@ class Router(formatOps: FormatOps) {
.withPolicy(SingleLineBlock(expire)),
Split(Space, 1).withPolicy(breakOnlyBeforeElse)
)
case FormatToken(close @ RightParen(), right, between) if (leftOwner match {
case FormatToken(close @ RightParen(), right, between)
if (leftOwner match {
case _: Term.If | _: Term.For | _: Term.ForYield => true
case _ => false
}) && !isFirstOrLastToken(close, leftOwner) =>
Expand All @@ -954,8 +956,8 @@ class Router(formatOps: FormatOps) {
if (attachedComment)
Space // Inline comment will force newline later.
else Newline
val exclude = insideBlock(formatToken, expire, _.is[LeftBrace])
.map(parensRange)
val exclude =
insideBlock(formatToken, expire, _.is[LeftBrace]).map(parensRange)
Seq(
Split(Space, 0, ignoreIf = attachedComment || newlines > 0)
.withPolicy(SingleLineBlock(expire, exclude = exclude)),
Expand Down Expand Up @@ -1044,10 +1046,10 @@ class Router(formatOps: FormatOps) {
Split(Space, 0),
Split(Newline, 1)
)
case tok @ FormatToken(
Ident(_) | Literal() | Interpolation.End() | Xml.End(),
Ident(_) | Literal() | Xml.Start(),
_) =>
case tok @ FormatToken(Ident(_) | Literal() | Interpolation.End() |
Xml.End(),
Ident(_) | Literal() | Xml.Start(),
_) =>
Seq(
Split(Space, 0)
)
Expand Down Expand Up @@ -1194,7 +1196,7 @@ class Router(formatOps: FormatOps) {
Seq(
Split(NoSplit, 0)
)
case FormatToken(left, kw@Keyword(), _) =>
case FormatToken(left, kw @ Keyword(), _) =>
if (!left.is[RightBrace] &&
Set("finally", "catch").contains(kw.syntax)) {
Seq(Split(Newline, 0))
Expand Down
5 changes: 0 additions & 5 deletions core/src/test/resources/import/Import.stat

This file was deleted.

15 changes: 15 additions & 0 deletions core/src/test/resources/scalajs/Import.source
@@ -0,0 +1,15 @@
80 columns |
<<< break #101
import org.scalajs.dom.experimental.serviceworkers.{ServiceWorkerGlobalScope, ServiceWorkerRegistration}
>>>
import org.scalajs.dom.experimental.serviceworkers.{
ServiceWorkerGlobalScope, ServiceWorkerRegistration
}
<<< break #101 short 2
import org.{Aaaa, Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, Ccccccccccccccccccccccccccccccc, D, E, F, G, H, I, J, K, L}
>>>
import org.{
Aaaa, Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, Ccccccccccccccccccccccccccccccc, D, E,
F, G, H, I, J, K, L
}

22 changes: 17 additions & 5 deletions core/src/test/resources/unit/Import.source
Expand Up @@ -71,17 +71,29 @@ import org.scalajs.dom.experimental.serviceworkers.{
ServiceWorkerGlobalScope,
ServiceWorkerRegistration
}
<<< ONLY break #101 short
import org.{A, GlobalScope, Registration}
<<< break #101 short
import org.{Aaaaaa, GlobalScope, Registration}
>>>
import org.{
Aaaaaa,
GlobalScope,
Registration
}
<<< ONLY break #101 short 2
<<< break #101 short 2
import org.{Aaaa, Bbbbb, C, D, E, F, G, H, I, J, K, L}
>>>
import org.{
GlobalScope,
Registration
Aaaa,
Bbbbb,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L
}

2 changes: 1 addition & 1 deletion core/src/test/scala/org/scalafmt/util/HasTests.scala
Expand Up @@ -132,7 +132,7 @@ trait HasTests extends FunSuiteLike with FormatAssertions {
case "spacesBeforeContextBound" =>
ScalafmtStyle.default.copy(spaceBeforeContextBoundColon = true)
case "import" =>
ScalafmtStyle.default.copy(breakUpLongImportSelectors = false)
ScalafmtStyle.default.copy(binPackImportSelectors = false)
case style => throw UnknownStyle(style)
}

Expand Down

0 comments on commit 8f67462

Please sign in to comment.