From 573738c87e5f16b806fd383bcd4c9bc5a072ce8e Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 9 Oct 2015 13:53:08 +0300 Subject: [PATCH] Refactoring: extract name highlighting to separate class --- .../FunctionsHighlightingVisitor.java | 16 +++---- .../kotlin/idea/highlighter/JetPsiChecker.kt | 17 -------- .../LabelsHighlightingVisitor.java | 2 +- .../idea/highlighter/NameHighlighter.kt | 42 +++++++++++++++++++ .../PropertiesHighlightingVisitor.java | 10 ++--- .../TypeKindHighlightingVisitor.java | 6 +-- .../VariablesHighlightingVisitor.java | 8 ++-- .../checkers/AbstractJetPsiCheckerTest.java | 6 +-- 8 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/NameHighlighter.kt diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.java index c56d83c0595..19dbb66b3f7 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.java @@ -40,7 +40,7 @@ public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisit public void visitNamedFunction(@NotNull JetNamedFunction function) { PsiElement nameIdentifier = function.getNameIdentifier(); if (nameIdentifier != null) { - JetPsiChecker.highlightName(holder, nameIdentifier, JetHighlightingColors.FUNCTION_DECLARATION); + NameHighlighter.highlightName(holder, nameIdentifier, JetHighlightingColors.FUNCTION_DECLARATION); } super.visitNamedFunction(function); @@ -55,7 +55,7 @@ public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisit if (typeElement instanceof JetUserType) { JetSimpleNameExpression nameExpression = ((JetUserType)typeElement).getReferenceExpression(); if (nameExpression != null) { - JetPsiChecker.highlightName(holder, nameExpression, JetHighlightingColors.CONSTRUCTOR_CALL); + NameHighlighter.highlightName(holder, nameExpression, JetHighlightingColors.CONSTRUCTOR_CALL); } } } @@ -70,25 +70,25 @@ public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisit CallableDescriptor calleeDescriptor = resolvedCall.getResultingDescriptor(); if (DynamicCallsKt.isDynamic(calleeDescriptor)) { - JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.DYNAMIC_FUNCTION_CALL); + NameHighlighter.highlightName(holder, callee, JetHighlightingColors.DYNAMIC_FUNCTION_CALL); } else if (resolvedCall instanceof VariableAsFunctionResolvedCall) { - JetPsiChecker.highlightName(holder, callee, containedInFunctionClassOrSubclass(calleeDescriptor) + NameHighlighter.highlightName(holder, callee, containedInFunctionClassOrSubclass(calleeDescriptor) ? JetHighlightingColors.VARIABLE_AS_FUNCTION_CALL : JetHighlightingColors.VARIABLE_AS_FUNCTION_LIKE_CALL); } else { if (calleeDescriptor instanceof ConstructorDescriptor) { - JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.CONSTRUCTOR_CALL); + NameHighlighter.highlightName(holder, callee, JetHighlightingColors.CONSTRUCTOR_CALL); } else if (calleeDescriptor instanceof FunctionDescriptor) { FunctionDescriptor fun = (FunctionDescriptor) calleeDescriptor; - JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.FUNCTION_CALL); + NameHighlighter.highlightName(holder, callee, JetHighlightingColors.FUNCTION_CALL); if (DescriptorUtils.isTopLevelDeclaration(fun)) { - JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.PACKAGE_FUNCTION_CALL); + NameHighlighter.highlightName(holder, callee, JetHighlightingColors.PACKAGE_FUNCTION_CALL); } if (fun.getExtensionReceiverParameter() != null) { - JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.EXTENSION_FUNCTION_CALL); + NameHighlighter.highlightName(holder, callee, JetHighlightingColors.EXTENSION_FUNCTION_CALL); } } } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/JetPsiChecker.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/JetPsiChecker.kt index 2527c4669c8..2764006df01 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/JetPsiChecker.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/JetPsiChecker.kt @@ -219,23 +219,6 @@ public open class JetPsiChecker : Annotator, HighlightRangeExtension { } companion object { - var namesHighlightingEnabled = true - @TestOnly set - - @JvmStatic - fun highlightName(holder: AnnotationHolder, psiElement: PsiElement, attributesKey: TextAttributesKey) { - if (namesHighlightingEnabled) { - holder.createInfoAnnotation(psiElement, null).setTextAttributes(attributesKey) - } - } - - @JvmStatic - fun highlightName(holder: AnnotationHolder, textRange: TextRange, attributesKey: TextAttributesKey) { - if (namesHighlightingEnabled) { - holder.createInfoAnnotation(textRange, null).setTextAttributes(attributesKey) - } - } - private fun getAfterAnalysisVisitor(holder: AnnotationHolder, bindingContext: BindingContext) = arrayOf( PropertiesHighlightingVisitor(holder, bindingContext), FunctionsHighlightingVisitor(holder, bindingContext), diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/LabelsHighlightingVisitor.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/LabelsHighlightingVisitor.java index 78263d417aa..71edda42f4e 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/LabelsHighlightingVisitor.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/LabelsHighlightingVisitor.java @@ -33,7 +33,7 @@ class LabelsHighlightingVisitor extends JetVisitorVoid { public void visitExpressionWithLabel(@NotNull JetExpressionWithLabel expression) { JetSimpleNameExpression targetLabel = expression.getTargetLabel(); if (targetLabel != null) { - JetPsiChecker.highlightName(holder, targetLabel, JetHighlightingColors.LABEL); + NameHighlighter.highlightName(holder, targetLabel, JetHighlightingColors.LABEL); } } } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/NameHighlighter.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/NameHighlighter.kt new file mode 100644 index 00000000000..9971817ff7b --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/NameHighlighter.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.highlighter + +import com.intellij.lang.annotation.AnnotationHolder +import com.intellij.openapi.editor.colors.TextAttributesKey +import com.intellij.openapi.util.TextRange +import com.intellij.psi.PsiElement +import org.jetbrains.annotations.TestOnly + +object NameHighlighter { + public var namesHighlightingEnabled = true + @TestOnly set + + @JvmStatic + fun highlightName(holder: AnnotationHolder, psiElement: PsiElement, attributesKey: TextAttributesKey) { + if (namesHighlightingEnabled) { + holder.createInfoAnnotation(psiElement, null).setTextAttributes(attributesKey) + } + } + + @JvmStatic + fun highlightName(holder: AnnotationHolder, textRange: TextRange, attributesKey: TextAttributesKey) { + if (namesHighlightingEnabled) { + holder.createInfoAnnotation(textRange, null).setTextAttributes(attributesKey) + } + } +} \ No newline at end of file diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/PropertiesHighlightingVisitor.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/PropertiesHighlightingVisitor.java index a56f46c42a6..44cac62acee 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/PropertiesHighlightingVisitor.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/PropertiesHighlightingVisitor.java @@ -44,7 +44,7 @@ class PropertiesHighlightingVisitor extends AfterAnalysisHighlightingVisitor { } DeclarationDescriptor target = bindingContext.get(BindingContext.REFERENCE_TARGET, expression); if (target instanceof SyntheticFieldDescriptor) { - JetPsiChecker.highlightName(holder, expression, JetHighlightingColors.BACKING_FIELD_VARIABLE); + NameHighlighter.highlightName(holder, expression, JetHighlightingColors.BACKING_FIELD_VARIABLE); return; } if (!(target instanceof PropertyDescriptor)) { @@ -53,7 +53,7 @@ class PropertiesHighlightingVisitor extends AfterAnalysisHighlightingVisitor { highlightProperty(expression, (PropertyDescriptor) target, false); if (expression.getReferencedNameElementType() == JetTokens.FIELD_IDENTIFIER) { - JetPsiChecker.highlightName(holder, expression, JetHighlightingColors.BACKING_FIELD_ACCESS); + NameHighlighter.highlightName(holder, expression, JetHighlightingColors.BACKING_FIELD_ACCESS); } } @@ -89,17 +89,17 @@ class PropertiesHighlightingVisitor extends AfterAnalysisHighlightingVisitor { boolean withBackingField ) { if (DynamicCallsKt.isDynamic(descriptor)) { - JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.DYNAMIC_PROPERTY_CALL); + NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.DYNAMIC_PROPERTY_CALL); return; } boolean isStatic = DescriptorUtils.isStaticDeclaration(descriptor); - JetPsiChecker.highlightName( + NameHighlighter.highlightName( holder, elementToHighlight, isStatic ? JetHighlightingColors.PACKAGE_PROPERTY : JetHighlightingColors.INSTANCE_PROPERTY ); if (descriptor.getExtensionReceiverParameter() != null) { - JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.EXTENSION_PROPERTY); + NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.EXTENSION_PROPERTY); } if (withBackingField) { holder.createInfoAnnotation(elementToHighlight, "This property has a backing field") diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.java index 8e8d652106e..5b930e92b6c 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.java @@ -36,7 +36,7 @@ class TypeKindHighlightingVisitor extends AfterAnalysisHighlightingVisitor { @Override public void visitSimpleNameExpression(@NotNull JetSimpleNameExpression expression) { - if (JetPsiChecker.Companion.getNamesHighlightingEnabled()) { + if (NameHighlighter.namesHighlightingEnabled) { DeclarationDescriptor referenceTarget = bindingContext.get(BindingContext.REFERENCE_TARGET, expression); if (referenceTarget instanceof ConstructorDescriptor) { referenceTarget = referenceTarget.getContainingDeclaration(); @@ -59,7 +59,7 @@ class TypeKindHighlightingVisitor extends AfterAnalysisHighlightingVisitor { private void highlightAnnotation(@NotNull JetSimpleNameExpression expression) { TextRange toHighlight = JetPsiUtilKt.getCalleeHighlightingRange(expression); - JetPsiChecker.highlightName(holder, toHighlight, JetHighlightingColors.ANNOTATION); + NameHighlighter.highlightName(holder, toHighlight, JetHighlightingColors.ANNOTATION); } @Override @@ -87,7 +87,7 @@ class TypeKindHighlightingVisitor extends AfterAnalysisHighlightingVisitor { } private void highlightName(@NotNull PsiElement whatToHighlight, @NotNull TextAttributesKey textAttributesKey) { - JetPsiChecker.highlightName(holder, whatToHighlight, textAttributesKey); + NameHighlighter.highlightName(holder, whatToHighlight, textAttributesKey); } @NotNull diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/VariablesHighlightingVisitor.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/VariablesHighlightingVisitor.java index 8930701aeeb..a9a8a4e4ab3 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/VariablesHighlightingVisitor.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/VariablesHighlightingVisitor.java @@ -90,12 +90,12 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor { VariableDescriptor variableDescriptor = (VariableDescriptor) descriptor; if (DynamicCallsKt.isDynamic(variableDescriptor)) { - JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.DYNAMIC_PROPERTY_CALL); + NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.DYNAMIC_PROPERTY_CALL); return; } if (variableDescriptor.isVar()) { - JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.MUTABLE_VARIABLE); + NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.MUTABLE_VARIABLE); } if (bindingContext.get(CAPTURED_IN_CLOSURE, variableDescriptor) == CaptureKind.NOT_INLINE) { @@ -106,11 +106,11 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor { } if (descriptor instanceof LocalVariableDescriptor) { - JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.LOCAL_VARIABLE); + NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.LOCAL_VARIABLE); } if (descriptor instanceof ValueParameterDescriptor) { - JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.PARAMETER); + NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.PARAMETER); } } } diff --git a/idea/tests/org/jetbrains/kotlin/checkers/AbstractJetPsiCheckerTest.java b/idea/tests/org/jetbrains/kotlin/checkers/AbstractJetPsiCheckerTest.java index 8976a0a57a0..3697dae5f33 100644 --- a/idea/tests/org/jetbrains/kotlin/checkers/AbstractJetPsiCheckerTest.java +++ b/idea/tests/org/jetbrains/kotlin/checkers/AbstractJetPsiCheckerTest.java @@ -20,8 +20,8 @@ import com.intellij.rt.execution.junit.FileComparisonFailure; import com.intellij.spellchecker.inspections.SpellCheckingInspection; import com.intellij.testFramework.LightProjectDescriptor; import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.idea.highlighter.NameHighlighter; import org.jetbrains.kotlin.idea.test.JetLightCodeInsightFixtureTestCase; -import org.jetbrains.kotlin.idea.highlighter.JetPsiChecker; import java.io.File; @@ -38,11 +38,11 @@ public abstract class AbstractJetPsiCheckerTest extends JetLightCodeInsightFixtu //noinspection unchecked myFixture.enableInspections(SpellCheckingInspection.class); - JetPsiChecker.Companion.setNamesHighlightingEnabled(false); + NameHighlighter.INSTANCE$.setNamesHighlightingEnabled(false); checkHighlighting(true, true, false); } finally { - JetPsiChecker.Companion.setNamesHighlightingEnabled(true); + NameHighlighter.INSTANCE$.setNamesHighlightingEnabled(true); } }