Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add IndentPackageBlocks formatting preference

  • Loading branch information...
commit 43f1222a031e6f6fbf863ed6e4ef4b02d500a2d9 1 parent 8f36450
@mdr authored
View
3  CHANGELOG
@@ -1,6 +1,7 @@
0.0.7 (..)
-* Rewrite parser -> formatter now ~65% faster
+* Rewrite parser; formatter is now ~60% faster
+* Add IndentPackageBlocks formatting preference
* Allow newline before self type declaration
* FIX: avoid abutting @ and an operator, otherwise it merges into a single identifer
* FIX: formatting for newline between private[foo] and trait/class/def etc
View
24 README.rst
@@ -103,6 +103,7 @@ Usage::
[+|-]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
+ [+|-]indentPackageBlocks Enable/disable Indent package blocks
-indentSpaces=[1-10] Set Number of spaces to use for indentation
[+|-]preserveSpaceBeforeArguments Enable/disable Preserve a space before a parenthesis argument
[+|-]rewriteArrowSymbols Enable/disable Replace arrow tokens with unicode equivalents: => with ⇒, and <- with ←
@@ -226,7 +227,28 @@ Default: ``true``
Format embedded XML literals; if ``false`` they will be left untouched.
-indentSpaces
+indentPackageBlocks
+~~~~~~~~~~~~~~~~~~~
+
+Default: ``true``
+
+Whether to indent package blocks. For example, if ``true``::
+
+ package foo {
+ package bar {
+ class Baz
+ }
+ }
+
+Else if ``false``::
+
+ package foo {
+ package bar {
+ class Baz
+ }
+ }
+
+indentSpaces
~~~~~~~~~~~~
Default: ``2``
View
24 docs/source/README.rst
@@ -103,6 +103,7 @@ Usage::
[+|-]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
+ [+|-]indentPackageBlocks Enable/disable Indent package blocks
-indentSpaces=[1-10] Set Number of spaces to use for indentation
[+|-]preserveSpaceBeforeArguments Enable/disable Preserve a space before a parenthesis argument
[+|-]rewriteArrowSymbols Enable/disable Replace arrow tokens with unicode equivalents: => with ⇒, and <- with ←
@@ -226,7 +227,28 @@ Default: ``true``
Format embedded XML literals; if ``false`` they will be left untouched.
-indentSpaces
+indentPackageBlocks
+~~~~~~~~~~~~~~~~~~~
+
+Default: ``true``
+
+Whether to indent package blocks. For example, if ``true``::
+
+ package foo {
+ package bar {
+ class Baz
+ }
+ }
+
+Else if ``false``::
+
+ package foo {
+ package bar {
+ class Baz
+ }
+ }
+
+indentSpaces
~~~~~~~~~~~~
Default: ``2``
View
2  project/plugins/project/build.properties
@@ -1,3 +1,3 @@
#Project properties
-#Sat Oct 09 12:13:48 BST 2010
+#Mon Oct 18 19:02:59 BST 2010
plugin.uptodate=true
View
23 scalariform/src/main/scala/scalariform/formatter/ExprFormatter.scala
@@ -97,7 +97,7 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
case ifExpr: IfExpr format(ifExpr)
case whileExpr: WhileExpr format(whileExpr)
case doExpr: DoExpr format(doExpr)
- case blockExpr: BlockExpr format(blockExpr)
+ case blockExpr: BlockExpr format(blockExpr, indent = true)
case forExpr: ForExpr format(forExpr)
case tryExpr: TryExpr format(tryExpr)
case template: Template format(template)
@@ -433,16 +433,21 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
private def isBlockExpr(expr: Expr) = expr.contents.size == 1 && expr.contents(0).isInstanceOf[BlockExpr]
- def format(blockExpr: BlockExpr)(implicit formatterState: FormatterState): FormatResult = {
+ def format(blockExpr: BlockExpr, indent: Boolean)(implicit formatterState: FormatterState): FormatResult = {
val BlockExpr(lbrace: Token, caseClausesOrStatSeq: Either[CaseClauses, StatSeq], rbrace: Token) = blockExpr
var formatResult: FormatResult = NoFormatResult
val singleLineBlock = !containsNewline(blockExpr)
val newFormatterState = formatterState.copy(inSingleLineBlock = singleLineBlock)
+ val (indentedInstruction, indentedState) =
+ if (indent)
+ (newFormatterState.nextIndentLevelInstruction, newFormatterState.indent)
+ else
+ (newFormatterState.currentIndentLevelInstruction, newFormatterState)
caseClausesOrStatSeq match {
case Left(caseClauses) // TODO: Duplication
if (!singleLineBlock) {
- formatResult = formatResult.before(caseClauses.firstToken, newFormatterState.nextIndentLevelInstruction)
- formatResult ++= format(caseClauses)(newFormatterState.indent)
+ formatResult = formatResult.before(caseClauses.firstToken, indentedInstruction)
+ formatResult ++= format(caseClauses)(indentedState)
formatResult = formatResult.before(rbrace, newFormatterState.currentIndentLevelInstruction)
} else
formatResult ++= format(caseClauses)(newFormatterState)
@@ -454,18 +459,18 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
case Some(Expr(List(anonFn@AnonymousFunction(params, arrowToken, body))))
formatResult = formatResult.before(statSeq.firstToken, CompactEnsuringGap)
for (firstToken body.headOption flatMap { _.firstTokenOption })
- formatResult = formatResult.before(firstToken, newFormatterState.nextIndentLevelInstruction)
+ formatResult = formatResult.before(firstToken, indentedInstruction)
formatResult ++= format(params)
- formatResult ++= format(body)(newFormatterState.indent)
+ formatResult ++= format(body)(indentedState)
case _
val instruction = statSeq.selfReferenceOpt match {
case Some((selfReference, arrow)) if !hiddenPredecessors(selfReference.firstToken).containsNewline
CompactEnsuringGap
case _
- newFormatterState.nextIndentLevelInstruction
+ indentedInstruction
}
formatResult = formatResult.before(statSeq.firstToken, instruction)
- formatResult ++= format(statSeq)(newFormatterState.indent)
+ formatResult ++= format(statSeq)(indentedState)
}
}
formatResult = formatResult.before(rbrace, newFormatterState.currentIndentLevelInstruction)
@@ -561,7 +566,7 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
}
val dummyBlock = BlockExpr(lbrace, Right(topStats), rbrace)
- formatResult ++= format(dummyBlock)
+ formatResult ++= format(dummyBlock, indent = formattingPreferences(IndentPackageBlocks))
formatResult
}
View
4 scalariform/src/main/scala/scalariform/formatter/TemplateFormatter.scala
@@ -80,7 +80,7 @@ trait TemplateFormatter { self: HasFormattingPreferences with AnnotationFormatte
}
val dummyBlock = BlockExpr(lbrace, Right(statSeq), rbrace)
- formatResult ++= format(dummyBlock)
+ formatResult ++= format(dummyBlock, indent = true)
formatResult
}
@@ -104,7 +104,7 @@ trait TemplateFormatter { self: HasFormattingPreferences with AnnotationFormatte
}
val dummyBlock = BlockExpr(lbrace, Right(statSeq), rbrace)
- formatResult ++= format(dummyBlock)
+ formatResult ++= format(dummyBlock, indent = true)
}
formatResult
View
2  scalariform/src/main/scala/scalariform/formatter/TypeFormatter.scala
@@ -52,7 +52,7 @@ trait TypeFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
private def format(refinement: Refinement)(implicit formatterState: FormatterState): FormatResult = {
val Refinement(lbrace: Token, statSeq: StatSeq, rbrace: Token) = refinement
val dummyBlock = BlockExpr(lbrace, Right(statSeq), rbrace)
- format(dummyBlock)
+ format(dummyBlock, indent = true)
}
}
View
7 scalariform/src/main/scala/scalariform/formatter/preferences/PreferenceDescriptor.scala
@@ -55,7 +55,7 @@ abstract trait BooleanPreferenceDescriptor extends PreferenceDescriptor[Boolean]
object AllPreferences {
val preferences: List[PreferenceDescriptor[_]] = List(RewriteArrowSymbols, IndentSpaces, SpaceBeforeColon, CompactStringConcatenation,
- PreserveSpaceBeforeArguments, AlignParameters, DoubleIndentClassDeclaration, FormatXml)
+ PreserveSpaceBeforeArguments, AlignParameters, DoubleIndentClassDeclaration, FormatXml, IndentPackageBlocks)
val preferencesByKey: Map[String, PreferenceDescriptor[_]] = {
var map: Map[String, PreferenceDescriptor[_]] = Map()
@@ -115,3 +115,8 @@ case object FormatXml extends BooleanPreferenceDescriptor {
val defaultValue = true
}
+case object IndentPackageBlocks extends BooleanPreferenceDescriptor {
+ val key = "indentPackageBlocks"
+ val description = "Indent package blocks"
+ val defaultValue = true
+}
View
19 scalariform/src/test/scala/scalariform/formatter/PackageFormatterTest.scala
@@ -2,6 +2,7 @@ package scalariform.formatter
import scalariform.parser._
import scalariform.formatter._
+import scalariform.formatter.preferences._
// format: OFF
class PackageFormatterTest extends AbstractFormatterTest {
@@ -40,4 +41,22 @@ class PackageFormatterTest extends AbstractFormatterTest {
|{}""" ==>
"""package a {}"""
+ {
+
+ implicit val formattingPreferences = FormattingPreferences.setPreference(IndentPackageBlocks, false)
+
+ """package foo {
+ |package bar {
+ |class Baz
+ |}
+ |}""" ==>
+ """package foo {
+ |package bar {
+ |class Baz
+ |}
+ |}"""
+
+ }
+
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.