Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add SpacesWithinPatternBinders preference (closes #15)

  • Loading branch information...
commit b58a81b5abeea817c701d2bf8d9d133a6f9b17ba 1 parent b1e493c
@mdr authored
View
1  CHANGELOG
@@ -4,6 +4,7 @@
* Add SpaceInsideParentheses and SpaceInsideBrackets preferences (issue #14)
* Ability to import/export preferences as properties; --preferencesFile=<path> command-line option
* FIX: incorrect indent behaviour for finally block after a catch block
+* Add SpacesWithinPatternBinders preference (issue #15)
0.0.9 (26/February/11)
View
14 README.rst
@@ -152,6 +152,7 @@ Usage::
[+|-]spaceBeforeColon Enable/disable Add a space before colons
[+|-]spaceInsideBrackets Enable/disable Require a space after '[' and before ']'
[+|-]spaceInsideParentheses Enable/disable Require a space after '(' and before ')'
+ [+|-]spacesWithinPatternBinders Enable/disable Add a space around the @ token in pattern binders
-alignSingleLineCaseStatements.maxArrowIndent=[1-100] Set Maximum number of spaces inserted before an arrow to align case statements
-indentSpaces=[1-10] Set Number of spaces to use for indentation
@@ -446,6 +447,19 @@ If ``false``, then::
def main(args : Array[String])
+spacesWithinPatternBinders
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Default: ``true``
+
+Whether to add a space around the @ token in pattern binders. For example, if ``true``,::
+
+ case elem @ Multi(values @ _*) =>
+
+If ``false``,::
+
+ case elem@Multi(values@_*) =>
+
Scala Style Guide
~~~~~~~~~~~~~~~~~
View
29 scalariform.maven-plugin/src/main/java/org/scalariform/ScalariformMojo.java
@@ -51,6 +51,11 @@
protected boolean formatXml;
/**
+ * @parameter default-value=false
+ */
+ protected boolean indentLocalDefs;
+
+ /**
* @parameter default-value=true
*/
protected boolean indentPackageBlocks;
@@ -63,6 +68,11 @@
/**
* @parameter default-value=false
*/
+ protected boolean preserveDanglingCloseParenthesis;
+
+ /**
+ * @parameter default-value=false
+ */
protected boolean preserveSpaceBeforeArguments;
/**
@@ -75,7 +85,21 @@
*/
protected boolean spaceBeforeColon;
+ /**
+ * @parameter default-value=false
+ */
+ protected boolean spacesInsideBrackets;
+
+ /**
+ * @parameter default-value=false
+ */
+ protected boolean spacesInsideParentheses;
+ /**
+ * @parameter default-value=true
+ */
+ protected boolean spacesWithinPatternBinders;
+
public void execute() throws MojoExecutionException {
MojoFormatter.format(baseDir, this.getLog(),
@@ -85,10 +109,15 @@ public void execute() throws MojoExecutionException {
compactStringConcatenation,
doubleIndentClassDeclaration,
formatXml,
+ indentLocalDefs,
indentPackageBlocks,
+ preserveDanglingCloseParenthesis,
preserveSpaceBeforeArguments,
rewriteArrowSymbols,
spaceBeforeColon,
+ spacesInsideBrackets,
+ spacesInsideParentheses,
+ spacesWithinPatternBinders,
indentSpaces);
}
View
10 scalariform.maven-plugin/src/main/scala/org/scalariform/MojoFormatter.scala
@@ -48,10 +48,15 @@ object MojoFormatter {
compactStringConcatenation: Boolean,
doubleIndentClassDeclaration: Boolean,
formatXml: Boolean,
+ indentLocalDefs: Boolean,
indentPackageBlocks: Boolean,
+ preserveDanglingCloseParenthesis: Boolean,
preserveSpaceBeforeArguments: Boolean,
rewriteArrowSymbols: Boolean,
spaceBeforeColon: Boolean,
+ spacesInsideBrackets: Boolean,
+ spacesInsideParentheses: Boolean,
+ spacesWithinPatternBinders: Boolean,
indentSpaces: Int) {
val preferences = FormattingPreferences()
@@ -61,10 +66,15 @@ object MojoFormatter {
.setPreference(DoubleIndentClassDeclaration, doubleIndentClassDeclaration)
.setPreference(CompactStringConcatenation, compactStringConcatenation)
.setPreference(FormatXml, formatXml)
+ .setPreference(IndentLocalDefs, indentLocalDefs)
.setPreference(IndentPackageBlocks, indentPackageBlocks)
+ .setPreference(PreserveDanglingCloseParenthesis, preserveDanglingCloseParenthesis)
.setPreference(PreserveSpaceBeforeArguments, preserveSpaceBeforeArguments)
.setPreference(RewriteArrowSymbols, rewriteArrowSymbols)
.setPreference(SpaceBeforeColon, spaceBeforeColon)
+ .setPreference(SpacesInsideParentheses, spacesInsideParentheses)
+ .setPreference(SpacesInsideBrackets, spacesInsideBrackets)
+ .setPreference(SpacesWithinPatternBinders, spacesWithinPatternBinders)
.setPreference(IndentSpaces, indentSpaces)
val files = findScalaFiles(path)
View
1  scalariform/src/main/scala/scalariform/formatter/AnnotationFormatter.scala
@@ -10,6 +10,7 @@ trait AnnotationFormatter { self: HasFormattingPreferences with TypeFormatter wi
val Annotation(atToken: Token, annotationType: Type, argumentExprss: List[ArgumentExprs], newlineOption: Option[Token]) = annotation
var formatResult: FormatResult = NoFormatResult
+ formatResult = formatResult.before(annotationType.firstToken, Compact)
formatResult ++= format(annotationType)
for (argumentExprs argumentExprss)
formatResult ++= format(argumentExprs)._1
View
4 scalariform/src/main/scala/scalariform/formatter/ScalaFormatter.scala
@@ -367,6 +367,10 @@ abstract class ScalaFormatter extends HasFormattingPreferences with TypeFormatte
if (type1.isId && type2.isId)
return CompactEnsuringGap
val firstCharOfToken2 = token2.getText.head
+ if (formattingPreferences(SpacesWithinPatternBinders) && type1.isId && type2 == AT)
+ return CompactEnsuringGap
+ if (formattingPreferences(SpacesWithinPatternBinders) && type1 == AT)
+ return CompactEnsuringGap
if (Set(HASH, AT).contains(type1) && isOperatorPart(firstCharOfToken2))
return CompactEnsuringGap
val lastCharOfToken1 = token1.getText.last
View
10 scalariform/src/main/scala/scalariform/formatter/preferences/PreferenceDescriptor.scala
@@ -63,7 +63,7 @@ object AllPreferences {
val preferences: List[PreferenceDescriptor[_]] = List(RewriteArrowSymbols, IndentSpaces, SpaceBeforeColon, CompactStringConcatenation,
PreserveSpaceBeforeArguments, AlignParameters, DoubleIndentClassDeclaration, FormatXml, IndentPackageBlocks,
AlignSingleLineCaseStatements, AlignSingleLineCaseStatements.MaxArrowIndent, IndentLocalDefs, PreserveDanglingCloseParenthesis,
- SpaceInsideParentheses, SpaceInsideBrackets)
+ SpaceInsideParentheses, SpaceInsideBrackets, SpacesWithinPatternBinders)
val preferencesByKey: Map[String, PreferenceDescriptor[_]] = {
var map: Map[String, PreferenceDescriptor[_]] = Map()
@@ -165,4 +165,10 @@ case object SpaceInsideBrackets extends BooleanPreferenceDescriptor {
val key = "spaceInsideBrackets"
val description = "Require a space after '[' and before ']'"
val defaultValue = false
-}
+}
+
+case object SpacesWithinPatternBinders extends BooleanPreferenceDescriptor {
+ val key = "spacesWithinPatternBinders"
+ val description = "Add a space around the @ token in pattern binders"
+ val defaultValue = true
+}
View
23 scalariform/src/test/scala/scalariform/formatter/CaseClausesFormatterTest.scala
@@ -88,6 +88,9 @@ class CaseClausesFormatterTest extends AbstractExpressionFormatterTest {
"a match { case b => ; c }" ==> "a match { case b => ; c }"
+ {
+ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesWithinPatternBinders, false)
+
"""a match {
| case b(c @ ~()) =>
| case b(c@ ~()) =>
@@ -96,7 +99,7 @@ class CaseClausesFormatterTest extends AbstractExpressionFormatterTest {
| case b(c@ ~()) =>
| case b(c@ ~()) =>
|}"""
-
+ }
{
@@ -256,4 +259,22 @@ class CaseClausesFormatterTest extends AbstractExpressionFormatterTest {
|}"""
+ {
+ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesWithinPatternBinders, false)
+
+ """(a: @switch) match {
+ |case elem@Multi(values@_*) =>
+ |}""" ==>
+ """(a: @switch) match {
+ | case elem@Multi(values@_*) =>
+ |}"""
+ }
+
+ """(a: @switch) match {
+ |case elem@Multi(values@_*) =>
+ |}""" ==>
+ """(a: @switch) match {
+ | case elem @ Multi(values @ _*) =>
+ |}"""
+
}
View
12 scalariform/src/test/scala/scalariform/formatter/MiscExpressionFormatterTest.scala
@@ -115,13 +115,18 @@ class MiscExpressionFormatterTest extends AbstractExpressionFormatterTest {
| } => 42
|}"""
+ {
+ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesWithinPatternBinders, false)
+
"""42 match {
| case foo_ @Bar =>
|}""" ==>
"""42 match {
| case foo_ @Bar =>
|}"""
-
+
+ }
+
"NEWLINE" ==> "NEWLINE"
"NEWLINES" ==> "NEWLINES"
@@ -187,13 +192,16 @@ class MiscExpressionFormatterTest extends AbstractExpressionFormatterTest {
| println("bar")
|})"""
+ {
+ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesWithinPatternBinders, false)
+
"""b match {
|case y@ <phone/> =>
|}""" ==>
"""b match {
| case y@ <phone/> =>
|}""" // TODO: Whitespace around @ in this case?
-
+ }
"""1 / // foo
|2""" ==>
View
10 scalariform/src/test/scala/scalariform/formatter/TemplateFormatterTest.scala
@@ -60,6 +60,16 @@ class TemplateFormatterTest extends AbstractFormatterTest {
|@A/*b*/
|@B(c = "d")/*c*/
|abstract class/*d*/E/*e*/[F]/*f*/@G()/*g*/private/*h*/(val h: I)/*i*/(implicit j: K)/*j*/extends/*k*/{} with/*l*/L(2) with M/*m*/{}""" because "sort out what we want"
+
+
+ {
+ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesWithinPatternBinders, true)
+
+ """@(Id@Field) class A""" ==>
+ """@(Id @Field)
+ |class A"""
+
+ }
"""class A {
|
Please sign in to comment.
Something went wrong with that request. Please try again.