Skip to content

Commit

Permalink
Merge pull request #253 from godenji/0.2.4
Browse files Browse the repository at this point in the history
0.2.4 release
  • Loading branch information
godenji authored Oct 25, 2017
2 parents e316052 + c4f3fc9 commit 52b7b18
Show file tree
Hide file tree
Showing 27 changed files with 312 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.2.4 (25/October/17)

* Updated to Scala 2.12.4
* Updated to Sbt 1.0.2
* Added support for multiline param groups (fixes #77)

0.2.3 (10/August/17)

* Updated to Scala 2.12.3
Expand Down
19 changes: 17 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Usage within a project

Have a use for the scalariform source code directly? You can use it as a build dependency: ::

"org.scalariform" %% "scalariform" % "0.2.3"
"org.scalariform" %% "scalariform" % "0.2.4"

Integration with Eclipse
------------------------
Expand Down Expand Up @@ -297,7 +297,7 @@ alignSingleLineCaseStatements.maxArrowIndent

Default: ``40``

When ``alignSingleLineCaseStatements`` is ``true``, this is a limit on
When ``alignSingleLineCaseStatements`` is ``true`` there is a limit on
the number of spaces that can be inserted before an arrow to align it
with other case statements. This can be used to avoid very large gaps,
e.g.:
Expand All @@ -309,6 +309,21 @@ e.g.:
case ccc => 2
}
allowParamGroupsOnNewlines
~~~~~~~~~~~~~~~~~~~~~~~~~~

Default: ``false``

When ``allowParamGroupsOnNewlines`` is ``true`` the default behavior of collapsing
param groups into a single line is disabled. This allows for the following formatting style:

.. code:: scala
def foo[T]
(a: A)
(b: B)
(implicit t: T)
compactControlReadability
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
9 changes: 6 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import com.typesafe.sbt.SbtScalariform.ScalariformKeys
import scalariform.formatter.preferences._

import scala.sys.process._
import sbt.io.Using

lazy val commonSettings = inConfig(Test)(Defaults.testSettings) ++
SbtScalariform.defaultScalariformSettings ++ Seq(
Seq(
organization := "org.scalariform",
sonatypeProfileName := organization.value,
scalaVersion := crossScalaVersions.value.head,
crossScalaVersions := Seq(
"2.12.3",
"2.12.4",
"2.11.11",
"2.10.6"
),
Expand Down Expand Up @@ -120,7 +123,7 @@ lazy val cli = (project
mainClass in assembly := Some("scalariform.commandline.Main"),
artifact in (Compile, assembly) := {
val art = (artifact in (Compile, assembly)).value
art.copy(`classifier` = Some("assembly"))
art.withClassifier(Some("assembly"))
}
)
settings(addArtifact(artifact in (Compile, assembly), assembly))
Expand Down
1 change: 1 addition & 0 deletions formatterPreferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
alignParameters=true
alignSingleLineCaseStatements=true
#alignSingleLineCaseStatements.maxArrowIndent=40
#allowParamGroupsOnNewlines=false
#compactControlReadability=false
#compactStringConcatenation=false
danglingCloseParenthesis=Force
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.scalariform</groupId>
<artifactId>scalariform.parent</artifactId>
<version>0.2.3</version>
<version>0.2.4</version>
<packaging>pom</packaging>

<!-- scm configuration is require to extract the github hash-->
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.15
sbt.version=1.0.2
8 changes: 4 additions & 4 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.7.1")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.6.1")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1")
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.1")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.7.0")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")
2 changes: 1 addition & 1 deletion scalariform.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<feature
id="scalariform.feature"
label="Scalariform"
version="0.2.3">
version="0.2.4">

<description>
Scala Code formatter
Expand Down
2 changes: 1 addition & 1 deletion scalariform.feature/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<parent>
<artifactId>scalariform.parent</artifactId>
<groupId>org.scalariform</groupId>
<version>0.2.3</version>
<version>0.2.4</version>
</parent>
</project>
2 changes: 1 addition & 1 deletion scalariform.update/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<parent>
<artifactId>scalariform.parent</artifactId>
<groupId>org.scalariform</groupId>
<version>0.2.3</version>
<version>0.2.4</version>
</parent>
</project>
4 changes: 2 additions & 2 deletions scalariform.update/site.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<site>
<description name="Scalariform Update Site"
url="https://github.com/scala-ide/scalariform/tree/0.2.3/scalariform.update/target/site">
url="https://github.com/scala-ide/scalariform/tree/0.2.4/scalariform.update/target/site">
Scalariform Update Site
</description>
<feature url="features/scalariform.feature_0.2.3.jar" version="0.2.3" id="scalariform.feature">
<feature url="features/scalariform.feature_0.2.4.jar" version="0.2.4" id="scalariform.feature">
<category name="Scala"/>
</feature>
<category-def name="Scala" label="Scala"/>
Expand Down
Binary file modified scalariform.update/target/site.zip
Binary file not shown.
Binary file modified scalariform.update/target/site/artifacts.jar
Binary file not shown.
Binary file modified scalariform.update/target/site/content.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions scalariform.update/target/site/site.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<site>
<description name="Scalariform Update Site"
url="https://github.com/scala-ide/scalariform/tree/0.2.3/scalariform.update/target/site">
url="https://github.com/scala-ide/scalariform/tree/0.2.4/scalariform.update/target/site">
Scalariform Update Site
</description>
<feature url="features/scalariform.feature_0.2.3.jar" version="0.2.3" id="scalariform.feature">
<feature url="features/scalariform.feature_0.2.4.jar" version="0.2.4" id="scalariform.feature">
<category name="Scala"/>
</feature>
<category-def name="Scala" label="Scala"/>
Expand Down
Binary file modified scalariform.update/target/site_assembly.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion scalariform/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Scalariform
Bundle-SymbolicName: scalariform
Bundle-Version: 0.2.3
Bundle-Version: 0.2.4
Require-Bundle: org.scala-lang.scala-library,
org.scala-lang.modules.scala-xml
Bundle-ClassPath: .
Expand Down
2 changes: 1 addition & 1 deletion scalariform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<artifactId>scalariform.parent</artifactId>
<groupId>org.scalariform</groupId>
<version>0.2.3</version>
<version>0.2.4</version>
</parent>
<dependencies>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ abstract class ScalaFormatter
if (suspendFormatting)
builder.append(token.rawText)
else {
val basicFormattingInstruction = inferredNewlineFormatting.get(token) getOrElse
defaultNewlineFormattingInstruction(previousTokenOption, token, nextTokenOption)
val basicFormattingInstruction =
handleNewlineFormatting(
previousTokenOption, token, nextTokenOption, tokens, tokenIndentMap,
inferredNewlineFormatting.get(token)
)
val formattingInstruction =
if (nextTokenOption.exists { _.tokenType == EOF } && basicFormattingInstruction.isInstanceOf[EnsureNewlineAndIndent])
EnsureNewlineAndIndent(0) // Adjustment for end of input when using non-zero initial indent
Expand Down Expand Up @@ -346,13 +349,90 @@ abstract class ScalaFormatter
}
implicit def stringBuilder2stringBuilderExtra(builder: StringBuilder): StringBuilderExtra = new StringBuilderExtra(builder)

private lazy val allowParamGroupsOnNewlines = formattingPreferences(AllowParamGroupsOnNewlines)
private lazy val indentSpacesNum = formattingPreferences(IndentSpaces)

private def handleNewlineFormatting(
previousTokenOption: Option[Token],
token: Token,
nextTokenOption: Option[Token],
tokens: List[Token],
tokenIndentMap: Map[Token, Int],
maybeNewlineFormat: Option[IntertokenFormatInstruction]
): IntertokenFormatInstruction = {

def defaultNewline() = defaultNewlineFormattingInstruction(
previousTokenOption, token, nextTokenOption
)

if(!allowParamGroupsOnNewlines) maybeNewlineFormat.getOrElse(defaultNewline())
else {
// add support for newline param groups:
//
// def launchTheMisslesWithFeeling
// [R <: Role, U <: User]
// (role: R, user: U)
// (implicit t: Timer)
val maybeIndentLevel = nextTokenOption.map(_.tokenType) match {
case
Some(LPAREN) | Some(LBRACKET) if (
token.tokenType == NEWLINE && maybeNewlineFormat.isEmpty &&
!previousTokenOption.exists(_.tokenType == RBRACE) // exclude `}` followed by `(`
) =>

// backtrack through tokens to find first open brace/newline delimiter
val tokenGroup = tokens.splitAt(tokens.indexOf(token))._1
val delimiterToken = Utils.withPreviousAndNext(tokenGroup).reverse.find { case (prev, curr, next) =>
val tt = curr.tokenType
tt == LBRACE || tt == NEWLINES || (tt == NEWLINE &&
// keep backtracking if newline plus `[`, `(`, or `]`
!next.exists(n => Set(LBRACKET, LPAREN).exists(_ == n.tokenType)) &&
!prev.exists(_.tokenType == RBRACKET) // e.g. private[this]
)
}.map(_._2)

// first token after delimiter has indent level of param group's parent
delimiterToken.flatMap{ x =>
val indentToken = tokenGroup(tokenGroup.indexOf(x) + 1)
tokenIndentMap.get(indentToken)
}.
// top-level definition, use first token's indent level
orElse(
tokenGroup.headOption.flatMap(tokenIndentMap.get)
)
case _ => None
}

def calcIndent(i: Int) = (
i / indentSpacesNum + 1
)

maybeNewlineFormat match {
case Some(t: EnsureNewlineAndIndent) => // apply new indent level
maybeIndentLevel.map( x => t.copy(indentLevel = calcIndent(x))).getOrElse(t)
case Some(t) => t
case None =>
maybeIndentLevel.map { num =>
EnsureNewlineAndIndent(calcIndent(num))
}.
getOrElse(defaultNewline())
}
}
}

private def isDeclarationStart(tokenType: Option[TokenType]) = {
tokenType.exists(
Set(CASE, CLASS, TRAIT, OBJECT, DEF, VAL, VAR, TYPE, ABSTRACT, FINAL, SEALED, OVERRIDE, IMPLICIT, LAZY)
)
}

private def defaultNewlineFormattingInstruction(previousTokenOption: Option[Token], token: Token, nextTokenOption: Option[Token]): IntertokenFormatInstruction = {
val previousTypeOption = previousTokenOption map { _.tokenType }
val nextTypeOption = nextTokenOption map { _.tokenType }
val result =
if (previousTypeOption == Some(TYPE))
CompactEnsuringGap
else if (previousTypeOption == Some(RBRACKET) && nextTypeOption.exists(Set(CASE, CLASS, TRAIT, OBJECT, DEF, VAL, VAR, TYPE, ABSTRACT, FINAL, SEALED, OVERRIDE, IMPLICIT, LAZY)))
else if (previousTypeOption == Some(RBRACKET) && isDeclarationStart(nextTypeOption))
CompactEnsuringGap
else if (nextTypeOption == Some(LBRACE))
CompactEnsuringGap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ trait IntegerPreferenceDescriptor extends PreferenceDescriptor[Int] {
object AllPreferences {
val preferences: List[PreferenceDescriptor[_]] = List(
AlignArguments, AlignParameters, AlignSingleLineCaseStatements, AlignSingleLineCaseStatements.MaxArrowIndent,
CompactControlReadability, CompactStringConcatenation, DanglingCloseParenthesis, DoubleIndentClassDeclaration,
DoubleIndentConstructorArguments, DoubleIndentMethodDeclaration, FirstArgumentOnNewline, FirstParameterOnNewline,
FormatXml, IndentLocalDefs, IndentPackageBlocks, IndentSpaces, IndentWithTabs, MultilineScaladocCommentsStartOnFirstLine,
AllowParamGroupsOnNewlines, CompactControlReadability, CompactStringConcatenation, DanglingCloseParenthesis,
DoubleIndentClassDeclaration, DoubleIndentConstructorArguments, DoubleIndentMethodDeclaration, FirstArgumentOnNewline,
FirstParameterOnNewline, FormatXml, IndentLocalDefs, IndentPackageBlocks, IndentSpaces, IndentWithTabs, MultilineScaladocCommentsStartOnFirstLine,
NewlineAtEndOfFile, PlaceScaladocAsterisksBeneathSecondAsterisk, PreserveSpaceBeforeArguments, RewriteArrowSymbols,
SpaceBeforeColon, SpaceBeforeContextColon, SpaceInsideBrackets, SpaceInsideParentheses, SpacesAroundMultiImports, SpacesWithinPatternBinders
)
Expand Down Expand Up @@ -129,6 +129,12 @@ case object AlignSingleLineCaseStatements extends BooleanPreferenceDescriptor {
}
}

case object AllowParamGroupsOnNewlines extends BooleanPreferenceDescriptor {
val key = "allowParamGroupsOnNewlines"
val description = "Allow class/method param groups to be separated by newlines"
val defaultValue = false
}

case object CompactControlReadability extends BooleanPreferenceDescriptor {
val key = "compactControlReadability"
val description = "Compact Control Readability style"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ object NewlineInferencer {

private val TOKENS_WHICH_CANNOT_BEGIN_A_STATEMENT: Set[TokenType] = Set(
CATCH, ELSE, EXTENDS, FINALLY, FORSOME, MATCH, WITH, YIELD, COMMA, DOT, SEMI, COLON, /* USCORE, */ EQUALS,
ARROW, LARROW, SUBTYPE, VIEWBOUND, SUPERTYPE, HASH, LBRACKET, RPAREN, RBRACKET, RBRACE
ARROW, LARROW, SUBTYPE, VIEWBOUND, SUPERTYPE, HASH, RPAREN, RBRACE, RBRACKET
)

}
Loading

0 comments on commit 52b7b18

Please sign in to comment.