From 681eb39c96d50129ee596b06a2d5cbc996ab40da Mon Sep 17 00:00:00 2001 From: Yichen Xu Date: Tue, 3 Dec 2024 16:24:47 +0100 Subject: [PATCH] add WrappedSourceFile --- .../src/dotty/tools/dotc/util/SourceFile.scala | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/util/SourceFile.scala b/compiler/src/dotty/tools/dotc/util/SourceFile.scala index 3ea43d16a7c8..f9a8c2da1bb8 100644 --- a/compiler/src/dotty/tools/dotc/util/SourceFile.scala +++ b/compiler/src/dotty/tools/dotc/util/SourceFile.scala @@ -286,7 +286,7 @@ object SourceFile { if isScript(file, chars) then ScriptSourceFile(file, chars) else - SourceFile(file, chars) + WrappedSourceFile(file, chars) def apply(file: AbstractFile | Null, computeContent: => Array[Char]): SourceFile = new SourceFile(file, computeContent) } @@ -295,3 +295,17 @@ object SourceFile { override def exists: Boolean = false override def atSpan(span: Span): SourcePosition = NoSourcePosition } + +object WrappedSourceFile: + val headerText = """//SOURCECODE_ORIGINAL_CODE_START_MARKER""" + @sharable private val headerPattern = Pattern.compile(s"""^$headerText(\r|\n|\r\n)""", Pattern.MULTILINE) + def apply(file: AbstractFile, content: Array[Char]): SourceFile = + val matcher = headerPattern matcher content.mkString + if matcher.find then + val offset = matcher.end + val originalSource = new SourceFile(file, content.drop(offset)) + new SourceFile(file, content): + override def atSpan(span: Span): SourcePosition = + if span.exists then SourcePosition(originalSource, span.shift(-offset)) + else NoSourcePosition + else new SourceFile(file, content)