fix: Fix build.sbt position

**Problem**
Compiler warnings and errors is not correct for build.sbt.

**Solution**
This adds line offset to the synthetic Scala file
so the original line position is displayed in the errors.
This commit is contained in:
Eugene Yokota 2025-01-20 01:16:12 -05:00
parent 2323250b01
commit 4a7e1097d9
1 changed files with 9 additions and 7 deletions

View File

@ -97,8 +97,8 @@ class Eval(
| }
|}
|""".stripMargin
val startLine = header.linesIterator.toList.size
EvalSourceFile(srcName, startLine, contents)
val headerLine = header.linesIterator.toList.size
EvalSourceFile(srcName, headerLine, line - 1, contents)
override def extract(run: Run, unit: CompilationUnit)(using ctx: Context): String =
atPhase(Phases.typerPhase.next) {
@ -159,8 +159,8 @@ class Eval(
|${definitions.map(_._1).mkString("\n")}
|}
|""".stripMargin
val startLine = header.linesIterator.toList.size
EvalSourceFile(srcName, startLine, contents)
val headerLine = header.linesIterator.toList.size
EvalSourceFile(srcName, headerLine, 0, contents)
override def extract(run: Run, unit: CompilationUnit)(using ctx: Context): Seq[String] =
atPhase(Phases.typerPhase.next) {
@ -319,13 +319,15 @@ object Eval:
private[sbt] final val WrapValName = "$sbtdef"
// used to map the position offset
class EvalSourceFile(name: String, startLine: Int, contents: String)
class EvalSourceFile(name: String, headerLine: Int, extraLine: Int, contents: String)
extends SourceFile(
new VirtualFile(name, contents.getBytes(StandardCharsets.UTF_8)),
contents.toArray
):
override def lineToOffset(line: Int): Int = super.lineToOffset((line + startLine) max 0)
override def offsetToLine(offset: Int): Int = super.offsetToLine(offset) - startLine
override def lineToOffset(line: Int): Int =
super.lineToOffset((line + headerLine - extraLine) max 0)
override def offsetToLine(offset: Int): Int =
super.offsetToLine(offset) - headerLine + extraLine
end EvalSourceFile
trait EvalType[A]: