Permalink
Browse files

Add SpaceInsideParentheses and SpaceInsideBrackets preferences (closes

  • Loading branch information...
1 parent 02ed3ed commit 9255ed442ff839185ef5109b9c838d09ea48156d @mdr mdr committed Mar 26, 2011
View
@@ -1,6 +1,7 @@
0.1.0 (..)
* Add forgiving mode to lexer
+* Add SpaceInsideParentheses and SpaceInsideBrackets preferences (issue #14)
0.0.9 (26/February/11)
View
@@ -149,6 +149,8 @@ Usage::
[+|-]preserveSpaceBeforeArguments Enable/disable Preserve a space before a parenthesis argument
[+|-]rewriteArrowSymbols Enable/disable Replace arrow tokens with unicode equivalents: => with ⇒, and <- with ←
[+|-]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 ')'
-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
@@ -417,6 +419,33 @@ If ``true``, then::
def add(a : Int, b : Int) : Int = a + b
+spaceInsideBrackets
+~~~~~~~~~~~~~~~~~~~
+
+Default: ``false``
+
+Whether to use a space inside type brackets. For example, if ``true``, then::
+
+ Array[ String ]
+
+If ``false``, then::
+
+ Array[String]
+
+spaceInsideParentheses
+~~~~~~~~~~~~~~~~~~~~~~
+
+Default: ``false``
+
+Whether to use a space inside non-empty parentheses. For example, if ``true``, then::
+
+ def main( args : Array[String] )
+
+If ``false``, then::
+
+ def main(args : Array[String])
+
+
Scala Style Guide
~~~~~~~~~~~~~~~~~
@@ -436,6 +465,8 @@ indentSpaces ``2``
preserveSpaceBeforeArguments ``false``
rewriteArrowSymbols ``false``
spaceBeforeColon ``false``
+spaceInsideBrackets ``false``
+spaceInsideParentheses ``false``
============================ ========= =========
Source directives
@@ -319,6 +319,14 @@ abstract class ScalaFormatter extends HasFormattingPreferences with TypeFormatte
val type2 = token2.getType
if (type2 == EOF)
return Compact
+ if (type1 == LPAREN && type2 != RPAREN && formattingPreferences(SpaceInsideParentheses))
+ return CompactEnsuringGap
+ if (type1 != LPAREN && type2 == RPAREN && formattingPreferences(SpaceInsideParentheses))
+ return CompactEnsuringGap
+ if (type1 == LBRACKET && type2 != RBRACKET && formattingPreferences(SpaceInsideBrackets))
+ return CompactEnsuringGap
+ if (type1 != LBRACKET && type2 == RBRACKET && formattingPreferences(SpaceInsideBrackets))
+ return CompactEnsuringGap
val xmlPreviousExceptions = Set(LBRACE, LPAREN, NEWLINE, NEWLINES)
if (type1 == TYPE && type2.isId)
return CompactEnsuringGap
@@ -11,9 +11,9 @@ sealed trait PreferenceType[T] {
case object BooleanPreference extends PreferenceType[Boolean] {
def parseValue(s: String) =
s.toLowerCase match {
- case "true" Right(true)
+ case "true" Right(true)
case "false" Right(false)
- case _ Left("Could not parse as boolean value: " + s)
+ case _ Left("Could not parse as boolean value: " + s)
}
}
@@ -56,7 +56,8 @@ abstract trait BooleanPreferenceDescriptor extends PreferenceDescriptor[Boolean]
object AllPreferences {
val preferences: List[PreferenceDescriptor[_]] = List(RewriteArrowSymbols, IndentSpaces, SpaceBeforeColon, CompactStringConcatenation,
PreserveSpaceBeforeArguments, AlignParameters, DoubleIndentClassDeclaration, FormatXml, IndentPackageBlocks,
- AlignSingleLineCaseStatements, AlignSingleLineCaseStatements.MaxArrowIndent, IndentLocalDefs, PreserveDanglingCloseParenthesis)
+ AlignSingleLineCaseStatements, AlignSingleLineCaseStatements.MaxArrowIndent, IndentLocalDefs, PreserveDanglingCloseParenthesis,
+ SpaceInsideParentheses, SpaceInsideBrackets)
val preferencesByKey: Map[String, PreferenceDescriptor[_]] = {
var map: Map[String, PreferenceDescriptor[_]] = Map()
@@ -144,6 +145,18 @@ case object IndentLocalDefs extends BooleanPreferenceDescriptor {
case object PreserveDanglingCloseParenthesis extends BooleanPreferenceDescriptor {
val key = "preserveDanglingCloseParenthesis"
- val description = "Allow a newline before a ')' in an argument expression."
+ val description = "Allow a newline before a ')' in an argument expression"
val defaultValue = false
}
+
+case object SpaceInsideParentheses extends BooleanPreferenceDescriptor {
+ val key = "spaceInsideParentheses"
+ val description = "Require a space after '(' and before ')'"
+ val defaultValue = false
+}
+
+case object SpaceInsideBrackets extends BooleanPreferenceDescriptor {
+ val key = "spaceInsideBrackets"
+ val description = "Require a space after '[' and before ']'"
+ val defaultValue = false
+}
@@ -289,7 +289,7 @@ class AstSelectorTest extends FlatSpec with ShouldMatchers {
case class IntermediateTest(source: String, initialSelectionDiagram: String) {
def ~(finalSelectionDiagram: String): IntermediateTest = {
val initialSelection = findSelectionRange(initialSelectionDiagram)
- val actualFinalSelection = new AstSelector(source).expandSelection(initialSelection) getOrElse initialSelection
+ val actualFinalSelection = AstSelector.expandSelection(source, initialSelection) getOrElse initialSelection
val expectedFinalSelection = findSelectionRange(finalSelectionDiagram)
("source\n>>>" + source + "<<<\n") should "expand\n>>>" + (initialSelectionDiagram + "<<<\n to \n>>>" + finalSelectionDiagram + "<<<\n") in {
actualFinalSelection should equal (expectedFinalSelection)
@@ -0,0 +1,24 @@
+package scalariform.formatter
+
+import scalariform.formatter.preferences._
+
+class ParenAndBracketSpacingTest extends AbstractExpressionFormatterTest {
+
+ {
+ implicit val formattingPreferences = FormattingPreferences.setPreference(SpaceInsideParentheses, true)
+ "()" ==> "()"
+ "(a: Int) => 3" ==> "( a: Int ) => 3"
+ "(3)" ==> "( 3 )"
+ "(3, 4)" ==> "( 3, 4 )"
+ "for (n <- 1 to 10) yield foo(n, n)" ==> "for ( n <- 1 to 10 ) yield foo( n, n )"
+ }
+
+ {
+ implicit val formattingPreferences = FormattingPreferences.setPreference(SpaceInsideBrackets, true)
+ "x: List[String]" ==> "x: List[ String ]"
+ "foo[Bar](baz)" ==> "foo[ Bar ](baz)"
+ "{ class A[B] { private[this] val bob } }" ==> "{ class A[ B ] { private[ this ] val bob } }"
+ "super[X].y" ==> "super[ X ].y"
+ }
+
+}

0 comments on commit 9255ed4

Please sign in to comment.