@@ -13,13 +13,15 @@ import printing.SyntaxHighlighting
1313import Diagnostic ._
1414import util .{ SourcePosition , NoSourcePosition }
1515import util .Chars .{ LF , CR , FF , SU }
16- import scala .annotation .switch
1716
18- import scala .collection .mutable
17+ import scala .annotation .switch
18+ import scala .collection .mutable , mutable .StringBuilder
19+ import scala .util .chaining .given
1920
2021trait MessageRendering {
2122 import Highlight .*
2223 import Offsets .*
24+ import MessageRendering .*
2325
2426 /** Remove ANSI coloring from `str`, useful for getting real length of
2527 * strings
@@ -204,12 +206,12 @@ trait MessageRendering {
204206 | ${Blue (" Explanation" ).show}
205207 | ${Blue (" ===========" ).show}""" .stripMargin
206208 )
207- sb.append( EOL ).append (m.explanation)
209+ sb.newLine (m.explanation)
208210 if (! m.explanation.endsWith(EOL )) sb.append(EOL )
209211 sb.toString
210212 }
211213
212- private def appendFilterHelp (dia : Diagnostic , sb : mutable. StringBuilder ): Unit =
214+ private def appendFilterHelp (dia : Diagnostic , sb : StringBuilder ): Unit =
213215 import dia ._
214216 val hasId = msg.errorId.errorNumber >= 0
215217 val category = dia match {
@@ -221,22 +223,22 @@ trait MessageRendering {
221223 if (hasId || category.nonEmpty)
222224 sb.append(EOL ).append(" Matching filters for @nowarn or -Wconf:" )
223225 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" ))
226228 if (category.nonEmpty)
227- sb.append( EOL ).append (" - cat=" ).append(category)
229+ sb.newLine (" - cat=" ).append(category)
228230
229231 /** The whole message rendered from `msg` */
230232 def messageAndPos (dia : Diagnostic )(using Context ): String = {
231- import dia ._
233+ import dia .{ pos , msg , level }
232234 val pos1 = pos.nonInlined
233235 val inlineStack = inlinePosStack(pos).filter(_ != pos1)
234236 val maxLineNumber =
235237 if pos.exists then (pos1 :: inlineStack).map(_.endLine).max + 1
236238 else 0
237239 given Level = Level (level)
238240 given Offset = Offset (maxLineNumber.toString.length + 2 )
239- val sb = mutable. StringBuilder ()
241+ val sb = StringBuilder ()
240242 val posString = posStr(pos, msg, diagnosticLevel(dia))
241243 if (posString.nonEmpty) sb.append(posString).append(EOL )
242244 if (pos.exists) {
@@ -248,16 +250,16 @@ trait MessageRendering {
248250 sb.append((srcBefore ::: marker :: err :: srcAfter).mkString(EOL ))
249251
250252 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 " )
253255 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" )
256258 if inlinedPos.source.file.exists then
257259 val (srcBefore, srcAfter, _) = sourceLines(inlinedPos)
258260 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)
261263 }
262264 else sb.append(msg.message)
263265 }
@@ -266,16 +268,16 @@ trait MessageRendering {
266268 appendFilterHelp(dia, sb)
267269
268270 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 ))
272274 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)
274276 }
275- sb.append( EOL ).append (endBox)
277+ sb.newLine (endBox)
276278 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`" )
279281
280282 sb.toString
281283 }
@@ -299,6 +301,10 @@ trait MessageRendering {
299301 }
300302
301303}
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 )
302308
303309private object Highlight {
304310 opaque type Level = Int
0 commit comments