Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f5f2996

Browse files
committed
Offset line numbers when reporting given a magic comment
1 parent b52e6b8 commit f5f2996

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import core.Decorators.*
1111
import printing.Highlighting.{Blue, Red, Yellow}
1212
import printing.SyntaxHighlighting
1313
import Diagnostic.*
14-
import util.{ SourcePosition, NoSourcePosition }
14+
import util.{ SourcePosition, NoSourcePosition, WrappedSourceFile }
1515
import util.Chars.{ LF, CR, FF, SU }
1616
import scala.annotation.switch
1717

@@ -44,7 +44,9 @@ trait MessageRendering {
4444
var maxLen = Int.MinValue
4545
def render(offsetAndLine: (Int, String)): String = {
4646
val (offset1, line) = offsetAndLine
47-
val lineNbr = (pos.source.offsetToLine(offset1) + 1).toString
47+
val magicOffset = WrappedSourceFile.locateMagicHeader(pos.source).getOrElse(0)
48+
println(i"magicOffset: $magicOffset")
49+
val lineNbr = (pos.source.offsetToLine(offset1) + 1 - magicOffset).toString
4850
val prefix = String.format(s"%${offset - 2}s |", lineNbr)
4951
maxLen = math.max(maxLen, prefix.length)
5052
val lnum = hl(" " * math.max(0, maxLen - prefix.length - 1) + prefix)

‎compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import core.Contexts.*
77
import collection.mutable
88
import scala.annotation.tailrec
99
import dotty.tools.dotc.reporting.Reporter
10-
import dotty.tools.dotc.util.SourcePosition;
10+
import dotty.tools.dotc.util.SourcePosition
1111

1212
import java.io.OutputStreamWriter
1313
import java.nio.charset.StandardCharsets.UTF_8

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ object ScriptSourceFile {
6161
}
6262
}
6363

64+
object WrappedSourceFile:
65+
private val cache: mutable.HashMap[SourceFile, Int] = mutable.HashMap.empty
66+
def locateMagicHeader(sourceFile: SourceFile)(using Context): Option[Int] =
67+
def findOffset: Int =
68+
val magicHeader = ctx.settings.XmagicOffsetHeader.value
69+
if magicHeader.isEmpty then
70+
-1
71+
else
72+
val s = new String(sourceFile.content)
73+
val regex = ("(?m)^" + java.util.regex.Pattern.quote(magicHeader) + "$").r
74+
val pos = regex.findFirstMatchIn(s).map(_.start).map(sourceFile.offsetToLine(_))
75+
pos.getOrElse(-1)
76+
val result = cache.getOrElseUpdate(sourceFile, findOffset)
77+
if result >= 0 then Some(result + 1) else None
78+
6479
class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends interfaces.SourceFile {
6580
import SourceFile.*
6681

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /