Skip to content

Commit

Permalink
Add SpaceInsideParentheses and SpaceInsideBrackets preferences (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdr committed Mar 26, 2011
1 parent 02ed3ed commit 9255ed4
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -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)

Expand Down
31 changes: 31 additions & 0 deletions README.rst
Expand Up @@ -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
Expand Down Expand Up @@ -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
~~~~~~~~~~~~~~~~~

Expand All @@ -436,6 +465,8 @@ indentSpaces ``2``
preserveSpaceBeforeArguments ``false``
rewriteArrowSymbols ``false``
spaceBeforeColon ``false``
spaceInsideBrackets ``false``
spaceInsideParentheses ``false``
============================ ========= =========

Source directives
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Up @@ -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)
Expand Down
@@ -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.