Made UnresolvedReferenceDiagnostic subtype of DiagnosticWithParameters1 to simplify diagnostic renderer.

This commit is contained in:
Evgeny Gerashchenko
2012-04-13 15:46:28 +04:00
parent d1f35cfd9c
commit 545e559d5b
3 changed files with 23 additions and 45 deletions
@@ -16,38 +16,15 @@
package org.jetbrains.jet.lang.diagnostics;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetArrayAccessExpression;
import org.jetbrains.jet.lang.psi.JetReferenceExpression;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.jet.lang.diagnostics.Severity.ERROR;
/**
* @author abreslav
*/
public class UnresolvedReferenceDiagnostic extends AbstractDiagnostic<JetReferenceExpression> {
@NotNull
@Override
public UnresolvedReferenceDiagnosticFactory getFactory() {
return (UnresolvedReferenceDiagnosticFactory)super.getFactory();
}
public class UnresolvedReferenceDiagnostic extends DiagnosticWithParameters1<JetReferenceExpression, String> {
public UnresolvedReferenceDiagnostic(@NotNull JetReferenceExpression referenceExpression,
@NotNull UnresolvedReferenceDiagnosticFactory factory) {
super(referenceExpression, factory, ERROR);
}
@NotNull
@Override
public List<TextRange> getTextRanges() {
JetReferenceExpression element = getPsiElement();
if (element instanceof JetArrayAccessExpression) {
return ((JetArrayAccessExpression) element).getBracketRanges();
}
return Collections.singletonList(element.getTextRange());
@NotNull UnresolvedReferenceDiagnosticFactory factory, @NotNull Severity severity) {
super(referenceExpression, referenceExpression.getText(), factory, severity);
}
}
@@ -16,18 +16,33 @@
package org.jetbrains.jet.lang.diagnostics;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetArrayAccessExpression;
import org.jetbrains.jet.lang.psi.JetReferenceExpression;
import java.util.Collections;
import java.util.List;
/**
* @author abreslav
*/
public class UnresolvedReferenceDiagnosticFactory extends AbstractDiagnosticFactory {
private UnresolvedReferenceDiagnosticFactory() {
public class UnresolvedReferenceDiagnosticFactory extends DiagnosticFactory1<JetReferenceExpression, String> {
public UnresolvedReferenceDiagnosticFactory() {
super(Severity.ERROR, new PositioningStrategy<JetReferenceExpression>() {
@NotNull
@Override
public List<TextRange> mark(@NotNull JetReferenceExpression element) {
if (element instanceof JetArrayAccessExpression) {
return ((JetArrayAccessExpression) element).getBracketRanges();
}
return Collections.singletonList(element.getTextRange());
}
});
}
public UnresolvedReferenceDiagnostic on(@NotNull JetReferenceExpression reference) {
return new UnresolvedReferenceDiagnostic(reference, this);
return new UnresolvedReferenceDiagnostic(reference, this, severity);
}
public static UnresolvedReferenceDiagnosticFactory create() {
@@ -53,7 +53,7 @@ public class DefaultDiagnosticRenderer implements DiagnosticRenderer<Diagnostic>
}
}));
map.put(UNRESOLVED_REFERENCE, new UnresolvedReferenceDiagnosticRenderer("Unresolved reference: "));
map.put(UNRESOLVED_REFERENCE, new DiagnosticWithParameters1Renderer<String>("Unresolved reference: {0}", TO_STRING));
map.put(INVISIBLE_REFERENCE,
new DiagnosticWithParameters2Renderer<DeclarationDescriptor, DeclarationDescriptor>("Cannot access ''{0}'' in ''{1}''",
@@ -124,7 +124,7 @@ public class DefaultDiagnosticRenderer implements DiagnosticRenderer<Diagnostic>
map.put(MIXING_NAMED_AND_POSITIONED_ARGUMENTS,
new SimpleDiagnosticRenderer("Mixing named and positioned arguments in not allowed"));
map.put(ARGUMENT_PASSED_TWICE, new SimpleDiagnosticRenderer("An argument is already passed for this parameter"));
map.put(NAMED_PARAMETER_NOT_FOUND, new UnresolvedReferenceDiagnosticRenderer("Cannot find a parameter with this name: "));
map.put(NAMED_PARAMETER_NOT_FOUND, new DiagnosticWithParameters1Renderer<String>("Cannot find a parameter with this name: {0}", TO_STRING));
map.put(VARARG_OUTSIDE_PARENTHESES,
new SimpleDiagnosticRenderer("Passing value as a vararg is only allowed inside a parenthesized argument list"));
map.put(NON_VARARG_SPREAD, new SimpleDiagnosticRenderer("The spread operator (*foo) may only be applied in a vararg position"));
@@ -529,20 +529,6 @@ public class DefaultDiagnosticRenderer implements DiagnosticRenderer<Diagnostic>
}
}
private static class UnresolvedReferenceDiagnosticRenderer implements DiagnosticRenderer<UnresolvedReferenceDiagnostic> {
private String messagePrefix;
private UnresolvedReferenceDiagnosticRenderer(@NotNull String messagePrefix) {
this.messagePrefix = messagePrefix;
}
@NotNull
@Override
public String render(@NotNull UnresolvedReferenceDiagnostic diagnostic) {
return messagePrefix + diagnostic.getPsiElement().getText();
}
}
private static class AmbiguousDescriptorDiagnosticRenderer
extends DiagnosticWithParameters1Renderer<Collection<? extends ResolvedCall<? extends CallableDescriptor>>> {
private AmbiguousDescriptorDiagnosticRenderer(@NotNull String message) {