Refactoring: extract name highlighting to separate class

This commit is contained in:
Nikolay Krasko
2015-10-09 13:53:08 +03:00
parent 734d3f4fca
commit 573738c87e
8 changed files with 66 additions and 41 deletions
@@ -40,7 +40,7 @@ public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisit
public void visitNamedFunction(@NotNull JetNamedFunction function) { public void visitNamedFunction(@NotNull JetNamedFunction function) {
PsiElement nameIdentifier = function.getNameIdentifier(); PsiElement nameIdentifier = function.getNameIdentifier();
if (nameIdentifier != null) { if (nameIdentifier != null) {
JetPsiChecker.highlightName(holder, nameIdentifier, JetHighlightingColors.FUNCTION_DECLARATION); NameHighlighter.highlightName(holder, nameIdentifier, JetHighlightingColors.FUNCTION_DECLARATION);
} }
super.visitNamedFunction(function); super.visitNamedFunction(function);
@@ -55,7 +55,7 @@ public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisit
if (typeElement instanceof JetUserType) { if (typeElement instanceof JetUserType) {
JetSimpleNameExpression nameExpression = ((JetUserType)typeElement).getReferenceExpression(); JetSimpleNameExpression nameExpression = ((JetUserType)typeElement).getReferenceExpression();
if (nameExpression != null) { 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(); CallableDescriptor calleeDescriptor = resolvedCall.getResultingDescriptor();
if (DynamicCallsKt.isDynamic(calleeDescriptor)) { 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) { 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_CALL
: JetHighlightingColors.VARIABLE_AS_FUNCTION_LIKE_CALL); : JetHighlightingColors.VARIABLE_AS_FUNCTION_LIKE_CALL);
} }
else { else {
if (calleeDescriptor instanceof ConstructorDescriptor) { if (calleeDescriptor instanceof ConstructorDescriptor) {
JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.CONSTRUCTOR_CALL); NameHighlighter.highlightName(holder, callee, JetHighlightingColors.CONSTRUCTOR_CALL);
} }
else if (calleeDescriptor instanceof FunctionDescriptor) { else if (calleeDescriptor instanceof FunctionDescriptor) {
FunctionDescriptor fun = (FunctionDescriptor) calleeDescriptor; FunctionDescriptor fun = (FunctionDescriptor) calleeDescriptor;
JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.FUNCTION_CALL); NameHighlighter.highlightName(holder, callee, JetHighlightingColors.FUNCTION_CALL);
if (DescriptorUtils.isTopLevelDeclaration(fun)) { if (DescriptorUtils.isTopLevelDeclaration(fun)) {
JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.PACKAGE_FUNCTION_CALL); NameHighlighter.highlightName(holder, callee, JetHighlightingColors.PACKAGE_FUNCTION_CALL);
} }
if (fun.getExtensionReceiverParameter() != null) { if (fun.getExtensionReceiverParameter() != null) {
JetPsiChecker.highlightName(holder, callee, JetHighlightingColors.EXTENSION_FUNCTION_CALL); NameHighlighter.highlightName(holder, callee, JetHighlightingColors.EXTENSION_FUNCTION_CALL);
} }
} }
} }
@@ -219,23 +219,6 @@ public open class JetPsiChecker : Annotator, HighlightRangeExtension {
} }
companion object { 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( private fun getAfterAnalysisVisitor(holder: AnnotationHolder, bindingContext: BindingContext) = arrayOf(
PropertiesHighlightingVisitor(holder, bindingContext), PropertiesHighlightingVisitor(holder, bindingContext),
FunctionsHighlightingVisitor(holder, bindingContext), FunctionsHighlightingVisitor(holder, bindingContext),
@@ -33,7 +33,7 @@ class LabelsHighlightingVisitor extends JetVisitorVoid {
public void visitExpressionWithLabel(@NotNull JetExpressionWithLabel expression) { public void visitExpressionWithLabel(@NotNull JetExpressionWithLabel expression) {
JetSimpleNameExpression targetLabel = expression.getTargetLabel(); JetSimpleNameExpression targetLabel = expression.getTargetLabel();
if (targetLabel != null) { if (targetLabel != null) {
JetPsiChecker.highlightName(holder, targetLabel, JetHighlightingColors.LABEL); NameHighlighter.highlightName(holder, targetLabel, JetHighlightingColors.LABEL);
} }
} }
} }
@@ -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)
}
}
}
@@ -44,7 +44,7 @@ class PropertiesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
} }
DeclarationDescriptor target = bindingContext.get(BindingContext.REFERENCE_TARGET, expression); DeclarationDescriptor target = bindingContext.get(BindingContext.REFERENCE_TARGET, expression);
if (target instanceof SyntheticFieldDescriptor) { if (target instanceof SyntheticFieldDescriptor) {
JetPsiChecker.highlightName(holder, expression, JetHighlightingColors.BACKING_FIELD_VARIABLE); NameHighlighter.highlightName(holder, expression, JetHighlightingColors.BACKING_FIELD_VARIABLE);
return; return;
} }
if (!(target instanceof PropertyDescriptor)) { if (!(target instanceof PropertyDescriptor)) {
@@ -53,7 +53,7 @@ class PropertiesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
highlightProperty(expression, (PropertyDescriptor) target, false); highlightProperty(expression, (PropertyDescriptor) target, false);
if (expression.getReferencedNameElementType() == JetTokens.FIELD_IDENTIFIER) { 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 boolean withBackingField
) { ) {
if (DynamicCallsKt.isDynamic(descriptor)) { if (DynamicCallsKt.isDynamic(descriptor)) {
JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.DYNAMIC_PROPERTY_CALL); NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.DYNAMIC_PROPERTY_CALL);
return; return;
} }
boolean isStatic = DescriptorUtils.isStaticDeclaration(descriptor); boolean isStatic = DescriptorUtils.isStaticDeclaration(descriptor);
JetPsiChecker.highlightName( NameHighlighter.highlightName(
holder, elementToHighlight, holder, elementToHighlight,
isStatic ? JetHighlightingColors.PACKAGE_PROPERTY : JetHighlightingColors.INSTANCE_PROPERTY isStatic ? JetHighlightingColors.PACKAGE_PROPERTY : JetHighlightingColors.INSTANCE_PROPERTY
); );
if (descriptor.getExtensionReceiverParameter() != null) { if (descriptor.getExtensionReceiverParameter() != null) {
JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.EXTENSION_PROPERTY); NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.EXTENSION_PROPERTY);
} }
if (withBackingField) { if (withBackingField) {
holder.createInfoAnnotation(elementToHighlight, "This property has a backing field") holder.createInfoAnnotation(elementToHighlight, "This property has a backing field")
@@ -36,7 +36,7 @@ class TypeKindHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
@Override @Override
public void visitSimpleNameExpression(@NotNull JetSimpleNameExpression expression) { public void visitSimpleNameExpression(@NotNull JetSimpleNameExpression expression) {
if (JetPsiChecker.Companion.getNamesHighlightingEnabled()) { if (NameHighlighter.namesHighlightingEnabled) {
DeclarationDescriptor referenceTarget = bindingContext.get(BindingContext.REFERENCE_TARGET, expression); DeclarationDescriptor referenceTarget = bindingContext.get(BindingContext.REFERENCE_TARGET, expression);
if (referenceTarget instanceof ConstructorDescriptor) { if (referenceTarget instanceof ConstructorDescriptor) {
referenceTarget = referenceTarget.getContainingDeclaration(); referenceTarget = referenceTarget.getContainingDeclaration();
@@ -59,7 +59,7 @@ class TypeKindHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
private void highlightAnnotation(@NotNull JetSimpleNameExpression expression) { private void highlightAnnotation(@NotNull JetSimpleNameExpression expression) {
TextRange toHighlight = JetPsiUtilKt.getCalleeHighlightingRange(expression); TextRange toHighlight = JetPsiUtilKt.getCalleeHighlightingRange(expression);
JetPsiChecker.highlightName(holder, toHighlight, JetHighlightingColors.ANNOTATION); NameHighlighter.highlightName(holder, toHighlight, JetHighlightingColors.ANNOTATION);
} }
@Override @Override
@@ -87,7 +87,7 @@ class TypeKindHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
} }
private void highlightName(@NotNull PsiElement whatToHighlight, @NotNull TextAttributesKey textAttributesKey) { private void highlightName(@NotNull PsiElement whatToHighlight, @NotNull TextAttributesKey textAttributesKey) {
JetPsiChecker.highlightName(holder, whatToHighlight, textAttributesKey); NameHighlighter.highlightName(holder, whatToHighlight, textAttributesKey);
} }
@NotNull @NotNull
@@ -90,12 +90,12 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
VariableDescriptor variableDescriptor = (VariableDescriptor) descriptor; VariableDescriptor variableDescriptor = (VariableDescriptor) descriptor;
if (DynamicCallsKt.isDynamic(variableDescriptor)) { if (DynamicCallsKt.isDynamic(variableDescriptor)) {
JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.DYNAMIC_PROPERTY_CALL); NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.DYNAMIC_PROPERTY_CALL);
return; return;
} }
if (variableDescriptor.isVar()) { 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) { if (bindingContext.get(CAPTURED_IN_CLOSURE, variableDescriptor) == CaptureKind.NOT_INLINE) {
@@ -106,11 +106,11 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
} }
if (descriptor instanceof LocalVariableDescriptor) { if (descriptor instanceof LocalVariableDescriptor) {
JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.LOCAL_VARIABLE); NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.LOCAL_VARIABLE);
} }
if (descriptor instanceof ValueParameterDescriptor) { if (descriptor instanceof ValueParameterDescriptor) {
JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.PARAMETER); NameHighlighter.highlightName(holder, elementToHighlight, JetHighlightingColors.PARAMETER);
} }
} }
} }
@@ -20,8 +20,8 @@ import com.intellij.rt.execution.junit.FileComparisonFailure;
import com.intellij.spellchecker.inspections.SpellCheckingInspection; import com.intellij.spellchecker.inspections.SpellCheckingInspection;
import com.intellij.testFramework.LightProjectDescriptor; import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.highlighter.NameHighlighter;
import org.jetbrains.kotlin.idea.test.JetLightCodeInsightFixtureTestCase; import org.jetbrains.kotlin.idea.test.JetLightCodeInsightFixtureTestCase;
import org.jetbrains.kotlin.idea.highlighter.JetPsiChecker;
import java.io.File; import java.io.File;
@@ -38,11 +38,11 @@ public abstract class AbstractJetPsiCheckerTest extends JetLightCodeInsightFixtu
//noinspection unchecked //noinspection unchecked
myFixture.enableInspections(SpellCheckingInspection.class); myFixture.enableInspections(SpellCheckingInspection.class);
JetPsiChecker.Companion.setNamesHighlightingEnabled(false); NameHighlighter.INSTANCE$.setNamesHighlightingEnabled(false);
checkHighlighting(true, true, false); checkHighlighting(true, true, false);
} }
finally { finally {
JetPsiChecker.Companion.setNamesHighlightingEnabled(true); NameHighlighter.INSTANCE$.setNamesHighlightingEnabled(true);
} }
} }