Skip to content

Commit

Permalink
Merge branch 'compact-control-readability-style'
Browse files Browse the repository at this point in the history
  • Loading branch information
mdr committed Jul 18, 2011
2 parents 7d7d264 + 39c2e4c commit fc777eb
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
32 changes: 32 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Usage::
Preferences:
[+|-]alignParameters Enable/disable Align parameters on different lines in the same column
[+|-]alignSingleLineCaseStatements Enable/disable Align the arrows of consecutive single-line case statements
[+|-]compactControlReadability Enable/disable Format control structures using Compact Control Readability style
[+|-]compactStringConcatenation Enable/disable Omit spaces when formatting a '+' operator on String literals
[+|-]doubleIndentClassDeclaration Enable/disable Double indent either a class's parameters or its inheritance list
[+|-]formatXml Enable/disable Format XML literals
Expand Down Expand Up @@ -253,6 +254,37 @@ e.g.::
case ccc => 2
}

compactControlReadability
~~~~~~~~~~~~~~~~~~~~~~~~~

Default: ``false``

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()
}
else if (y == z) {
bar()
}
else {
baz()
}

try {
foo()
}
catch {
case _ => bar()
}
finally {
baz()
}

compactStringConcatenation
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 2 additions & 2 deletions project/plugins/project/build.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#Project properties
#Fri May 27 11:12:53 BST 2011
#Project properties
#Tue Jun 28 09:54:49 EDT 2011
plugin.uptodate=true
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public class ScalariformMojo extends AbstractMojo {
*/
protected int alignSingleLineCaseStatements_maxArrowIndent;

/**
* @parameter default-value=false
*/
protected boolean compactControlReadability;

/**
* @parameter default-value=false
*/
Expand Down Expand Up @@ -116,6 +121,7 @@ public void execute() throws MojoExecutionException {
alignParameters,
alignSingleLineCaseStatements,
alignSingleLineCaseStatements_maxArrowIndent,
compactControlReadability,
compactStringConcatenation,
doubleIndentClassDeclaration,
formatXml,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ object MojoFormatter {
alignParameters: Boolean,
alignSingleLineCaseStatements: Boolean,
alignSingleLineCaseStatements_maxArrowIndent: Int,
compactControlReadability: Boolean,
compactStringConcatenation: Boolean,
doubleIndentClassDeclaration: Boolean,
formatXml: Boolean,
Expand All @@ -65,8 +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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,11 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi

// TODO: Simplified version of elseClause formatting
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 (hiddenPredecessors(catchToken).containsNewline && !(bodyIsABlock && containsNewline(body)))
formatResult = formatResult.before(catchToken, formatterState.currentIndentLevelInstruction)

catchBlockOrExpr match {
case Left(catchBlock)
formatResult = formatResult.before(catchBlock.firstToken, CompactEnsuringGap)
Expand All @@ -315,7 +318,9 @@ 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 (hiddenPredecessors(finallyToken).containsNewline && !previousIsMultilineBracesBlock)
formatResult = formatResult.before(finallyToken, formatterState.currentIndentLevelInstruction)

val indentFinallyBody =
Expand Down Expand Up @@ -369,8 +374,9 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi

// TODO: take into account pre-Else semi
for (ElseClause(elseSemiOpt, elseToken, elseBody) elseClauseOption) {

if (bodyIsABlock && containsNewline(body))
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))))
formatResult = formatResult.before(elseToken, formatterState.currentIndentLevelInstruction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ case object CompactPreservingGap extends IntertokenFormatInstruction
case class EnsureNewlineAndIndent(indentLevel: Int, relativeTo: Option[Token] = None) extends IntertokenFormatInstruction

/** Places the token at spaces number of spaces after the indent level, padding with spaces if necessary */
case class PlaceAtColumn(indentLevel: Int, spaces: Int) extends IntertokenFormatInstruction
case class PlaceAtColumn(indentLevel: Int, spaces: Int) extends IntertokenFormatInstruction
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ object AllPreferences {
val preferences: List[PreferenceDescriptor[_]] = List(RewriteArrowSymbols, IndentSpaces, SpaceBeforeColon, CompactStringConcatenation,
PreserveSpaceBeforeArguments, AlignParameters, DoubleIndentClassDeclaration, FormatXml, IndentPackageBlocks,
AlignSingleLineCaseStatements, AlignSingleLineCaseStatements.MaxArrowIndent, IndentLocalDefs, PreserveDanglingCloseParenthesis,
SpaceInsideParentheses, SpaceInsideBrackets, SpacesWithinPatternBinders, MultilineScaladocCommentsStartOnFirstLine, IndentWithTabs)
SpaceInsideParentheses, SpaceInsideBrackets, SpacesWithinPatternBinders, MultilineScaladocCommentsStartOnFirstLine, IndentWithTabs,
CompactControlReadability)

val preferencesByKey: Map[String, PreferenceDescriptor[_]] = {
var map: Map[String, PreferenceDescriptor[_]] = Map()
Expand Down Expand Up @@ -183,4 +184,10 @@ case object IndentWithTabs extends BooleanPreferenceDescriptor {
val key = "indentWithTabs"
val description = "Use a tab character for indentation"
val defaultValue = false
}

case object CompactControlReadability extends BooleanPreferenceDescriptor {
val key = "compactControlReadability"
val description = "Enable Compact Control Readability style"
val defaultValue = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package scalariform.formatter

import preferences.FormattingPreferences._
import scalariform.formatter.preferences._
import scalariform.parser._
import scalariform.formatter._

// format: OFF
class CompactControlReadabilityTest extends AbstractExpressionFormatterTest {

implicit val formattingPreferences = FormattingPreferences.setPreference(CompactControlReadability, true)

"""if(a){
|foo
|} else {
|bar }""" ==>
"""if (a) {
| foo
|}
|else {
| bar
|}"""

"""if(a){
|foo
|}
|else {
|bar }""" ==>
"""if (a) {
| foo
|}
|else {
| bar
|}"""

"""if(a){
|foo
|} else {
|
|bar }""" ==>
"""if (a) {
| foo
|}
|else {
|
| bar
|}"""

"""try{
| foo
|} catch {
| bar
|}""" ==>
"""try {
| foo
|}
|catch {
| bar
|}"""

"""try{
| foo
|} finally {
| bar
|}""" ==>
"""try {
| foo
|}
|finally {
| bar
|}"""

"""try{
| foo
|}
|finally {
| bar
|}""" ==>
"""try {
| foo
|}
|finally {
| bar
|}"""

"""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"



}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package scalariform.formatter
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"
Expand Down Expand Up @@ -351,5 +348,7 @@ class IfExprFormatterTest extends AbstractExpressionFormatterTest {
| d
|} else e"""

override val debug = false

}

0 comments on commit fc777eb

Please sign in to comment.