diff --git a/compiler/src/dotty/tools/dotc/util/SourcePosition.scala b/compiler/src/dotty/tools/dotc/util/SourcePosition.scala index 8333b376a38b..a68531138e94 100644 --- a/compiler/src/dotty/tools/dotc/util/SourcePosition.scala +++ b/compiler/src/dotty/tools/dotc/util/SourcePosition.scala @@ -22,8 +22,7 @@ extends interfaces.SourcePosition with Showable { def point: Int = span.point - /** The line of the position, starting at 0 */ - def line: Int = source.offsetToLine(point) + def line: Int = if (source.exists) source.offsetToLine(point) else -1 /** Extracts the lines from the underlying source file as `Array[Char]`*/ def linesSlice: Array[Char] = @@ -43,17 +42,16 @@ extends interfaces.SourcePosition with Showable { def beforeAndAfterPoint: (List[Int], List[Int]) = lineOffsets.partition(_ <= point) - /** The column of the position, starting at 0 */ - def column: Int = source.column(point) + def column: Int = if (source.exists) source.column(point) else -1 def start: Int = span.start - def startLine: Int = source.offsetToLine(start) - def startColumn: Int = source.column(start) + def startLine: Int = if (source.exists) source.offsetToLine(start) else -1 + def startColumn: Int = if (source.exists) source.column(start) else -1 def startColumnPadding: String = source.startColumnPadding(start) def end: Int = span.end - def endLine: Int = source.offsetToLine(end) - def endColumn: Int = source.column(end) + def endLine: Int = if (source.exists) source.offsetToLine(end) else -1 + def endColumn: Int = if (source.exists) source.column(end) else -1 def withOuter(outer: SourcePosition): SourcePosition = SourcePosition(source, span, outer) def withSpan(range: Span) = SourcePosition(source, range, outer) diff --git a/interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java b/interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java index d8afbf5f607a..8e0d1c5a2e45 100644 --- a/interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java +++ b/interfaces/src/dotty/tools/dotc/interfaces/SourcePosition.java @@ -17,23 +17,23 @@ public interface SourcePosition { /** @return Offset to the point */ int point(); - /** @return Line number of the point, starting at 0 */ + /** @return Line number of the point, starting at 0. -1 if the line cannot be computed */ int line(); - /** @return Column number of the point, starting at 0 */ + /** @return Column number of the point, starting at 0. -1 if the column cannot be computed */ int column(); /** @return Offset to the range start */ int start(); - /** @return Line number of the range start, starting at 0 */ + /** @return Line number of the range start, starting at 0. -1 if the line cannot be computed */ int startLine(); - /** @return Column number of the range start, starting at 0 */ + /** @return Column number of the range start, starting at 0. -1 if the column cannot be computed */ int startColumn(); /** @return Offset to the range end */ int end(); - /** @return Line number of the range end, starting at 0 */ + /** @return Line number of the range end, starting at 0. -1 if the line cannot be computed */ int endLine(); - /** @return Column number of the range end, starting at 0 */ + /** @return Column number of the range end, starting at 0. -1 if the column cannot be computed */ int endColumn(); /** The source file corresponding to this position. diff --git a/sbt-bridge/src/xsbt/DelegatingReporter.java b/sbt-bridge/src/xsbt/DelegatingReporter.java index 98671a9aff24..5f72cb2c18d4 100644 --- a/sbt-bridge/src/xsbt/DelegatingReporter.java +++ b/sbt-bridge/src/xsbt/DelegatingReporter.java @@ -80,13 +80,17 @@ public void doReport(MessageContainer cont, Context ctx) { SourceFile src = pos.source(); position = new Position() { public Optional sourceFile() { - return Optional.ofNullable(src.file().file()); + if (src.exists()) return Optional.empty(); + else return Optional.ofNullable(src.file().file()); } public Optional sourcePath() { - return Optional.ofNullable(src.file().path()); + if (src.exists()) return Optional.empty(); + else return Optional.ofNullable(src.file().path()); } public Optional line() { - return Optional.of(pos.line()); + int line = pos.line(); + if (line == -1) return Optional.empty(); + else return Optional.of(line); } public String lineContent() { String line = pos.lineContent(); @@ -101,15 +105,19 @@ public Optional offset() { return Optional.of(pos.point()); } public Optional pointer() { - return Optional.of(pos.point() - src.startOfLine(pos.point())); + if (!src.exists()) return Optional.empty(); + else return Optional.of(pos.point() - src.startOfLine(pos.point())); } public Optional pointerSpace() { - String lineContent = this.lineContent(); - int pointer = this.pointer().get(); - StringBuilder result = new StringBuilder(); - for (int i = 0; i < pointer; i++) - result.append(lineContent.charAt(i) == '\t' ? '\t' : ' '); - return Optional.of(result.toString()); + if (!src.exists()) return Optional.empty(); + else { + String lineContent = this.lineContent(); + int pointer = this.pointer().get(); + StringBuilder result = new StringBuilder(); + for (int i = 0; i < pointer; i++) + result.append(lineContent.charAt(i) == '\t' ? '\t' : ' '); + return Optional.of(result.toString()); + } } }; } else {