Skip to content

Commit e93f41b

Browse files
committed
Line helpers
1 parent 89c3540 commit e93f41b

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import printing.SyntaxHighlighting
1313
import Diagnostic._
1414
import util.{ SourcePosition, NoSourcePosition }
1515
import 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

2021
trait 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

303309
private object Highlight {
304310
opaque type Level = Int

compiler/src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,9 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
198198
def lineContent(offset: Int): String =
199199
content.slice(startOfLine(offset), nextLine(offset)).mkString
200200

201-
/** The column corresponding to `offset`, starting at 0 */
202-
def column(offset: Int): Int = {
203-
var idx = startOfLine(offset)
204-
offset - idx
205-
}
201+
/** The column (index into the line) corresponding to `offset`, starting at 0 */
202+
def column(offset: Int): Int =
203+
offset - startOfLine(offset)
206204

207205
/** The padding of the column corresponding to `offset`, includes tabs */
208206
def startColumnPadding(offset: Int): String = {

0 commit comments

Comments
 (0)