@@ -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)
0 commit comments