-
Couldn't load subscription status.
- Fork 106
feat: improve error highlighting to use precise ranges #1156
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
Open
warcholjakub
wants to merge
4
commits into
scalacenter:main
Choose a base branch
from
warcholjakub:feat/precise-errors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 0 additions & 67 deletions
67
instrumentation/src/main/scala/org/scastie/instrumentation/LineMapper.scala
This file was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
instrumentation/src/main/scala/org/scastie/instrumentation/PositionMapper.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package org.scastie.instrumentation | ||
|
|
||
| import RuntimeConstants._ | ||
| import org.scastie.api.ScalaTarget | ||
| import org.scastie.api.ScalaCli | ||
|
|
||
| case class PositionMapper private ( | ||
| private val lineMapping: Int => Int, | ||
| private val columnOffsetMapping: Int => Int | ||
| ) { | ||
|
|
||
| /** | ||
| * Maps a line from instrumented code back to the original user code. | ||
| * | ||
| * @param line | ||
| * the line number in the instrumented code (1-based) | ||
| * @return | ||
| * the corresponding line number in the original user code (1-based) | ||
| */ | ||
| def mapLine(line: Int): Int = { | ||
| lineMapping(line) | ||
| } | ||
|
|
||
| /** | ||
| * Maps a column from instrumented code back to the original user code, taking into account any column offsets | ||
| * | ||
| * @param line | ||
| * the line number in the instrumented code (1-based) | ||
| * @param column | ||
| * the column number in the instrumented code (1-based) | ||
| * @return | ||
| * the corresponding column number in the original user code (1-based) | ||
| */ | ||
| def mapColumn(line: Int, column: Int): Int = { | ||
| val offset = columnOffsetMapping(line) | ||
| math.max(1, column - offset) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| object PositionMapper { | ||
|
|
||
| private val instrumentationPrefix: String = "val $t = " | ||
| private val prefixOffset: Int = instrumentationPrefix.length | ||
|
|
||
| /** | ||
| * Creates a PositionMapper that maps positions from instrumented Scastie code back to original code. | ||
| * | ||
| * @param instrumentedCode | ||
| * the code after Scastie instrumentation has been applied | ||
| * @param isScalaCli | ||
| * whether the target is Scala CLI | ||
| * @return | ||
| * a PositionMapper for mapping positions | ||
| */ | ||
| def apply(instrumentedCode: String, isScalaCli: Boolean = false): PositionMapper = { | ||
| val lines = instrumentedCode.split('\n') | ||
| val (lineMapping, columnOffsetMapping) = calculateMappings(lines, isScalaCli) | ||
| new PositionMapper(lineMapping, columnOffsetMapping) | ||
| } | ||
|
|
||
| private def calculateMappings(lines: Array[String], isScalaCli: Boolean): (Int => Int, Int => Int) = { | ||
| case class State( | ||
| userCodeLinesSeen: Int = 0, | ||
| lineMappings: Map[Int, Int] = Map.empty, | ||
| columnOffsets: Map[Int, Int] = Map.empty | ||
| ) | ||
|
|
||
| val result = lines.zipWithIndex | ||
| .foldLeft(State()) { case (State(userCodeLinesSeen, lineMappings, columnOffsets), (line, index)) => | ||
| val instrumentedLineNumber = index + 1 | ||
| val trimmed = line.trim | ||
|
|
||
| val columnOffset = if (isWrappedUserCode(trimmed)) prefixOffset else 0 | ||
|
|
||
| if (!isExperimentalImport(trimmed) && !isInstrumentationLine(trimmed, isScalaCli)) { | ||
| val newCount = userCodeLinesSeen + 1 | ||
| State( | ||
| newCount, | ||
| lineMappings + (instrumentedLineNumber -> newCount), | ||
| columnOffsets + (instrumentedLineNumber -> columnOffset) | ||
| ) | ||
| } else { | ||
| val fallbackLine = if (userCodeLinesSeen > 0) userCodeLinesSeen else 1 | ||
| State( | ||
| userCodeLinesSeen, | ||
| lineMappings + (instrumentedLineNumber -> fallbackLine), | ||
| columnOffsets + (instrumentedLineNumber -> columnOffset) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| val lineMapping: Int => Int = | ||
| instrumentedLineNumber => result.lineMappings.getOrElse(instrumentedLineNumber, instrumentedLineNumber) | ||
|
|
||
| val columnOffsetMapping: Int => Int = | ||
| instrumentedLineNumber => result.columnOffsets.getOrElse(instrumentedLineNumber, 0) | ||
|
|
||
| (lineMapping, columnOffsetMapping) | ||
| } | ||
|
|
||
| private def isInstrumentationLine(line: String, isScalaCli: Boolean): Boolean = { | ||
| line.matches("""\$doc\.startStatement\(\d+,\s*\d+\);""") || | ||
| line.matches("""\$doc\.endStatement\(\);""") || | ||
| line.matches("""\$doc\.binder\(.+,\s*\d+,\s*\d+\);""") || | ||
| line == "scala.Predef.locally {" || | ||
| line == "$t}" || | ||
| line.startsWith(s"import $runtimePackage") || | ||
| line.startsWith(s"object $instrumentedObject extends ScastieApp with $instrumentationRecorderT") || | ||
| (line.startsWith("//> using") && isScalaCli) | ||
| } | ||
|
|
||
| private def isWrappedUserCode(line: String): Boolean = { | ||
| line.startsWith(instrumentationPrefix) | ||
| } | ||
|
|
||
| private def isExperimentalImport(line: String): Boolean = { | ||
| val experimentalRegex = """^\s*import\s+language\.experimental\.[^\n]+""".r | ||
| experimentalRegex.matches(line) | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this ever happen? Maybe lets throw here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question 😅. I honestly can't remember what the use case for this was. I’m sure I had a reason, but from what I’ve tested now, I couldn’t reproduce this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe lets make it a tuple Option[(Int, Int)] ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Diagnostic requires a Double. Is this the solution you had in mind?