From 011e2675ab67c500e37b01c18ad5f1ebe3a7d57d Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 22 Oct 2013 20:02:50 +0400 Subject: [PATCH] Refactoring: store information about registered redeclaration errors for element --- .../jet/plugin/highlighter/JetPsiChecker.java | 234 +++++++++--------- 1 file changed, 121 insertions(+), 113 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java b/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java index 3a63d319998..e59e3f162b1 100644 --- a/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java @@ -26,7 +26,6 @@ import com.intellij.lang.annotation.Annotator; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.progress.ProcessCanceledException; -import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.TextRange; import com.intellij.psi.MultiRangeReference; import com.intellij.psi.PsiElement; @@ -116,9 +115,9 @@ public class JetPsiChecker implements Annotator, HighlightRangeExtension { } if (JetPluginUtil.isInSource(element, /* includeLibrarySources = */ false)) { - Ref isMarkedWithRedeclaration = Ref.create(false); + ElementAnnotator elementAnnotator = new ElementAnnotator(element, holder); for (Diagnostic diagnostic : bindingContext.getDiagnostics().forElement(element)) { - registerDiagnosticAnnotations(element, diagnostic, holder, isMarkedWithRedeclaration); + elementAnnotator.registerDiagnosticAnnotations(diagnostic); } } @@ -133,141 +132,150 @@ public class JetPsiChecker implements Annotator, HighlightRangeExtension { return file instanceof JetFile; } - private static void registerDiagnosticAnnotations( - @NotNull PsiElement element, @NotNull Diagnostic diagnostic, - @NotNull AnnotationHolder holder, Ref isMarkedWithRedeclaration - ) { - if (!diagnostic.isValid()) return; + private static class ElementAnnotator { + @NotNull private final PsiElement element; + @NotNull private final AnnotationHolder holder; - assert diagnostic.getPsiElement() == element; + private boolean isMarkedWithRedeclaration; - List textRanges = diagnostic.getTextRanges(); - if (diagnostic.getSeverity() == Severity.ERROR) { - if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(diagnostic.getFactory())) { - JetReferenceExpression referenceExpression = (JetReferenceExpression)diagnostic.getPsiElement(); - PsiReference reference = referenceExpression.getReference(); - if (reference instanceof MultiRangeReference) { - MultiRangeReference mrr = (MultiRangeReference)reference; - for (TextRange range : mrr.getRanges()) { - Annotation annotation = holder.createErrorAnnotation(range.shiftRight(referenceExpression.getTextOffset()), getDefaultMessage(diagnostic)); - setUpAnnotation(diagnostic, annotation, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL); + ElementAnnotator(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { + this.element = element; + this.holder = holder; + } + + void registerDiagnosticAnnotations(@NotNull Diagnostic diagnostic) { + if (!diagnostic.isValid()) return; + + assert diagnostic.getPsiElement() == element; + + List textRanges = diagnostic.getTextRanges(); + if (diagnostic.getSeverity() == Severity.ERROR) { + if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(diagnostic.getFactory())) { + JetReferenceExpression referenceExpression = (JetReferenceExpression)diagnostic.getPsiElement(); + PsiReference reference = referenceExpression.getReference(); + if (reference instanceof MultiRangeReference) { + MultiRangeReference mrr = (MultiRangeReference)reference; + for (TextRange range : mrr.getRanges()) { + Annotation annotation = holder.createErrorAnnotation(range.shiftRight(referenceExpression.getTextOffset()), getDefaultMessage(diagnostic)); + setUpAnnotation(diagnostic, annotation, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL); + } } + else { + for (TextRange textRange : textRanges) { + Annotation annotation = holder.createErrorAnnotation(textRange, getDefaultMessage(diagnostic)); + setUpAnnotation(diagnostic, annotation, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL); + } + } + + return; } - else { - for (TextRange textRange : textRanges) { + + if (diagnostic.getFactory() == Errors.ILLEGAL_ESCAPE) { + for (TextRange textRange : diagnostic.getTextRanges()) { Annotation annotation = holder.createErrorAnnotation(textRange, getDefaultMessage(diagnostic)); - setUpAnnotation(diagnostic, annotation, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL); + annotation.setTooltip(getMessage(diagnostic)); + annotation.setTextAttributes(JetHighlightingColors.INVALID_STRING_ESCAPE); } + return; } - return; - } - - if (diagnostic.getFactory() == Errors.ILLEGAL_ESCAPE) { - for (TextRange textRange : diagnostic.getTextRanges()) { - Annotation annotation = holder.createErrorAnnotation(textRange, getDefaultMessage(diagnostic)); - annotation.setTooltip(getMessage(diagnostic)); - annotation.setTextAttributes(JetHighlightingColors.INVALID_STRING_ESCAPE); + if (!isMarkedWithRedeclaration && Errors.REDECLARATION_DIAGNOSTICS.contains(diagnostic.getFactory())) { + isMarkedWithRedeclaration = true; + Annotation annotation = holder.createErrorAnnotation(diagnostic.getTextRanges().get(0), ""); + setUpAnnotation(diagnostic, annotation, null); + return; } - return; - } - if (!isMarkedWithRedeclaration.get() && Errors.REDECLARATION_DIAGNOSTICS.contains(diagnostic.getFactory())) { - isMarkedWithRedeclaration.set(true); - Annotation annotation = holder.createErrorAnnotation(diagnostic.getTextRanges().get(0), ""); - setUpAnnotation(diagnostic, annotation, null); - return; - } - - // Generic annotation - for (TextRange textRange : textRanges) { - Annotation errorAnnotation = holder.createErrorAnnotation(textRange, getDefaultMessage(diagnostic)); - setUpAnnotation(diagnostic, errorAnnotation, - diagnostic.getFactory() == Errors.INVISIBLE_REFERENCE + // Generic annotation + for (TextRange textRange : textRanges) { + Annotation errorAnnotation = holder.createErrorAnnotation(textRange, getDefaultMessage(diagnostic)); + setUpAnnotation(diagnostic, errorAnnotation, + diagnostic.getFactory() == Errors.INVISIBLE_REFERENCE ? ProblemHighlightType.LIKE_UNKNOWN_SYMBOL : null); + } } - } - else if (diagnostic.getSeverity() == Severity.WARNING) { - for (TextRange textRange : textRanges) { - Annotation annotation = holder.createWarningAnnotation(textRange, getDefaultMessage(diagnostic)); - setUpAnnotation(diagnostic, annotation, - Errors.UNUSED_ELEMENT_DIAGNOSTICS.contains(diagnostic.getFactory()) + else if (diagnostic.getSeverity() == Severity.WARNING) { + for (TextRange textRange : textRanges) { + Annotation annotation = holder.createWarningAnnotation(textRange, getDefaultMessage(diagnostic)); + setUpAnnotation(diagnostic, annotation, + Errors.UNUSED_ELEMENT_DIAGNOSTICS.contains(diagnostic.getFactory()) ? ProblemHighlightType.LIKE_UNUSED_SYMBOL : null); - } - } - } - - private static void setUpAnnotation(Diagnostic diagnostic, Annotation annotation, @Nullable ProblemHighlightType highlightType) { - annotation.setTooltip(getMessage(diagnostic)); - registerQuickFix(annotation, diagnostic); - - if (highlightType != null) { - annotation.setHighlightType(highlightType); - } - } - - /* - * Add a quick fix if and return modified annotation. - */ - @Nullable - private static Annotation registerQuickFix(@Nullable Annotation annotation, @NotNull Diagnostic diagnostic) { - if (annotation == null) { - return null; - } - - Collection intentionActionsFactories = QuickFixes.getActionsFactories(diagnostic.getFactory()); - for (JetIntentionActionsFactory intentionActionsFactory : intentionActionsFactories) { - if (intentionActionsFactory != null) { - for (IntentionAction action: intentionActionsFactory.createActions(diagnostic)) { - annotation.registerFix(action); } } } - Collection actions = QuickFixes.getActions(diagnostic.getFactory()); - for (IntentionAction action : actions) { - annotation.registerFix(action); - } + private static void setUpAnnotation(Diagnostic diagnostic, Annotation annotation, @Nullable ProblemHighlightType highlightType) { + annotation.setTooltip(getMessage(diagnostic)); + registerQuickFix(annotation, diagnostic); - // Making warnings suppressable - if (diagnostic.getSeverity() == Severity.WARNING) { - annotation.setProblemGroup(new KotlinSuppressableWarningProblemGroup(diagnostic.getFactory())); - - List fixes = annotation.getQuickFixes(); - if (fixes == null || fixes.isEmpty()) { - // if there are no quick fixes we need to register an EmptyIntentionAction to enable 'suppress' actions - annotation.registerFix(new EmptyIntentionAction(diagnostic.getFactory().getName())); + if (highlightType != null) { + annotation.setHighlightType(highlightType); } } - return annotation; - } - - @NotNull - private static String getMessage(@NotNull Diagnostic diagnostic) { - String message = IdeErrorMessages.RENDERER.render(diagnostic); - if (ApplicationManager.getApplication().isInternal() || ApplicationManager.getApplication().isUnitTestMode()) { - String factoryName = diagnostic.getFactory().getName(); - if (message.startsWith("")) { - message = String.format("[%s] %s", factoryName, message.substring("".length())); - } else { - message = String.format("[%s] %s", factoryName, message); + /* + * Add a quick fix if and return modified annotation. + */ + @Nullable + private static Annotation registerQuickFix(@Nullable Annotation annotation, @NotNull Diagnostic diagnostic) { + if (annotation == null) { + return null; } - } - if (!message.startsWith("")) { - message = "" + XmlStringUtil.escapeString(message) + ""; - } - return message; - } - @NotNull - private static String getDefaultMessage(@NotNull Diagnostic diagnostic) { - String message = DefaultErrorMessages.RENDERER.render(diagnostic); - if (ApplicationManager.getApplication().isInternal() || ApplicationManager.getApplication().isUnitTestMode()) { - return String.format("[%s] %s", diagnostic.getFactory().getName(), message); + Collection intentionActionsFactories = QuickFixes.getActionsFactories(diagnostic.getFactory()); + for (JetIntentionActionsFactory intentionActionsFactory : intentionActionsFactories) { + if (intentionActionsFactory != null) { + for (IntentionAction action: intentionActionsFactory.createActions(diagnostic)) { + annotation.registerFix(action); + } + } + } + + Collection actions = QuickFixes.getActions(diagnostic.getFactory()); + for (IntentionAction action : actions) { + annotation.registerFix(action); + } + + // Making warnings suppressable + if (diagnostic.getSeverity() == Severity.WARNING) { + annotation.setProblemGroup(new KotlinSuppressableWarningProblemGroup(diagnostic.getFactory())); + + List fixes = annotation.getQuickFixes(); + if (fixes == null || fixes.isEmpty()) { + // if there are no quick fixes we need to register an EmptyIntentionAction to enable 'suppress' actions + annotation.registerFix(new EmptyIntentionAction(diagnostic.getFactory().getName())); + } + } + + return annotation; + } + + @NotNull + private static String getMessage(@NotNull Diagnostic diagnostic) { + String message = IdeErrorMessages.RENDERER.render(diagnostic); + if (ApplicationManager.getApplication().isInternal() || ApplicationManager.getApplication().isUnitTestMode()) { + String factoryName = diagnostic.getFactory().getName(); + if (message.startsWith("")) { + message = String.format("[%s] %s", factoryName, message.substring("".length())); + } else { + message = String.format("[%s] %s", factoryName, message); + } + } + if (!message.startsWith("")) { + message = "" + XmlStringUtil.escapeString(message) + ""; + } + return message; + } + + @NotNull + private static String getDefaultMessage(@NotNull Diagnostic diagnostic) { + String message = DefaultErrorMessages.RENDERER.render(diagnostic); + if (ApplicationManager.getApplication().isInternal() || ApplicationManager.getApplication().isUnitTestMode()) { + return String.format("[%s] %s", diagnostic.getFactory().getName(), message); + } + return message; } - return message; } }