Skip to content

Commit

Permalink
Merge pull request scala-ide#189 from jkinkead/double_indent
Browse files Browse the repository at this point in the history
Add an option to doubly-indent method parameters.
  • Loading branch information
kiritsuku committed Feb 20, 2016
2 parents 401b4a9 + 4ddd98f commit 1ba5a06
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 3 deletions.
23 changes: 23 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,29 @@ Or:
.. _recommended: http://docs.scala-lang.org/style/declarations.html#classes

doubleIndentMethodDeclaration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Default: ``false``

With this set to ``true``, method declarations will have an extra indentation
added to their parameter list, if it spans multiple lines.
This provides a visual distinction from the method body. For example::

def longMethodNameIsLong(paramOneNameIsLong: String, paramTwo: String,
paramThreeNameIsReallyLong): Unit = {
val startOfMethod = ...
}

Or::

def longMethodNameIsLong(
paramOneNameIsLong: String,
paramTwoNameIsLong: String,
paramThreeNameIsLong): Unit = {
val startOfMethod = ...
}

formatXml
~~~~~~~~~

Expand Down
3 changes: 2 additions & 1 deletion formatterPreferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ indentPackageBlocks=true
formatXml=true
preserveSpaceBeforeArguments=false
doubleIndentClassDeclaration=false
doubleIndentMethodDeclaration=false
rewriteArrowSymbols=false
alignSingleLineCaseStatements=true
alignSingleLineCaseStatements.maxArrowIndent=40
Expand All @@ -16,4 +17,4 @@ preserveDanglingCloseParenthesis=false
indentSpaces=2
indentLocalDefs=false
spacesWithinPatternBinders=true
spacesAroundMultiImports=true
spacesAroundMultiImports=true
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,8 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
val FunDefOrDcl(_, _, typeParamClauseOpt, paramClauses, returnTypeOpt, funBodyOpt, _) = funDefOrDcl
for (typeParamClause typeParamClauseOpt)
formatResult ++= format(typeParamClause.contents)
formatResult ++= formatParamClauses(paramClauses)
val doubleIndentParams = formattingPreferences(DoubleIndentMethodDeclaration)
formatResult ++= formatParamClauses(paramClauses, doubleIndentParams)
for ((colon, type_) returnTypeOpt)
formatResult ++= format(type_)
for (funBody funBodyOpt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ object AllPreferences {
PreserveSpaceBeforeArguments, AlignParameters, AlignArguments, DoubleIndentClassDeclaration, FormatXml, IndentPackageBlocks,
AlignSingleLineCaseStatements, AlignSingleLineCaseStatements.MaxArrowIndent, IndentLocalDefs, PreserveDanglingCloseParenthesis, DanglingCloseParenthesis,
SpaceInsideParentheses, SpaceInsideBrackets, SpacesWithinPatternBinders, MultilineScaladocCommentsStartOnFirstLine, IndentWithTabs,
CompactControlReadability, PlaceScaladocAsterisksBeneathSecondAsterisk, SpacesAroundMultiImports, NewlineAtEndOfFile
CompactControlReadability, PlaceScaladocAsterisksBeneathSecondAsterisk, DoubleIndentMethodDeclaration, SpacesAroundMultiImports,
NewlineAtEndOfFile
)

val preferencesByKey: Map[String, PreferenceDescriptor[_]] =
Expand Down Expand Up @@ -152,6 +153,12 @@ case object DoubleIndentClassDeclaration extends BooleanPreferenceDescriptor {
val defaultValue = false
}

case object DoubleIndentMethodDeclaration extends BooleanPreferenceDescriptor {
val key = "doubleIndentMethodDeclaration"
val description = "Double indent a method's parameters, if they span multiple lines"
val defaultValue = false
}

case object FormatXml extends BooleanPreferenceDescriptor {
val key = "formatXml"
val description = "Format XML literals"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package scalariform.formatter

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

// format: OFF
class DoubleIndentMethodFormatterTest extends AbstractFormatterTest {

override val debug = false

{

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

// Test basic formatting for regressions.
"def foo" ==> "def foo"

"def foo=42" ==> "def foo = 42"

"def foo()" ==> "def foo()"

"def foo(n:Int)" ==> "def foo(n: Int)"

"""def foo(n: Int
|)""" ==>
"def foo(n: Int)"

"def foo( n:Int,m:String )" ==> "def foo(n: Int, m: String)"

"def foo{doStuff()}" ==> "def foo { doStuff() }"

"def foo(x_ : Int)" ==> "def foo(x_ : Int)"

"""def a
|: String""" ==>
"""def a: String"""

"""def a()
|{
|b
| }""" ==>
"""def a() {
| b
|}"""

"""def a[B,
|C]
|()""" ==>
"""def a[B, C]()"""

// Verify that continued parameters are maintained & doubly-indented.
"""def foo(
| a: String)""" ==>
"""def foo(
| a: String
|)"""

// TODO: Broken until https://github.com/scala-ide/scalariform/issues/187 is fixed.
"""def foo(a: String,
| b: String)""" =/=>
"""def foo(
| a: String,
| b: String
|)"""

// TODO: Broken until https://github.com/scala-ide/scalariform/issues/187 is fixed.
"""def foo(a: String,
| b: String)""" =/=>
"""def foo(
| a: String,
| b: String
|)"""

"""def foo(a: String, b: String,
| c: String)""" ==>
"""def foo(a: String, b: String,
| c: String)"""

"""def foo(
| a: String,
| b: String)""" ==>
"""def foo(
| a: String,
| b: String
|)"""
}

def parse(parser: ScalaParser) = parser.nonLocalDefOrDcl()

type Result = FullDefOrDcl

def format(formatter: ScalaFormatter, result: Result) = formatter.format(result)(FormatterState(indentLevel = 0))
}

0 comments on commit 1ba5a06

Please sign in to comment.