Skip to content

Commit dcf60d5

Browse files
committed
preserve postfix assignment spacing; fixes #254
1 parent 1798e96 commit dcf60d5

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

scalariform/src/main/scala/scalariform/formatter/ScalaFormatter.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,19 @@ abstract class ScalaFormatter
9191
var suspendFormatting = false
9292
var edits: List[TextEdit] = Nil // Stored in reverse
9393

94-
def printableFormattingInstruction(previousTokenOpt: Option[Token], token: Token) = {
94+
def printableFormattingInstruction(previousTokenOption: Option[Token], token: Token, nextTokenOption: Option[Token]) = {
95+
val maybePredecessorFormatting = predecessorFormatting.get(token)
9596
val isGaplessAssignment =
96-
predecessorFormatting.get(token) match { // avoid `foreach(_.id= ...)` gapless assignment (see MutateTest.scala)
97-
case Some(PlaceAtColumn(_, _, Some(Token(USCORE, _, _, _)))) if token.tokenType == EQUALS => true
98-
case _ => false
97+
// avoid `foreach(_.id= ..)` and `foreach(foo= _)` gapless assignment (see MutateTest.scala)
98+
maybePredecessorFormatting exists {
99+
case x @ PlaceAtColumn(_, _, Some(Token(USCORE, _, _, _))) => token.tokenType == EQUALS
100+
case _ => token.tokenType == EQUALS && nextTokenOption.exists(_.tokenType == USCORE)
99101
}
100102
val maybeInstruction =
101103
if (isGaplessAssignment) Some(CompactEnsuringGap)
102104
else
103-
predecessorFormatting.get(token).orElse(
104-
previousTokenOpt.map(defaultFormattingInstruction(_, token))
105+
maybePredecessorFormatting.orElse(
106+
previousTokenOption.map(defaultFormattingInstruction(_, token))
105107
)
106108
maybeInstruction.getOrElse(
107109
if (token.tokenType == EOF) EnsureNewlineAndIndent(0) /* <-- to allow formatting of files with just a scaladoc comment */
@@ -128,7 +130,7 @@ abstract class ScalaFormatter
128130
basicFormattingInstruction
129131
val nextTokenUnindents = nextTokenOption exists { _.tokenType == RBRACE }
130132
val includeBufferBeforeNextToken = nextTokenOption exists { nextToken
131-
!printableFormattingInstruction(Some(token), nextToken).isInstanceOf[EnsureNewlineAndIndent]
133+
!printableFormattingInstruction(Some(token), nextToken, None).isInstanceOf[EnsureNewlineAndIndent]
132134
}
133135
edits :::= writeHiddenTokens(builder, inferredNewlines(token), formattingInstruction, nextTokenUnindents,
134136
includeBufferBeforeNextToken, previousTokenIsPrintable, tokenIndentMap).toList
@@ -140,7 +142,7 @@ abstract class ScalaFormatter
140142
tokenIndentMap += (token -> builder.currentColumn)
141143
builder.append(token.rawText)
142144
} else {
143-
val formattingInstruction = printableFormattingInstruction(previousTokenOption, token)
145+
val formattingInstruction = printableFormattingInstruction(previousTokenOption, token, nextTokenOption)
144146
val nextTokenUnindents = token.tokenType == RBRACE
145147
val includeBufferBeforeNextToken = true // <-- i.e. current token
146148
val hiddenTokens = hiddenPredecessors(token)

scalariform/src/test/scala/scalariform/formatter/MutateTest.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class MutateTest extends AbstractFormatterTest {
2121
| maybeHtmlNode.foreach(_.outerHTML = block.toString)
2222
|}"""
2323

24+
"""val assignment = foo.foreach(bar.baz = _)""" ==>
25+
"""val assignment = foo.foreach(bar.baz = _)"""
26+
2427
// format: ON
2528

2629
override val debug = false

0 commit comments

Comments
 (0)