From 43f1222a031e6f6fbf863ed6e4ef4b02d500a2d9 Mon Sep 17 00:00:00 2001 From: Matt Russell Date: Mon, 18 Oct 2010 19:19:44 +0100 Subject: [PATCH] Add IndentPackageBlocks formatting preference --- CHANGELOG | 3 ++- README.rst | 24 ++++++++++++++++++- docs/source/README.rst | 24 ++++++++++++++++++- project/plugins/project/build.properties | 2 +- .../scalariform/formatter/ExprFormatter.scala | 23 +++++++++++------- .../formatter/TemplateFormatter.scala | 4 ++-- .../scalariform/formatter/TypeFormatter.scala | 2 +- .../preferences/PreferenceDescriptor.scala | 7 +++++- .../formatter/PackageFormatterTest.scala | 19 +++++++++++++++ 9 files changed, 91 insertions(+), 17 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3e8d0943..20018057 100644 --- a/CHANGELOG +++ b/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 diff --git a/README.rst b/README.rst index 13a62b80..57113b89 100644 --- a/README.rst +++ b/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`` diff --git a/docs/source/README.rst b/docs/source/README.rst index 13a62b80..57113b89 100644 --- a/docs/source/README.rst +++ b/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`` diff --git a/project/plugins/project/build.properties b/project/plugins/project/build.properties index 1010160a..18f74b23 100644 --- a/project/plugins/project/build.properties +++ b/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 diff --git a/scalariform/src/main/scala/scalariform/formatter/ExprFormatter.scala b/scalariform/src/main/scala/scalariform/formatter/ExprFormatter.scala index b0560d2b..81a7e687 100644 --- a/scalariform/src/main/scala/scalariform/formatter/ExprFormatter.scala +++ b/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 } diff --git a/scalariform/src/main/scala/scalariform/formatter/TemplateFormatter.scala b/scalariform/src/main/scala/scalariform/formatter/TemplateFormatter.scala index 2dd1c688..8e1f1d70 100644 --- a/scalariform/src/main/scala/scalariform/formatter/TemplateFormatter.scala +++ b/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 diff --git a/scalariform/src/main/scala/scalariform/formatter/TypeFormatter.scala b/scalariform/src/main/scala/scalariform/formatter/TypeFormatter.scala index 3e6c3b9f..67883968 100644 --- a/scalariform/src/main/scala/scalariform/formatter/TypeFormatter.scala +++ b/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) } } diff --git a/scalariform/src/main/scala/scalariform/formatter/preferences/PreferenceDescriptor.scala b/scalariform/src/main/scala/scalariform/formatter/preferences/PreferenceDescriptor.scala index 4882340e..8ef08b03 100644 --- a/scalariform/src/main/scala/scalariform/formatter/preferences/PreferenceDescriptor.scala +++ b/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 +} diff --git a/scalariform/src/test/scala/scalariform/formatter/PackageFormatterTest.scala b/scalariform/src/test/scala/scalariform/formatter/PackageFormatterTest.scala index e6ab907e..db762c98 100644 --- a/scalariform/src/test/scala/scalariform/formatter/PackageFormatterTest.scala +++ b/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 + |} + |}""" + + } + + }