Permalink
Browse files

Add option to support CompactControlReadability style -- thanks to by…

… Owein Reese (https://github.com/wheaties) and Rose Toomey (https://github.com/rktoomey) for the patch (closes #22)
  • Loading branch information...
1 parent e260364 commit 39c2e4c4c994e2b46bc6d62968a5675c7b3c19d6 @mdr mdr committed Jul 18, 2011
View
@@ -5,6 +5,7 @@
* ParenExpr now allows newline after opening paren (issue #18)
* FIX: spurious indentation in staggered dot expressions (issue #25)
* Preserve newline before annotations (issue #28)
+* Add option to support CompactControlReadability style (issue #22) (thanks to by Owein Reese (https://github.com/wheaties) and Rose Toomey (https://github.com/rktoomey) for the patch
0.1.0 (16/July/11)
View
@@ -3,3 +3,4 @@ Scalariform is maintained by Matt Russell (https://github.com/mdr/scalariform).
* A command line option to read a list of files was contributed by Olivier Michallat (https://github.com/olim7t).
* The Maven formatter plugin was written by Adam Crain (https://github.com/jadamcrain).
* Better Vim integration via a command line option to return the input unchanged on parse error was contributed by Oliver Braun (https://github.com/obcode).
+* CompactControlReadability patch by Owein Reese (https://github.com/wheaties) and Rose Toomey (https://github.com/rktoomey)
View
@@ -259,8 +259,11 @@ compactControlReadability
Default: ``false``
-When ``compactControlReadability`` is ``true``, if/else and try/catch/finally control structures will be formatted using
-Compact Control Readability style:
+When ``compactControlReadability`` is ``true``, ``if``/``else`` and
+``try``/``catch``/``finally`` control structures will be formatted
+using `Compact Control Readability`_ style:
+
+.. _Compact Control Readability: http://en.wikipedia.org/wiki/Indent_style#Compact_Control_Readability_style
if (x == y) {
foo()
@@ -272,6 +275,15 @@ Compact Control Readability style:
baz()
}
+ try {
+ foo()
+ }
+ catch {
+ case _ => bar()
+ }
+ finally {
+ baz()
+ }
compactStringConcatenation
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -66,9 +66,9 @@ object MojoFormatter {
.setPreference(AlignParameters, alignParameters)
.setPreference(AlignSingleLineCaseStatements, alignSingleLineCaseStatements)
.setPreference(AlignSingleLineCaseStatements.MaxArrowIndent, alignSingleLineCaseStatements_maxArrowIndent)
- .setPreference(DoubleIndentClassDeclaration, doubleIndentClassDeclaration)
.setPreference(CompactControlReadability, compactControlReadability)
.setPreference(CompactStringConcatenation, compactStringConcatenation)
+ .setPreference(DoubleIndentClassDeclaration, doubleIndentClassDeclaration)
.setPreference(FormatXml, formatXml)
.setPreference(IndentLocalDefs, indentLocalDefs)
.setPreference(IndentPackageBlocks, indentPackageBlocks)
@@ -293,13 +293,11 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
formatResult ++= format(body)(bodyFormatterState)
// TODO: Simplified version of elseClause formatting
- val cuddledBraces = formattingPreferences(CompactControlReadability)
for (CatchClause(catchToken, catchBlockOrExpr) catchClauseOption) {
- if (hiddenPredecessors(catchToken).containsNewline && !(bodyIsABlock && containsNewline(body)))
+ if (formattingPreferences(CompactControlReadability) && bodyIsABlock && containsNewline(body))
formatResult = formatResult.before(catchToken, formatterState.currentIndentLevelInstruction)
- else if(cuddledBraces){
+ else if (hiddenPredecessors(catchToken).containsNewline && !(bodyIsABlock && containsNewline(body)))
formatResult = formatResult.before(catchToken, formatterState.currentIndentLevelInstruction)
- }
catchBlockOrExpr match {
case Left(catchBlock)
@@ -320,11 +318,10 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
val previousIsMultilineBracesBlock = catchClauseOption.isEmpty && bodyIsABlock && containsNewline(body) ||
cond(catchClauseOption) { case Some(CatchClause(_, Left(catchBlock))) containsNewline(catchBlock) }
- if (hiddenPredecessors(finallyToken).containsNewline && !previousIsMultilineBracesBlock)
+ if (formattingPreferences(CompactControlReadability) && previousIsMultilineBracesBlock)
formatResult = formatResult.before(finallyToken, formatterState.currentIndentLevelInstruction)
- else if(cuddledBraces){
+ else if (hiddenPredecessors(finallyToken).containsNewline && !previousIsMultilineBracesBlock)
formatResult = formatResult.before(finallyToken, formatterState.currentIndentLevelInstruction)
- }
val indentFinallyBody =
if (isBlockExpr(finallyBody))
@@ -376,11 +373,9 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
formatResult ++= format(body)(bodyFormatterState)
// TODO: take into account pre-Else semi
- val cuddledBraces = formattingPreferences(CompactControlReadability)
for (ElseClause(elseSemiOpt, elseToken, elseBody) elseClauseOption) {
- if(cuddledBraces){
+ if (formattingPreferences(CompactControlReadability) && bodyIsABlock && containsNewline(body))
formatResult = formatResult.before(elseToken, formatterState.currentIndentLevelInstruction)
- }
else if (bodyIsABlock && containsNewline(body))
formatResult = formatResult.before(elseToken, CompactEnsuringGap)
else if (hiddenPredecessors(elseToken).containsNewline || containsNewline(body) || (indentBody && (hiddenPredecessors(elseBody.firstToken).containsNewline || isBlockExpr(elseBody) || isIfExpr(elseBody))))
@@ -10,8 +10,6 @@ case class FormatterState(
private val nextIndentLevel = indentLevel + 1
- private val previousIndentLevel = if(indentLevel > 0) indentLevel - 1 else indentLevel
-
def indent: FormatterState = indent(1)
def indent(n: Int): FormatterState = copy(indentLevel = indentLevel + n)
@@ -22,8 +20,6 @@ case class FormatterState(
def currentIndentLevelInstruction = EnsureNewlineAndIndent(indentLevel, relativeTo = indentRelativeToTokenOption)
- def previousIndentLevelInstruction = EnsureNewlineAndIndent(previousIndentLevel, relativeTo = indentRelativeToTokenOption)
-
def indentForExpressionBreak = indent.copy(expressionBreakHappened = true)
def indentForExpressionBreakIfNeeded = if (expressionBreakHappened) this else indent.copy(expressionBreakHappened = true)
@@ -81,7 +81,7 @@ abstract class ScalaFormatter extends HasFormattingPreferences with TypeFormatte
EnsureNewlineAndIndent(0) // Adjustment for end of input when using non-zero initial indent
else
basicFormattingInstruction
- val nextTokenUnindents = nextTokenOption exists { _.getType == RBRACE } //TODO: perhaps could just touch here to make newline unindenting?
+ val nextTokenUnindents = nextTokenOption exists { _.getType == RBRACE }
val includeBufferBeforeNextToken = nextTokenOption exists { nextToken
!printableFormattingInstruction(Some(token), nextToken).isInstanceOf[EnsureNewlineAndIndent]
}
@@ -96,7 +96,7 @@ abstract class ScalaFormatter extends HasFormattingPreferences with TypeFormatte
builder.append(token.getText)
} else {
val formattingInstruction = printableFormattingInstruction(previousTokenOption, token)
- val nextTokenUnindents = token.getType == RBRACE //TODO: same here, could alter just this part too to make newline unindenting
+ val nextTokenUnindents = token.getType == RBRACE
val includeBufferBeforeNextToken = true // <-- i.e. current token
val hiddenTokens = hiddenPredecessors(token)
val positionHintOption = if (hiddenTokens.isEmpty) Some(token.startIndex) else None
@@ -5,42 +5,39 @@ import scalariform.formatter.preferences._
import scalariform.parser._
import scalariform.formatter._
+// format: OFF
+class CompactControlReadabilityTest extends AbstractExpressionFormatterTest {
-class CompactControlReadabilityTest extends AbstractExpressionFormatterTest{
-
- override val debug = false
-
- string2FormatTest(
- """if(a){
- |foo
- |} else {
- |bar }""")(FormattingPreferences().setPreference(CompactControlReadability, true)) ==>
+ implicit val formattingPreferences = FormattingPreferences.setPreference(CompactControlReadability, true)
+
+ """if(a){
+ |foo
+ |} else {
+ |bar }""" ==>
"""if (a) {
| foo
|}
|else {
| bar
|}"""
- string2FormatTest(
- """if(a){
- |foo
- |}
- |else {
- |bar }""")(FormattingPreferences().setPreference(CompactControlReadability, true)) ==>
+ """if(a){
+ |foo
+ |}
+ |else {
+ |bar }""" ==>
"""if (a) {
| foo
|}
|else {
| bar
|}"""
- string2FormatTest(
- """if(a){
- |foo
- |} else {
- |
- |bar }""")(FormattingPreferences().setPreference(CompactControlReadability, true)) ==>
+ """if(a){
+ |foo
+ |} else {
+ |
+ |bar }""" ==>
"""if (a) {
| foo
|}
@@ -49,58 +46,61 @@ class CompactControlReadabilityTest extends AbstractExpressionFormatterTest{
| bar
|}"""
- string2FormatTest(
- """try{
- | foo
- |} catch {
- | bar
- |}""")(FormattingPreferences().setPreference(CompactControlReadability, true)) ==>
+ """try{
+ | foo
+ |} catch {
+ | bar
+ |}""" ==>
"""try {
| foo
|}
|catch {
| bar
|}"""
- string2FormatTest(
- """try{
- | foo
- |} finally {
- | bar
- |}""")(FormattingPreferences().setPreference(CompactControlReadability, true)) ==>
+ """try{
+ | foo
+ |} finally {
+ | bar
+ |}""" ==>
"""try {
| foo
|}
|finally {
| bar
|}"""
- string2FormatTest(
- """try{
- | foo
- |}
- |finally {
- | bar
- |}""")(FormattingPreferences().setPreference(CompactControlReadability, true)) ==>
+ """try{
+ | foo
+ |}
+ |finally {
+ | bar
+ |}""" ==>
"""try {
| foo
|}
|finally {
| bar
|}"""
- string2FormatTest(
- """try{
- | foo
- |} finally {
- |
- | bar
- |}""")(FormattingPreferences().setPreference(CompactControlReadability, true)) ==>
+ """try{
+ | foo
+ |} finally {
+ |
+ | bar
+ |}""" ==>
"""try {
| foo
|}
|finally {
|
| bar
|}"""
+
+ "if (y > 0) positive else if (y < 0) negative else zero" ==> "if (y > 0) positive else if (y < 0) negative else zero"
+
+ "try x catch y finally z" ==> "try x catch y finally z"
+
+
+
}
@@ -1,15 +1,11 @@
package scalariform.formatter
-import preferences.FormattingPreferences._
import scalariform.parser._
import scalariform.formatter._
+// format: OFF
class IfExprFormatterTest extends AbstractExpressionFormatterTest {
- override val debug = false
-
- // format: OFF
-
"if(x>y)(x)else(y)" ==> "if (x > y) (x) else (y)"
"if (true) 3 else 4" ==> "if (true) 3 else 4"
@@ -352,5 +348,7 @@ class IfExprFormatterTest extends AbstractExpressionFormatterTest {
| d
|} else e"""
+ override val debug = false
+
}

0 comments on commit 39c2e4c

Please sign in to comment.