Made RedeclarationDiagnostic subtype of DiagnosticWithParameters1 to simplify diagnostic renderer.

This commit is contained in:
Evgeny Gerashchenko
2012-04-13 16:07:47 +04:00
parent 9f3f309625
commit 4d05b6757c
3 changed files with 34 additions and 63 deletions
@@ -27,48 +27,9 @@ import java.util.List;
/**
* @author abreslav
*/
public class RedeclarationDiagnostic extends AbstractDiagnostic<PsiElement> {
private String name;
public RedeclarationDiagnostic(@NotNull PsiElement psiElement, @NotNull String name, RedeclarationDiagnosticFactory factory) {
super(psiElement, factory, factory.severity);
this.name = name;
public class RedeclarationDiagnostic extends DiagnosticWithParameters1<PsiElement, String> {
public RedeclarationDiagnostic(@NotNull PsiElement psiElement, @NotNull String name,
@NotNull RedeclarationDiagnosticFactory factory, @NotNull Severity severity) {
super(psiElement, name, factory, severity);
}
@NotNull
@Override
public RedeclarationDiagnosticFactory getFactory() {
return (RedeclarationDiagnosticFactory)super.getFactory();
}
public String getName() {
return name;
}
@NotNull
@Override
public List<TextRange> getTextRanges() {
return POSITION_REDECLARATION.mark(getPsiElement());
}
private static final PositioningStrategy<PsiElement> POSITION_REDECLARATION = new PositioningStrategy<PsiElement>() {
@NotNull
@Override
public List<TextRange> mark(@NotNull PsiElement element) {
if (element instanceof JetNamedDeclaration) {
PsiElement nameIdentifier = ((JetNamedDeclaration) element).getNameIdentifier();
if (nameIdentifier != null) {
return markElement(nameIdentifier);
}
}
else if (element instanceof JetFile) {
JetFile file = (JetFile) element;
PsiElement nameIdentifier = file.getNamespaceHeader().getNameIdentifier();
if (nameIdentifier != null) {
return markElement(nameIdentifier);
}
}
return markElement(element);
}
};
}
@@ -16,20 +16,44 @@
package org.jetbrains.jet.lang.diagnostics;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamedDeclaration;
import java.util.List;
/**
* @author abreslav
*/
public class RedeclarationDiagnosticFactory extends AbstractDiagnosticFactory {
final Severity severity;
public class RedeclarationDiagnosticFactory extends DiagnosticFactory1<PsiElement, String> {
private static final PositioningStrategy<PsiElement> POSITION_REDECLARATION = new PositioningStrategy<PsiElement>() {
@NotNull
@Override
public List<TextRange> mark(@NotNull PsiElement element) {
if (element instanceof JetNamedDeclaration) {
PsiElement nameIdentifier = ((JetNamedDeclaration) element).getNameIdentifier();
if (nameIdentifier != null) {
return markElement(nameIdentifier);
}
}
else if (element instanceof JetFile) {
JetFile file = (JetFile) element;
PsiElement nameIdentifier = file.getNamespaceHeader().getNameIdentifier();
if (nameIdentifier != null) {
return markElement(nameIdentifier);
}
}
return markElement(element);
}
};
public RedeclarationDiagnosticFactory(Severity severity) {
this.severity = severity;
super(severity, POSITION_REDECLARATION);
}
public RedeclarationDiagnostic on(@NotNull PsiElement duplicatingElement, @NotNull String name) {
return new RedeclarationDiagnostic(duplicatingElement, name, this);
return new RedeclarationDiagnostic(duplicatingElement, name, this, severity);
}
}
@@ -62,8 +62,8 @@ public class DefaultDiagnosticRenderer implements DiagnosticRenderer<Diagnostic>
new DiagnosticWithParameters2Renderer<DeclarationDescriptor, DeclarationDescriptor>("Cannot access ''{0}'' in ''{1}''",
NAME, NAME));
map.put(REDECLARATION, new RedeclarationDiagnosticRenderer("Redeclaration: "));
map.put(NAME_SHADOWING, new RedeclarationDiagnosticRenderer("Name shadowed: "));
map.put(REDECLARATION, new DiagnosticWithParameters1Renderer<String>("Redeclaration: {0}", NAME));
map.put(NAME_SHADOWING, new DiagnosticWithParameters1Renderer<String>("Name shadowed: {0}", NAME));
map.put(TYPE_MISMATCH,
new DiagnosticWithParameters2Renderer<JetType, JetType>("Type mismatch: inferred type is {1} but {0} was expected",
@@ -515,20 +515,6 @@ public class DefaultDiagnosticRenderer implements DiagnosticRenderer<Diagnostic>
return renderer.render(diagnostic);
}
private static class RedeclarationDiagnosticRenderer implements DiagnosticRenderer<RedeclarationDiagnostic> {
private String messagePrefix;
private RedeclarationDiagnosticRenderer(@NotNull String messagePrefix) {
this.messagePrefix = messagePrefix;
}
@NotNull
@Override
public String render(@NotNull RedeclarationDiagnostic diagnostic) {
return messagePrefix + diagnostic.getName();
}
}
private static class AmbiguousDescriptorDiagnosticRenderer
extends DiagnosticWithParameters1Renderer<Collection<? extends ResolvedCall<? extends CallableDescriptor>>> {
private AmbiguousDescriptorDiagnosticRenderer(@NotNull String message) {