KT-2111 Highlight Java classes/interfaces/etc in Kotlin code
#KT-2111 fixed
This commit is contained in:
@@ -83,7 +83,6 @@ public class JetPsiChecker implements Annotator {
|
|||||||
return new HighlightingVisitor[]{
|
return new HighlightingVisitor[]{
|
||||||
new SoftKeywordsHighlightingVisitor(holder),
|
new SoftKeywordsHighlightingVisitor(holder),
|
||||||
new LabelsHighlightingVisitor(holder),
|
new LabelsHighlightingVisitor(holder),
|
||||||
new TypeKindHighlightingVisitor(holder),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,6 +91,7 @@ public class JetPsiChecker implements Annotator {
|
|||||||
new PropertiesHighlightingVisitor(holder, bindingContext),
|
new PropertiesHighlightingVisitor(holder, bindingContext),
|
||||||
new FunctionsHighlightingVisitor(holder, bindingContext),
|
new FunctionsHighlightingVisitor(holder, bindingContext),
|
||||||
new VariablesHighlightingVisitor(holder, bindingContext),
|
new VariablesHighlightingVisitor(holder, bindingContext),
|
||||||
|
new TypeKindHighlightingVisitor(holder, bindingContext),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,54 +21,36 @@ import com.intellij.openapi.editor.colors.TextAttributesKey;
|
|||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiElement;
|
||||||
import com.intellij.psi.PsiReference;
|
import com.intellij.psi.PsiReference;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.lang.descriptors.*;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
import org.jetbrains.jet.lexer.JetTokens;
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Evgeny Gerashchenko
|
* @author Evgeny Gerashchenko
|
||||||
* @since 3/29/12
|
* @since 3/29/12
|
||||||
*/
|
*/
|
||||||
class TypeKindHighlightingVisitor extends HighlightingVisitor {
|
class TypeKindHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
|
||||||
protected TypeKindHighlightingVisitor(AnnotationHolder holder) {
|
TypeKindHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) {
|
||||||
super(holder);
|
super(holder, bindingContext);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitAnnotationEntry(JetAnnotationEntry annotationEntry) {
|
|
||||||
JetTypeReference typeReference = annotationEntry.getTypeReference();
|
|
||||||
if (typeReference == null) return;
|
|
||||||
JetTypeElement typeElement = typeReference.getTypeElement();
|
|
||||||
if (!(typeElement instanceof JetUserType)) return;
|
|
||||||
JetUserType userType = (JetUserType)typeElement;
|
|
||||||
if (userType.getQualifier() != null) return;
|
|
||||||
JetSimpleNameExpression referenceExpression = userType.getReferenceExpression();
|
|
||||||
if (referenceExpression != null) {
|
|
||||||
holder.createInfoAnnotation(referenceExpression.getNode(), null).setTextAttributes(JetHighlightingColors.ANNOTATION);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void visitNameExpression(JetExpression expression) {
|
|
||||||
PsiReference ref = expression.getReference();
|
|
||||||
if (ref == null) return;
|
|
||||||
if (JetPsiChecker.isNamesHighlightingEnabled()) {
|
|
||||||
PsiElement target = ref.resolve();
|
|
||||||
if (target instanceof JetClass) {
|
|
||||||
highlightClassByKind((JetClass)target, expression);
|
|
||||||
}
|
|
||||||
else if (target instanceof JetTypeParameter) {
|
|
||||||
JetPsiChecker.highlightName(holder, expression, JetHighlightingColors.TYPE_PARAMETER);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitSimpleNameExpression(JetSimpleNameExpression expression) {
|
public void visitSimpleNameExpression(JetSimpleNameExpression expression) {
|
||||||
visitNameExpression(expression);
|
PsiReference ref = expression.getReference();
|
||||||
|
if (ref == null) return;
|
||||||
|
if (JetPsiChecker.isNamesHighlightingEnabled()) {
|
||||||
|
DeclarationDescriptor referenceTarget = bindingContext.get(BindingContext.REFERENCE_TARGET, expression);
|
||||||
|
if (referenceTarget instanceof ConstructorDescriptor) {
|
||||||
|
referenceTarget = referenceTarget.getContainingDeclaration();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
if (referenceTarget instanceof ClassDescriptor) {
|
||||||
public void visitQualifiedExpression(JetQualifiedExpression expression) {
|
highlightClassByKind((ClassDescriptor) referenceTarget, expression);
|
||||||
visitNameExpression(expression);
|
}
|
||||||
|
else if (referenceTarget instanceof TypeParameterDescriptor) {
|
||||||
|
JetPsiChecker.highlightName(holder, expression, JetHighlightingColors.TYPE_PARAMETER);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -77,31 +59,32 @@ class TypeKindHighlightingVisitor extends HighlightingVisitor {
|
|||||||
if (identifier != null) {
|
if (identifier != null) {
|
||||||
JetPsiChecker.highlightName(holder, identifier, JetHighlightingColors.TYPE_PARAMETER);
|
JetPsiChecker.highlightName(holder, identifier, JetHighlightingColors.TYPE_PARAMETER);
|
||||||
}
|
}
|
||||||
|
super.visitTypeParameter(parameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitClass(JetClass klass) {
|
public void visitClass(JetClass klass) {
|
||||||
PsiElement identifier = klass.getNameIdentifier();
|
PsiElement identifier = klass.getNameIdentifier();
|
||||||
if (identifier != null) {
|
ClassDescriptor classDescriptor = bindingContext.get(BindingContext.CLASS, klass);
|
||||||
highlightClassByKind(klass, identifier);
|
if (identifier != null && classDescriptor != null) {
|
||||||
|
highlightClassByKind(classDescriptor, identifier);
|
||||||
}
|
}
|
||||||
|
super.visitClass(klass);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void highlightClassByKind(@NotNull JetClass klass, @NotNull PsiElement whatToHighlight) {
|
private void highlightClassByKind(@NotNull ClassDescriptor classDescriptor, @NotNull PsiElement whatToHighlight) {
|
||||||
TextAttributesKey textAttributes = JetHighlightingColors.CLASS;
|
TextAttributesKey textAttributes;
|
||||||
if (klass.isTrait()) {
|
switch (classDescriptor.getKind()) {
|
||||||
|
case TRAIT:
|
||||||
textAttributes = JetHighlightingColors.TRAIT;
|
textAttributes = JetHighlightingColors.TRAIT;
|
||||||
}
|
break;
|
||||||
else {
|
case ANNOTATION_CLASS:
|
||||||
JetModifierList modifierList = klass.getModifierList();
|
|
||||||
if (modifierList != null) {
|
|
||||||
if (modifierList.hasModifier(JetTokens.ANNOTATION_KEYWORD)) {
|
|
||||||
textAttributes = JetHighlightingColors.ANNOTATION;
|
textAttributes = JetHighlightingColors.ANNOTATION;
|
||||||
}
|
break;
|
||||||
else if (modifierList.hasModifier(JetTokens.ABSTRACT_KEYWORD)) {
|
default:
|
||||||
textAttributes = JetHighlightingColors.ABSTRACT_CLASS;
|
textAttributes = classDescriptor.getModality() == Modality.ABSTRACT
|
||||||
}
|
? JetHighlightingColors.ABSTRACT_CLASS
|
||||||
}
|
: JetHighlightingColors.CLASS;
|
||||||
}
|
}
|
||||||
JetPsiChecker.highlightName(holder, whatToHighlight, textAttributes);
|
JetPsiChecker.highlightName(holder, whatToHighlight, textAttributes);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
<info textAttributesKey="KOTLIN_ANNOTATION">Override</info> class <info textAttributesKey="KOTLIN_CLASS">TheClass</info> : <info textAttributesKey="KOTLIN_TRAIT">Runnable</info>, <info textAttributesKey="KOTLIN_CLASS"><info textAttributesKey="KOTLIN_CONSTRUCTOR">Thread</info></info>() {
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.jet.plugin;
|
package org.jetbrains.jet.plugin;
|
||||||
|
|
||||||
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
|
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
|
||||||
|
import com.intellij.openapi.projectRoots.Sdk;
|
||||||
import org.jetbrains.jet.plugin.highlighter.JetPsiChecker;
|
import org.jetbrains.jet.plugin.highlighter.JetPsiChecker;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,6 +41,15 @@ public class NamesHighlightingTest extends LightDaemonAnalyzerTestCase {
|
|||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testJavaTypes() throws Exception {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Sdk getProjectJDK() {
|
||||||
|
return PluginTestCaseBase.jdkFromIdeaHome();
|
||||||
|
}
|
||||||
|
|
||||||
private void doTest() throws Exception {
|
private void doTest() throws Exception {
|
||||||
doTest(getTestName(false) + ".kt", false, true);
|
doTest(getTestName(false) + ".kt", false, true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user