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) {
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);
}
}
}
@@ -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),
@@ -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);
}
}
}
@@ -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);
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")
@@ -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
@@ -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);
}
}
}
@@ -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);
}
}