Skip to content

Commit

Permalink
resolves scala-ide#269
Browse files Browse the repository at this point in the history
  • Loading branch information
odisseus committed Feb 23, 2018
1 parent 5cda34e commit 534016a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
} yield head.isInstanceOf[AnonymousFunction]).getOrElse(false)

val (instruction, subStatState) =
if (hasNestedAnonymousFunction(subStatSeq))
if (formattingPreferences(NewlinesAtNestedAnonymousFunctions) == Prevent && hasNestedAnonymousFunction(subStatSeq))
(CompactEnsuringGap, indentedState.indent(-1))
else if (hiddenPredecessors(params.head.firstToken).containsNewline)
(indentedInstruction, indentedState.indent)
Expand All @@ -834,7 +834,7 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
formatResult ++= format(params)
for (firstToken subStatSeq.firstTokenOption) {
val instruction =
if (hasNestedAnonymousFunction(subStatSeq))
if (formattingPreferences(NewlinesAtNestedAnonymousFunctions) == Prevent && hasNestedAnonymousFunction(subStatSeq))
CompactEnsuringGap
else if (hiddenPredecessors(firstToken).containsNewline || containsNewline(subStatSeq))
statFormatterState(subStatSeq.firstStatOpt)(subStatState).currentIndentLevelInstruction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ object AllPreferences {
AllowParamGroupsOnNewlines, CompactControlReadability, CompactStringConcatenation, DanglingCloseParenthesis,
DoubleIndentClassDeclaration, DoubleIndentConstructorArguments, DoubleIndentMethodDeclaration, FirstArgumentOnNewline,
FirstParameterOnNewline, FormatXml, IndentLocalDefs, IndentPackageBlocks, IndentSpaces, IndentWithTabs,
MultilineScaladocCommentsStartOnFirstLine, NewlineAtEndOfFile, PlaceScaladocAsterisksBeneathSecondAsterisk,
PreserveSpaceBeforeArguments, RewriteArrowSymbols, SingleCasePatternOnNewline, SpaceBeforeColon,
SpaceBeforeContextColon, SpaceInsideBrackets, SpaceInsideParentheses, SpacesAroundMultiImports, SpacesWithinPatternBinders
MultilineScaladocCommentsStartOnFirstLine, NewlineAtEndOfFile, NewlinesAtNestedAnonymousFunctions,
PlaceScaladocAsterisksBeneathSecondAsterisk, PreserveSpaceBeforeArguments, RewriteArrowSymbols, SingleCasePatternOnNewline,
SpaceBeforeColon, SpaceBeforeContextColon, SpaceInsideBrackets, SpaceInsideParentheses, SpacesAroundMultiImports,
SpacesWithinPatternBinders
)

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

case object NewlinesAtNestedAnonymousFunctions extends IntentPreferenceDescriptor {
val key = "newlinesAtNestedAnonymousFunctions"
val description = "When there are nested anonymous functions, add a newline after arrow at each nesting level"
val defaultValue: Prevent.type = Prevent
}

case object PlaceScaladocAsterisksBeneathSecondAsterisk extends BooleanPreferenceDescriptor {
val key = "placeScaladocAsterisksBeneathSecondAsterisk"
val description = "Place Scaladoc asterisks beneath the second asterisk in the opening '/**', as opposed to the first"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package scalariform.formatter

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

// format: OFF
class NestedAnonymousFunctionsTest extends AbstractFormatterTest {

implicit val formattingPreferences = FormattingPreferences
.setPreference(NewlinesAtNestedAnonymousFunctions, Force)
.setPreference(AlignParameters, true)
.setPreference(AlignSingleLineCaseStatements, true)
.setPreference(AlignSingleLineCaseStatements.MaxArrowIndent, 100)
.setPreference(AlignArguments, true)

"""def bar: Int => Int => String = {
| x: Int =>
| y: Int =>
| "x"
|}""" ==>
"""def bar: Int => Int => String = {
| x: Int =>
| y: Int =>
| "x"
|}"""

// format: ON

override val debug = false

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

type Result = FullDefOrDcl

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

}

0 comments on commit 534016a

Please sign in to comment.