Skip to content

Commit

Permalink
Add IndentPackageBlocks formatting preference
Browse files Browse the repository at this point in the history
  • Loading branch information
mdr committed Oct 18, 2010
1 parent 8f36450 commit 43f1222
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 17 deletions.
3 changes: 2 additions & 1 deletion 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
Expand Down
24 changes: 23 additions & 1 deletion README.rst
Expand Up @@ -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 ←
Expand Down Expand Up @@ -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``
Expand Down
24 changes: 23 additions & 1 deletion docs/source/README.rst
Expand Up @@ -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 ←
Expand Down Expand Up @@ -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``
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

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

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

}
Expand Down
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Up @@ -2,6 +2,7 @@ package scalariform.formatter

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

// format: OFF
class PackageFormatterTest extends AbstractFormatterTest {
Expand Down Expand Up @@ -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
|}
|}"""

}


}

0 comments on commit 43f1222

Please sign in to comment.