Skip to content

Commit

Permalink
Fixed scala-ide#269.
Browse files Browse the repository at this point in the history
  • Loading branch information
odisseus committed Feb 28, 2018
1 parent 884c941 commit 168adfb
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,10 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
var formatResult: FormatResult = NoFormatResult

val (instruction, subStatState) =
if (hasNestedAnonymousFunction(body))
if (hasNestedAnonymousFunction(body) && formattingPreferences(NewlinesAtNestedAnonymousFunctions) == Prevent)
(CompactEnsuringGap, formatterState.indent(-1))
else if (hasNestedAnonymousFunction(body) && formattingPreferences(NewlinesAtNestedAnonymousFunctions) == Force)
(EnsureNewlineAndIndent(1, Some(parentBlock.firstToken)), formatterState.indent)
else if (hiddenPredecessors(parameters.head.firstToken).containsNewline)
(indentedInstruction, formatterState.indent)
else
Expand All @@ -855,18 +857,23 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
formatResult = formatResult.before(parentBlock.firstToken, instruction)
formatResult ++= format(parameters)

var bodyState = subStatState

for (firstToken body.firstTokenOption) {
val instruction =
if (hasNestedAnonymousFunction(body))
CompactEnsuringGap
val (instruction, newState) =
if (hasNestedAnonymousFunction(body) && formattingPreferences(NewlinesAtNestedAnonymousFunctions) == Prevent)
(CompactEnsuringGap, subStatState)
else if (hasNestedAnonymousFunction(body) && formattingPreferences(NewlinesAtNestedAnonymousFunctions) == Preserve && !hiddenPredecessors(firstToken).containsNewline)
(CompactEnsuringGap, subStatState.indent(-1))
else if (hiddenPredecessors(firstToken).containsNewline || containsNewline(body))
statFormatterState(body.firstStatOpt)(subStatState).currentIndentLevelInstruction
(statFormatterState(body.firstStatOpt)(subStatState).currentIndentLevelInstruction, subStatState)
else
CompactEnsuringGap
(CompactEnsuringGap, subStatState)
formatResult = formatResult.before(firstToken, instruction)
bodyState = newState
}

formatResult ++= format(body)(subStatState)
formatResult ++= format(body)(bodyState)

formatResult
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package scalariform.formatter

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


class NestedAnonymousFunctionsTest extends AbstractFormatterTest {
// format: OFF
{
implicit val formattingPreferences = FormattingPreferences
.setPreference(NewlinesAtNestedAnonymousFunctions, Force)

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

{
implicit val formattingPreferences = FormattingPreferences
.setPreference(NewlinesAtNestedAnonymousFunctions, Prevent)

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

{
implicit val formattingPreferences = FormattingPreferences
.setPreference(NewlinesAtNestedAnonymousFunctions, Preserve)

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

"""def quux: Int => Int => String = { x: Int => y: Int =>
| "x"
|}""" ==>
"""def quux: 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 168adfb

Please sign in to comment.