@@ -13,13 +13,15 @@ import printing.SyntaxHighlighting
13
13
import Diagnostic ._
14
14
import util .{ SourcePosition , NoSourcePosition }
15
15
import util .Chars .{ LF , CR , FF , SU }
16
- import scala .annotation .switch
17
16
18
- import scala .collection .mutable
17
+ import scala .annotation .switch
18
+ import scala .collection .mutable , mutable .StringBuilder
19
+ import scala .util .chaining .given
19
20
20
21
trait MessageRendering {
21
22
import Highlight .*
22
23
import Offsets .*
24
+ import MessageRendering .*
23
25
24
26
/** Remove ANSI coloring from `str`, useful for getting real length of
25
27
* strings
@@ -204,12 +206,12 @@ trait MessageRendering {
204
206
| ${Blue (" Explanation" ).show}
205
207
| ${Blue (" ===========" ).show}""" .stripMargin
206
208
)
207
- sb.append( EOL ).append (m.explanation)
209
+ sb.newLine (m.explanation)
208
210
if (! m.explanation.endsWith(EOL )) sb.append(EOL )
209
211
sb.toString
210
212
}
211
213
212
- private def appendFilterHelp (dia : Diagnostic , sb : mutable. StringBuilder ): Unit =
214
+ private def appendFilterHelp (dia : Diagnostic , sb : StringBuilder ): Unit =
213
215
import dia ._
214
216
val hasId = msg.errorId.errorNumber >= 0
215
217
val category = dia match {
@@ -221,22 +223,22 @@ trait MessageRendering {
221
223
if (hasId || category.nonEmpty)
222
224
sb.append(EOL ).append(" Matching filters for @nowarn or -Wconf:" )
223
225
if (hasId)
224
- sb.append( EOL ).append (" - id=E" ).append(msg.errorId.errorNumber)
225
- sb.append( EOL ).append (" - name=" ).append(msg.errorId.productPrefix.stripSuffix(" ID" ))
226
+ sb.newLine (" - id=E" ).append(msg.errorId.errorNumber)
227
+ sb.newLine (" - name=" ).append(msg.errorId.productPrefix.stripSuffix(" ID" ))
226
228
if (category.nonEmpty)
227
- sb.append( EOL ).append (" - cat=" ).append(category)
229
+ sb.newLine (" - cat=" ).append(category)
228
230
229
231
/** The whole message rendered from `msg` */
230
232
def messageAndPos (dia : Diagnostic )(using Context ): String = {
231
- import dia ._
233
+ import dia .{ pos , msg , level }
232
234
val pos1 = pos.nonInlined
233
235
val inlineStack = inlinePosStack(pos).filter(_ != pos1)
234
236
val maxLineNumber =
235
237
if pos.exists then (pos1 :: inlineStack).map(_.endLine).max + 1
236
238
else 0
237
239
given Level = Level (level)
238
240
given Offset = Offset (maxLineNumber.toString.length + 2 )
239
- val sb = mutable. StringBuilder ()
241
+ val sb = StringBuilder ()
240
242
val posString = posStr(pos, msg, diagnosticLevel(dia))
241
243
if (posString.nonEmpty) sb.append(posString).append(EOL )
242
244
if (pos.exists) {
@@ -248,16 +250,16 @@ trait MessageRendering {
248
250
sb.append((srcBefore ::: marker :: err :: srcAfter).mkString(EOL ))
249
251
250
252
if inlineStack.nonEmpty then
251
- sb.append( EOL ).append (newBox())
252
- sb.append( EOL ).append (offsetBox).append(i " Inline stack trace " )
253
+ sb.newLine (newBox())
254
+ sb.newLine (offsetBox).append(i " Inline stack trace " )
253
255
for inlinedPos <- inlineStack if inlinedPos != pos1 do
254
- sb.append( EOL ).append (newBox(soft = true ))
255
- sb.append( EOL ).append (offsetBox).append(i " This location contains code that was inlined from $pos" )
256
+ sb.newLine (newBox(soft = true ))
257
+ sb.newLine (offsetBox).append(i " This location contains code that was inlined from $pos" )
256
258
if inlinedPos.source.file.exists then
257
259
val (srcBefore, srcAfter, _) = sourceLines(inlinedPos)
258
260
val marker = positionMarker(inlinedPos)
259
- sb.append( EOL ).append ((srcBefore ::: marker :: srcAfter).mkString(EOL ))
260
- sb.append( EOL ).append (endBox)
261
+ sb.newLine ((srcBefore ::: marker :: srcAfter).mkString(EOL ))
262
+ sb.newLine (endBox)
261
263
}
262
264
else sb.append(msg.message)
263
265
}
@@ -266,16 +268,16 @@ trait MessageRendering {
266
268
appendFilterHelp(dia, sb)
267
269
268
270
if Diagnostic .shouldExplain(dia) then
269
- sb.append( EOL ).append (newBox())
270
- sb.append( EOL ).append (offsetBox).append(" Explanation (enabled by `-explain`)" )
271
- sb.append( EOL ).append (newBox(soft = true ))
271
+ sb.newLine (newBox())
272
+ sb.newLine (offsetBox).append(" Explanation (enabled by `-explain`)" )
273
+ sb.newLine (newBox(soft = true ))
272
274
dia.msg.explanation.split(raw " \R " ).foreach { line =>
273
- sb.append( EOL ).append (offsetBox).append(if line.isEmpty then " " else " " ).append(line)
275
+ sb.newLine (offsetBox).append(if line.isEmpty then " " else " " ).append(line)
274
276
}
275
- sb.append( EOL ).append (endBox)
277
+ sb.newLine (endBox)
276
278
else if dia.msg.canExplain then
277
- sb.append( EOL ).append (offsetBox)
278
- sb.append( EOL ).append (offsetBox).append(" longer explanation available when compiling with `-explain`" )
279
+ sb.newLine (offsetBox)
280
+ sb.newLine (offsetBox).append(" longer explanation available when compiling with `-explain`" )
279
281
280
282
sb.toString
281
283
}
@@ -299,6 +301,10 @@ trait MessageRendering {
299
301
}
300
302
301
303
}
304
+ private object MessageRendering :
305
+ extension (sb : StringBuilder )
306
+ def newLine (s : String ): sb.type = sb.tap(_.append(EOL ).append(s))
307
+ def mkLines : String = sb.mkString(EOL )
302
308
303
309
private object Highlight {
304
310
opaque type Level = Int
0 commit comments