@@ -91,26 +91,6 @@ abstract class ScalaFormatter
91
91
var suspendFormatting = false
92
92
var edits : List [TextEdit ] = Nil // Stored in reverse
93
93
94
- def printableFormattingInstruction (previousTokenOption : Option [Token ], token : Token , nextTokenOption : Option [Token ]) = {
95
- val maybePredecessorFormatting = predecessorFormatting.get(token)
96
- val isGaplessAssignment =
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 )
101
- }
102
- val maybeInstruction =
103
- if (isGaplessAssignment) Some (CompactEnsuringGap )
104
- else
105
- maybePredecessorFormatting.orElse(
106
- previousTokenOption.map(defaultFormattingInstruction(_, token))
107
- )
108
- maybeInstruction.getOrElse(
109
- if (token.tokenType == EOF ) EnsureNewlineAndIndent (0 ) /* <-- to allow formatting of files with just a scaladoc comment */
110
- else Compact
111
- )
112
- }
113
-
114
94
for ((previousTokenOption, token, nextTokenOption) ← Utils .withPreviousAndNext(tokens)) {
115
95
val previousTokenIsPrintable = previousTokenOption exists { ! isInferredNewline(_) }
116
96
if (isInferredNewline(token)) {
@@ -130,7 +110,9 @@ abstract class ScalaFormatter
130
110
basicFormattingInstruction
131
111
val nextTokenUnindents = nextTokenOption exists { _.tokenType == RBRACE }
132
112
val includeBufferBeforeNextToken = nextTokenOption exists { nextToken ⇒
133
- ! printableFormattingInstruction(Some (token), nextToken, None ).isInstanceOf [EnsureNewlineAndIndent ]
113
+ ! printableFormattingInstruction(
114
+ Some (token), nextToken, None , predecessorFormatting
115
+ ).isInstanceOf [EnsureNewlineAndIndent ]
134
116
}
135
117
edits :::= writeHiddenTokens(builder, inferredNewlines(token), formattingInstruction, nextTokenUnindents,
136
118
includeBufferBeforeNextToken, previousTokenIsPrintable, tokenIndentMap).toList
@@ -142,7 +124,10 @@ abstract class ScalaFormatter
142
124
tokenIndentMap += (token -> builder.currentColumn)
143
125
builder.append(token.rawText)
144
126
} else {
145
- val formattingInstruction = printableFormattingInstruction(previousTokenOption, token, nextTokenOption)
127
+ val formattingInstruction =
128
+ printableFormattingInstruction(
129
+ previousTokenOption, token, nextTokenOption, predecessorFormatting
130
+ )
146
131
val nextTokenUnindents = token.tokenType == RBRACE
147
132
val includeBufferBeforeNextToken = true // <-- i.e. current token
148
133
val hiddenTokens = hiddenPredecessors(token)
@@ -444,6 +429,31 @@ abstract class ScalaFormatter
444
429
result
445
430
}
446
431
432
+ private def printableFormattingInstruction (
433
+ previousTokenOption : Option [Token ],
434
+ token : Token ,
435
+ nextTokenOption : Option [Token ],
436
+ predecessorFormatting : Map [Token , IntertokenFormatInstruction ]): IntertokenFormatInstruction = {
437
+
438
+ val maybePredecessorFormatting = predecessorFormatting.get(token)
439
+ val isGaplessAssignment =
440
+ maybePredecessorFormatting match {
441
+ // `foreach(_.id= ..)`
442
+ case x @ Some (PlaceAtColumn (_, _, Some (Token (USCORE , _, _, _)))) => token.tokenType == EQUALS
443
+ // `foreach(foo= _)`
444
+ case _ => token.tokenType == EQUALS && nextTokenOption.exists(_.tokenType == USCORE )
445
+ }
446
+ val maybeInstruction =
447
+ if (isGaplessAssignment) Some (CompactEnsuringGap )
448
+ else maybePredecessorFormatting.orElse(
449
+ previousTokenOption.map(defaultFormattingInstruction(_, token))
450
+ )
451
+ maybeInstruction.getOrElse(
452
+ if (token.tokenType == EOF ) EnsureNewlineAndIndent (0 ) /* <-- to allow formatting of files with just a scaladoc comment */
453
+ else Compact
454
+ )
455
+ }
456
+
447
457
private def defaultFormattingInstruction (token1 : Token , token2 : Token ): IntertokenFormatInstruction = {
448
458
val result = actualDefaultFormattingInstruction(token1, token2)
449
459
// println("defaultFormattingInstruction(" + token1 + ", " + token2 + ") = " + result)
0 commit comments