Skip to content

Support magic comment for offseting source code line number #22100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion compiler/src/dotty/tools/dotc/util/SourceFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a concept for a different offset for Sourcefile by setting the underlying file and the start offset

if span.exists then SourcePosition(originalSource, span.shift(-offset))
else NoSourcePosition
else new SourceFile(file, content)
Loading