-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix diagnostics for Scala 2.13.12 (#1522)
* Fix diagnostics for Scala 2.13.12 * Fix sha256sum for org.scalameta:semanticdb-scalac_2_11:4.8.6 * Add Tally as an adopter * Fix ProtoReporter issue with semanticdb in Scala 2.12.x
- Loading branch information
Showing
10 changed files
with
304 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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
102 changes: 102 additions & 0 deletions
102
...a/io/bazel/rulesscala/scalac/reporter/after_2_12_13_and_before_2_13_12/ProtoReporter.java
This file contains 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,102 @@ | ||
package io.bazel.rulesscala.scalac.reporter; | ||
|
||
import io.bazel.rules_scala.diagnostics.Diagnostics; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.*; | ||
import scala.reflect.internal.util.Position; | ||
import scala.reflect.internal.util.RangePosition; | ||
import scala.tools.nsc.Settings; | ||
import scala.tools.nsc.reporters.ConsoleReporter; | ||
|
||
public class ProtoReporter extends ConsoleReporter { | ||
|
||
private final Map<String, List<Diagnostics.Diagnostic>> builder; | ||
|
||
public ProtoReporter(Settings settings) { | ||
super(settings); | ||
builder = new LinkedHashMap<>(); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
super.reset(); | ||
} | ||
|
||
public void writeTo(Path path) throws IOException { | ||
Diagnostics.TargetDiagnostics.Builder targetDiagnostics = | ||
Diagnostics.TargetDiagnostics.newBuilder(); | ||
for (Map.Entry<String, List<Diagnostics.Diagnostic>> entry : builder.entrySet()) { | ||
targetDiagnostics.addDiagnostics( | ||
Diagnostics.FileDiagnostics.newBuilder() | ||
.setPath(entry.getKey()) | ||
.addAllDiagnostics(entry.getValue())); | ||
} | ||
Files.write( | ||
path, | ||
targetDiagnostics.build().toByteArray(), | ||
StandardOpenOption.CREATE, | ||
StandardOpenOption.APPEND); | ||
} | ||
|
||
@Override | ||
public void info0(Position pos, String msg, Severity severity, boolean force) { | ||
doReport(pos, msg, severity); | ||
} | ||
|
||
@Override | ||
public void doReport(Position pos, String msg, Severity severity) { | ||
super.doReport(pos, msg, severity); | ||
|
||
Diagnostics.Diagnostic diagnostic = | ||
Diagnostics.Diagnostic.newBuilder() | ||
.setRange(positionToRange(pos)) | ||
.setSeverity(convertSeverity(severity)) | ||
.setMessage(msg) | ||
.build(); | ||
// TODO: Handle generated files | ||
String uri = "workspace-root://" + pos.source().file().path(); | ||
List<Diagnostics.Diagnostic> diagnostics = builder.computeIfAbsent(uri, key -> new ArrayList()); | ||
diagnostics.add(diagnostic); | ||
} | ||
|
||
private Diagnostics.Severity convertSeverity(Object severity) { | ||
String stringified = severity.toString().toLowerCase(); | ||
if ("error".equals(stringified)) { | ||
return Diagnostics.Severity.ERROR; | ||
} else if ("warning".equals(stringified)) { | ||
return Diagnostics.Severity.WARNING; | ||
} else if ("info".equals(stringified)) { | ||
return Diagnostics.Severity.INFORMATION; | ||
} | ||
throw new RuntimeException("Unknown severity: " + stringified); | ||
} | ||
|
||
private Diagnostics.Range positionToRange(Position pos) { | ||
if (pos instanceof RangePosition) { | ||
RangePosition rangePos = (RangePosition) pos; | ||
int startLine = pos.source().offsetToLine(rangePos.start()); | ||
int endLine = pos.source().offsetToLine(rangePos.end()); | ||
return Diagnostics.Range.newBuilder() | ||
.setStart( | ||
Diagnostics.Position.newBuilder() | ||
.setLine(startLine) | ||
.setCharacter(rangePos.start() - pos.source().lineToOffset(startLine))) | ||
.setEnd( | ||
Diagnostics.Position.newBuilder() | ||
.setLine(endLine) | ||
.setCharacter(rangePos.end() - pos.source().lineToOffset(endLine)) | ||
.build()) | ||
.build(); | ||
} | ||
return Diagnostics.Range.newBuilder() | ||
.setStart( | ||
Diagnostics.Position.newBuilder() | ||
.setLine(pos.line() - 1) | ||
.setCharacter(pos.column() - 1)) | ||
.setEnd(Diagnostics.Position.newBuilder().setLine(pos.line())) | ||
.build(); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
src/java/io/bazel/rulesscala/scalac/reporter/after_2_13_12/ProtoReporter.java
This file contains 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,99 @@ | ||
package io.bazel.rulesscala.scalac.reporter; | ||
|
||
import io.bazel.rules_scala.diagnostics.Diagnostics; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.*; | ||
import scala.collection.immutable.List$; | ||
import scala.reflect.internal.util.CodeAction; | ||
import scala.reflect.internal.util.Position; | ||
import scala.reflect.internal.util.RangePosition; | ||
import scala.tools.nsc.Settings; | ||
import scala.tools.nsc.reporters.ConsoleReporter; | ||
|
||
public class ProtoReporter extends ConsoleReporter { | ||
|
||
private final Map<String, List<Diagnostics.Diagnostic>> builder; | ||
|
||
public ProtoReporter(Settings settings) { | ||
super(settings); | ||
builder = new LinkedHashMap<>(); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
super.reset(); | ||
} | ||
|
||
public void writeTo(Path path) throws IOException { | ||
Diagnostics.TargetDiagnostics.Builder targetDiagnostics = | ||
Diagnostics.TargetDiagnostics.newBuilder(); | ||
for (Map.Entry<String, List<Diagnostics.Diagnostic>> entry : builder.entrySet()) { | ||
targetDiagnostics.addDiagnostics( | ||
Diagnostics.FileDiagnostics.newBuilder() | ||
.setPath(entry.getKey()) | ||
.addAllDiagnostics(entry.getValue())); | ||
} | ||
Files.write( | ||
path, | ||
targetDiagnostics.build().toByteArray(), | ||
StandardOpenOption.CREATE, | ||
StandardOpenOption.APPEND); | ||
} | ||
|
||
@Override | ||
public void doReport(Position pos, String msg, Severity severity, scala.collection.immutable.List<CodeAction> actions) { | ||
super.doReport(pos, msg, severity, List$.MODULE$.<CodeAction>empty()); | ||
|
||
Diagnostics.Diagnostic diagnostic = | ||
Diagnostics.Diagnostic.newBuilder() | ||
.setRange(positionToRange(pos)) | ||
.setSeverity(convertSeverity(severity)) | ||
.setMessage(msg) | ||
.build(); | ||
// TODO: Handle generated files | ||
String uri = "workspace-root://" + pos.source().file().path(); | ||
List<Diagnostics.Diagnostic> diagnostics = builder.computeIfAbsent(uri, key -> new ArrayList()); | ||
diagnostics.add(diagnostic); | ||
} | ||
|
||
private Diagnostics.Severity convertSeverity(Object severity) { | ||
String stringified = severity.toString().toLowerCase(); | ||
if ("error".equals(stringified)) { | ||
return Diagnostics.Severity.ERROR; | ||
} else if ("warning".equals(stringified)) { | ||
return Diagnostics.Severity.WARNING; | ||
} else if ("info".equals(stringified)) { | ||
return Diagnostics.Severity.INFORMATION; | ||
} | ||
throw new RuntimeException("Unknown severity: " + stringified); | ||
} | ||
|
||
private Diagnostics.Range positionToRange(Position pos) { | ||
if (pos instanceof RangePosition) { | ||
RangePosition rangePos = (RangePosition) pos; | ||
int startLine = pos.source().offsetToLine(rangePos.start()); | ||
int endLine = pos.source().offsetToLine(rangePos.end()); | ||
return Diagnostics.Range.newBuilder() | ||
.setStart( | ||
Diagnostics.Position.newBuilder() | ||
.setLine(startLine) | ||
.setCharacter(rangePos.start() - pos.source().lineToOffset(startLine))) | ||
.setEnd( | ||
Diagnostics.Position.newBuilder() | ||
.setLine(endLine) | ||
.setCharacter(rangePos.end() - pos.source().lineToOffset(endLine)) | ||
.build()) | ||
.build(); | ||
} | ||
return Diagnostics.Range.newBuilder() | ||
.setStart( | ||
Diagnostics.Position.newBuilder() | ||
.setLine(pos.line() - 1) | ||
.setCharacter(pos.column() - 1)) | ||
.setEnd(Diagnostics.Position.newBuilder().setLine(pos.line())) | ||
.build(); | ||
} | ||
} |
14 changes: 9 additions & 5 deletions
14
...sscala/scalac/reporter/ProtoReporter.java → ...eporter/before_2_12_13/ProtoReporter.java
This file contains 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 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
66 changes: 66 additions & 0 deletions
66
test/diagnostics_reporter/after_2_13_12/DiagnosticsReporterTest.java
This file contains 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,66 @@ | ||
package diagnostics_reporter; | ||
|
||
import io.bazel.rules_scala.diagnostics.Diagnostics; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DiagnosticsReporterTest { | ||
@SuppressWarnings("DoubleBraceInitialization") | ||
private static final Map<String, diagnostics_reporter.VerifyDiagnosticsOutput[]> tests = | ||
new HashMap<String, diagnostics_reporter.VerifyDiagnosticsOutput[]>() { | ||
{ | ||
put( | ||
"error_file", | ||
new diagnostics_reporter.VerifyDiagnosticsOutput[] { | ||
new diagnostics_reporter.VerifyDiagnosticsOutput( | ||
Diagnostics.Severity.ERROR, 5, 2, 6, 0) | ||
}); | ||
put( | ||
"two_errors_file", | ||
new diagnostics_reporter.VerifyDiagnosticsOutput[] { | ||
new diagnostics_reporter.VerifyDiagnosticsOutput( | ||
Diagnostics.Severity.ERROR, 4, 4, 4, 10), | ||
new diagnostics_reporter.VerifyDiagnosticsOutput( | ||
Diagnostics.Severity.ERROR, 5, 4, 5, 9) | ||
}); | ||
put( | ||
"warning_file", | ||
new diagnostics_reporter.VerifyDiagnosticsOutput[] { | ||
new diagnostics_reporter.VerifyDiagnosticsOutput( | ||
Diagnostics.Severity.WARNING, 0, 0, 0, 26) | ||
}); | ||
put( | ||
"error_and_warning_file", | ||
new diagnostics_reporter.VerifyDiagnosticsOutput[] { | ||
new diagnostics_reporter.VerifyDiagnosticsOutput( | ||
Diagnostics.Severity.WARNING, 0, 0, 0, 26), | ||
new diagnostics_reporter.VerifyDiagnosticsOutput( | ||
Diagnostics.Severity.ERROR, 4, 4, 4, 10) | ||
}); | ||
put( | ||
"info_file", | ||
new diagnostics_reporter.VerifyDiagnosticsOutput[] { | ||
new diagnostics_reporter.VerifyDiagnosticsOutput( | ||
Diagnostics.Severity.INFORMATION, -1, -1, 0, 0) | ||
}); | ||
} | ||
}; | ||
|
||
public static void main(String[] args) throws IOException { | ||
if (args.length != 1) throw new IllegalArgumentException("Args: <diagnostics_output>"); | ||
|
||
String diagnosticsOutput = args[0]; | ||
for (Map.Entry<String, VerifyDiagnosticsOutput[]> entry : tests.entrySet()) { | ||
String test = entry.getKey(); | ||
VerifyDiagnosticsOutput[] expectedDiagnosticsOutputs = entry.getValue(); | ||
List<Diagnostics.Diagnostic> diagnostics = | ||
VerifyDiagnosticsOutput.getDiagnostics( | ||
diagnosticsOutput + "/" + test + ".diagnosticsproto"); | ||
for (VerifyDiagnosticsOutput expectedDiagnostic : expectedDiagnosticsOutputs) { | ||
expectedDiagnostic.testOutput(diagnostics); | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains 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 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