Reload rendrer when application changes (for tests)
This commit is contained in:
+1
-1
@@ -90,7 +90,7 @@ public final class AnalyzerWithCompilerReport {
|
||||
render = ((MyDiagnostic)diagnostic).message;
|
||||
}
|
||||
else {
|
||||
render = DefaultErrorMessages.RENDERER.render(diagnostic);
|
||||
render = DefaultErrorMessages.render(diagnostic);
|
||||
}
|
||||
PsiFile file = diagnostic.getPsiFile();
|
||||
messageCollector.report(convertSeverity(diagnostic.getSeverity()), render,
|
||||
|
||||
@@ -611,7 +611,7 @@ public class CheckerTestUtil {
|
||||
|
||||
@NotNull
|
||||
public static TextDiagnostic asTextDiagnostic(@NotNull Diagnostic diagnostic) {
|
||||
DiagnosticRenderer renderer = getRenderer(diagnostic);
|
||||
DiagnosticRenderer renderer = DefaultErrorMessages.getRendererForDiagnostic(diagnostic);
|
||||
String diagnosticName = diagnostic.getFactory().getName();
|
||||
if (renderer instanceof AbstractDiagnosticWithParametersRenderer) {
|
||||
//noinspection unchecked
|
||||
@@ -627,17 +627,6 @@ public class CheckerTestUtil {
|
||||
return new TextDiagnostic(diagnosticName, null);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static DiagnosticRenderer getRenderer(@NotNull Diagnostic diagnostic) {
|
||||
for (DiagnosticFactoryToRendererMap map : DefaultErrorMessages.MAPS) {
|
||||
DiagnosticRenderer renderer = map.get(diagnostic.getFactory());
|
||||
if (renderer != null)
|
||||
return renderer;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
private final String name;
|
||||
@Nullable
|
||||
|
||||
@@ -35,7 +35,7 @@ public interface DiagnosticSink {
|
||||
if (diagnostic.getSeverity() == Severity.ERROR) {
|
||||
PsiFile psiFile = diagnostic.getPsiFile();
|
||||
List<TextRange> textRanges = diagnostic.getTextRanges();
|
||||
String diagnosticText = DefaultErrorMessages.RENDERER.render(diagnostic);
|
||||
String diagnosticText = DefaultErrorMessages.render(diagnostic);
|
||||
throw new IllegalStateException(diagnostic.getFactory().getName() + ": " + diagnosticText + " " + DiagnosticUtils.atLocation(psiFile, textRanges.get(0)));
|
||||
}
|
||||
}
|
||||
|
||||
+60
-9
@@ -17,11 +17,15 @@
|
||||
package org.jetbrains.jet.lang.diagnostics.rendering;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.intellij.openapi.application.Application;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
import com.intellij.openapi.extensions.Extensions;
|
||||
import kotlin.Function1;
|
||||
import kotlin.KotlinPackage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticFactory;
|
||||
import org.jetbrains.jet.lang.diagnostics.Errors;
|
||||
@@ -54,9 +58,42 @@ public class DefaultErrorMessages {
|
||||
}
|
||||
|
||||
private static final DiagnosticFactoryToRendererMap MAP = new DiagnosticFactoryToRendererMap();
|
||||
public static final List<DiagnosticFactoryToRendererMap> MAPS = ImmutableList.<DiagnosticFactoryToRendererMap>builder()
|
||||
.addAll(
|
||||
KotlinPackage.map(
|
||||
private static List<DiagnosticFactoryToRendererMap> maps = null;
|
||||
private static Application application = ApplicationManager.getApplication();
|
||||
private static DispatchingDiagnosticRenderer renderer = null;
|
||||
|
||||
@NotNull
|
||||
public static String render(@NotNull Diagnostic diagnostic) {
|
||||
return getRenderer().render(diagnostic);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static DiagnosticRenderer<Diagnostic> getRenderer() {
|
||||
boolean mapsChanged = resetMapsIfNeeded();
|
||||
|
||||
// Renderer is changed in tests only
|
||||
if (renderer == null || mapsChanged) {
|
||||
renderer = new DispatchingDiagnosticRenderer(maps);
|
||||
}
|
||||
|
||||
return renderer;
|
||||
}
|
||||
|
||||
private static boolean resetMapsIfNeeded() {
|
||||
boolean needToResetMaps = maps == null;
|
||||
Application newApp = ApplicationManager.getApplication();
|
||||
|
||||
if (application != newApp) {
|
||||
assert newApp.isUnitTestMode(): "Expected application switch only in tests";
|
||||
application = newApp;
|
||||
needToResetMaps = true;
|
||||
}
|
||||
|
||||
if (!needToResetMaps) return false;
|
||||
|
||||
maps = ImmutableList.<DiagnosticFactoryToRendererMap>builder()
|
||||
.addAll(
|
||||
KotlinPackage.map(
|
||||
Extensions.getExtensions(Extension.EP_NAME),
|
||||
new Function1<Extension, DiagnosticFactoryToRendererMap>() {
|
||||
@Override
|
||||
@@ -64,15 +101,27 @@ public class DefaultErrorMessages {
|
||||
return extension.getMap();
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
.add(MAP)
|
||||
.build();
|
||||
)
|
||||
)
|
||||
.add(MAP)
|
||||
.build();
|
||||
|
||||
public static final DiagnosticRenderer<Diagnostic> RENDERER = new DispatchingDiagnosticRenderer(MAPS);
|
||||
return true;
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
@Nullable
|
||||
public static DiagnosticRenderer getRendererForDiagnostic(@NotNull Diagnostic diagnostic) {
|
||||
for (DiagnosticFactoryToRendererMap map : maps) {
|
||||
DiagnosticRenderer renderer = map.get(diagnostic.getFactory());
|
||||
|
||||
if (renderer != null) return renderer;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static {
|
||||
|
||||
MAP.put(UNRESOLVED_REFERENCE, "Unresolved reference: {0}", ELEMENT_TEXT);
|
||||
|
||||
MAP.put(INVISIBLE_REFERENCE, "Cannot access ''{0}'': it is ''{1}'' in ''{2}''", NAME, TO_STRING, NAME);
|
||||
@@ -568,6 +617,8 @@ public class DefaultErrorMessages {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resetMapsIfNeeded();
|
||||
}
|
||||
|
||||
private DefaultErrorMessages() {
|
||||
|
||||
+2
-1
@@ -22,9 +22,10 @@ import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||
import java.util.List;
|
||||
|
||||
public class DispatchingDiagnosticRenderer implements DiagnosticRenderer<Diagnostic> {
|
||||
@NotNull
|
||||
private final List<DiagnosticFactoryToRendererMap> maps;
|
||||
|
||||
public DispatchingDiagnosticRenderer(List<DiagnosticFactoryToRendererMap> maps) {
|
||||
public DispatchingDiagnosticRenderer(@NotNull List<DiagnosticFactoryToRendererMap> maps) {
|
||||
this.maps = maps;
|
||||
}
|
||||
|
||||
|
||||
@@ -233,7 +233,7 @@ public class JetTestUtils {
|
||||
@Override
|
||||
public void report(@NotNull Diagnostic diagnostic) {
|
||||
if (diagnostic.getSeverity() == Severity.ERROR) {
|
||||
throw new IllegalStateException(DefaultErrorMessages.RENDERER.render(diagnostic));
|
||||
throw new IllegalStateException(DefaultErrorMessages.render(diagnostic));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -148,7 +148,7 @@ public class TypeSubstitutorTest extends KotlinTestWithEnvironment {
|
||||
new Function<Diagnostic, String>() {
|
||||
@Override
|
||||
public String fun(Diagnostic diagnostic) {
|
||||
return DefaultErrorMessages.RENDERER.render(diagnostic);
|
||||
return DefaultErrorMessages.render(diagnostic);
|
||||
}
|
||||
},
|
||||
"\n"));
|
||||
|
||||
Reference in New Issue
Block a user