Skip to content

Commit

Permalink
Merge pull request #1527 from lf-lang/clem.cleanup-error-rendering
Browse files Browse the repository at this point in the history
Refactor error reporter
  • Loading branch information
petervdonovan authored Jun 22, 2023
2 parents 1e28d32 + d5e7fdc commit 8366599
Show file tree
Hide file tree
Showing 102 changed files with 1,463 additions and 1,642 deletions.
4 changes: 2 additions & 2 deletions cli/base/src/main/java/org/lflang/cli/CliBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.eclipse.xtext.validation.CheckMode;
import org.eclipse.xtext.validation.IResourceValidator;
import org.eclipse.xtext.validation.Issue;
import org.lflang.ErrorReporter;
import org.lflang.LFRuntimeModule;
import org.lflang.LFStandaloneSetup;
import org.lflang.MessageReporter;
import org.lflang.util.FileUtil;
import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
Expand Down Expand Up @@ -88,7 +88,7 @@ static class MutuallyExclusive {
@Inject protected ReportingBackend reporter;

/** Used to report error messages at the end. */
@Inject protected ErrorReporter errorReporter;
@Inject protected MessageReporter messageReporter;

/** IO context of this run. */
@Inject protected Io io;
Expand Down
4 changes: 2 additions & 2 deletions cli/base/src/main/java/org/lflang/cli/LFStandaloneModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.eclipse.emf.ecore.EValidator;
import org.eclipse.emf.ecore.impl.EValidatorRegistryImpl;
import org.eclipse.xtext.validation.ValidationMessageAcceptor;
import org.lflang.ErrorReporter;
import org.lflang.LFRuntimeModule;
import org.lflang.MessageReporter;

/**
* Module that is only available when running LFC as a standalone program.
Expand All @@ -56,7 +56,7 @@ public LFStandaloneModule(ReportingBackend helper, Io io) {

@Override
public void configure(Binder binder) {
binder.bind(ErrorReporter.class).to(StandaloneErrorReporter.class);
binder.bind(MessageReporter.class).to(StandaloneMessageReporter.class);
binder.bind(ReportingBackend.class).toInstance(helper);
binder.bind(Io.class).toInstance(io);
binder.bind(ValidationMessageAcceptor.class).to(StandaloneIssueAcceptor.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,72 +30,42 @@
import com.google.inject.Inject;
import java.nio.file.Path;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.xtext.diagnostics.Severity;
import org.lflang.ErrorReporter;
import org.lflang.MessageReporterBase;
import org.lflang.generator.Range;

/**
* An error reporter that forwards all messages to an {@link IssueCollector}. They'll be sorted out
* later.
*/
public class StandaloneErrorReporter implements ErrorReporter {
public class StandaloneMessageReporter extends MessageReporterBase {

@Inject private StandaloneIssueAcceptor issueAcceptor;

private String reportWithNode(String message, Severity severity, EObject obj) {
issueAcceptor.accept(severity, message, obj, null, 0, null);
return message;
}

private String reportSimpleFileCtx(String message, Severity severity, Integer line, Path path) {
LfIssue issue = new LfIssue(message, severity, line, 1, line, 1, 0, path);
issueAcceptor.accept(issue);
// Return a string that can be inserted into the generated code.
return message;
}

@Override
public String reportError(String message) {
return reportSimpleFileCtx(message, Severity.ERROR, null, null);
}

@Override
public String reportWarning(String message) {
return reportSimpleFileCtx(message, Severity.WARNING, null, null);
}

@Override
public String reportInfo(String message) {
return reportSimpleFileCtx(message, Severity.INFO, null, null);
static Severity convertSeverity(DiagnosticSeverity severity) {
return switch (severity) {
case Error -> Severity.ERROR;
case Warning -> Severity.WARNING;
case Information, Hint -> Severity.INFO;
};
}

@Override
public String reportError(EObject obj, String message) {
return reportWithNode(message, Severity.ERROR, obj);
protected void reportOnNode(EObject node, DiagnosticSeverity severity, String message) {
issueAcceptor.accept(convertSeverity(severity), message, node, null, 0, null);
}

@Override
public String reportWarning(EObject obj, String message) {
return reportWithNode(message, Severity.WARNING, obj);
}

@Override
public String reportInfo(EObject obj, String message) {
return reportWithNode(message, Severity.INFO, obj);
}

@Override
public String reportError(Path file, Integer line, String message) {
return reportSimpleFileCtx(message, Severity.ERROR, line, file);
}

@Override
public String reportWarning(Path file, Integer line, String message) {
return reportSimpleFileCtx(message, Severity.WARNING, line, file);
protected void report(Path path, Range range, DiagnosticSeverity severity, String message) {
LfIssue issue = new LfIssue(message, convertSeverity(severity), path, range);
issueAcceptor.accept(issue);
}

@Override
public String reportInfo(Path file, Integer line, String message) {
return reportSimpleFileCtx(message, Severity.INFO, line, file);
protected void reportWithoutPosition(DiagnosticSeverity severity, String message) {
LfIssue issue = new LfIssue(message, convertSeverity(severity), null, null);
issueAcceptor.accept(issue);
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions cli/base/src/main/kotlin/org/lflang/cli/ReportingUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package org.lflang.cli
import com.google.inject.Inject
import com.google.inject.Singleton
import org.eclipse.xtext.diagnostics.Severity
import org.lflang.generator.Range
import java.io.IOException
import java.io.PrintStream
import java.lang.Error
Expand Down Expand Up @@ -135,6 +136,21 @@ data class LfIssue(
val file: Path?
) : Comparable<LfIssue> {

constructor(
message: String,
severity: Severity,
path: Path?,
range: Range?
) : this(
message, severity,
line = range?.startInclusive?.oneBasedLine,
column = range?.startInclusive?.oneBasedColumn,
endLine = range?.endExclusive?.oneBasedLine,
endColumn = range?.endExclusive?.oneBasedColumn,
length = null,
file = path
)

override operator fun compareTo(other: LfIssue): Int =
issueComparator.compare(this, other)

Expand Down
2 changes: 1 addition & 1 deletion cli/lfc/src/main/java/org/lflang/cli/Lfc.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private void invokeGenerator(List<Path> files, Path root, Properties properties)
properties,
resource,
this.fileAccess,
fileConfig -> errorReporter);
fileConfig -> messageReporter);

try {
this.generator.generate(resource, this.fileAccess, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.junit.jupiter.api.Test;
import org.lflang.Target;
import org.lflang.generator.IntegratedBuilder;
import org.lflang.generator.LanguageServerErrorReporter;
import org.lflang.generator.LanguageServerMessageReporter;
import org.lflang.tests.LFTest;
import org.lflang.tests.LfInjectedTestBase;
import org.lflang.tests.TestBase;
Expand Down Expand Up @@ -150,7 +150,7 @@ private void checkDiagnostics(
Random random)
throws IOException {
MockLanguageClient client = new MockLanguageClient();
LanguageServerErrorReporter.setClient(client);
LanguageServerMessageReporter.setClient(client);
for (LFTest test : selectTests(target, random)) {
client.clearDiagnostics();
if (alterer != null) {
Expand Down
68 changes: 0 additions & 68 deletions core/src/main/java/org/lflang/DefaultErrorReporter.java

This file was deleted.

33 changes: 33 additions & 0 deletions core/src/main/java/org/lflang/DefaultMessageReporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.lflang;

import java.nio.file.Path;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.lflang.generator.Range;

/** Simple implementation of the ErrorReport interface that simply prints to standard out. */
public class DefaultMessageReporter extends MessageReporterBase implements MessageReporter {

private void println(String s) {
System.out.println(s);
}

@Override
protected void report(Path path, Range range, DiagnosticSeverity severity, String message) {
reportWithoutPosition(severity, message);
}

@Override
protected void reportOnNode(EObject node, DiagnosticSeverity severity, String message) {
reportWithoutPosition(severity, message);
}

@Override
protected void reportWithoutPosition(DiagnosticSeverity severity, String message) {
switch (severity) {
case Error -> println("ERROR: " + message);
case Warning -> println("WARNING: " + message);
case Information, Hint -> println("INFO: " + message);
}
}
}
Loading

0 comments on commit 8366599

Please sign in to comment.