use of com.intellij.openapi.editor.colors.TextAttributesScheme in project intellij-community by JetBrains.
the class HighlightVisitorImpl method visitIdentifier.
@Override
public void visitIdentifier(final PsiIdentifier identifier) {
TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
PsiElement parent = identifier.getParent();
if (parent instanceof PsiVariable) {
PsiVariable variable = (PsiVariable) parent;
myHolder.add(HighlightUtil.checkVariableAlreadyDefined(variable));
if (variable.getInitializer() == null) {
final PsiElement child = variable.getLastChild();
if (child instanceof PsiErrorElement && child.getPrevSibling() == identifier)
return;
}
boolean isMethodParameter = variable instanceof PsiParameter && ((PsiParameter) variable).getDeclarationScope() instanceof PsiMethod;
if (isMethodParameter) {
// mark param as present in current file
myReassignedParameters.put((PsiParameter) variable, 1);
} else {
// method params are highlighted in visitMethod since we should make sure the method body was visited before
if (HighlightControlFlowUtil.isReassigned(variable, myFinalVarProblems)) {
myHolder.add(HighlightNamesUtil.highlightReassignedVariable(variable, identifier));
} else {
myHolder.add(HighlightNamesUtil.highlightVariableName(variable, identifier, colorsScheme));
}
}
} else if (parent instanceof PsiClass) {
PsiClass aClass = (PsiClass) parent;
if (aClass.isAnnotationType()) {
myHolder.add(checkFeature(identifier, Feature.ANNOTATIONS));
}
myHolder.add(HighlightClassUtil.checkClassAlreadyImported(aClass, identifier));
if (!(parent instanceof PsiAnonymousClass) && aClass.getNameIdentifier() == identifier) {
myHolder.add(HighlightNamesUtil.highlightClassName(aClass, identifier, colorsScheme));
}
if (!myHolder.hasErrorResults() && myLanguageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
myHolder.add(GenericsHighlightUtil.checkUnrelatedDefaultMethods(aClass, identifier));
}
if (!myHolder.hasErrorResults()) {
myHolder.add(GenericsHighlightUtil.checkUnrelatedConcrete(aClass, identifier));
}
} else if (parent instanceof PsiMethod) {
PsiMethod method = (PsiMethod) parent;
if (method.isConstructor()) {
myHolder.add(HighlightMethodUtil.checkConstructorName(method));
}
myHolder.add(HighlightNamesUtil.highlightMethodName(method, identifier, true, colorsScheme));
final PsiClass aClass = method.getContainingClass();
if (aClass != null) {
myHolder.add(GenericsHighlightUtil.checkDefaultMethodOverrideEquivalentToObjectNonPrivate(myLanguageLevel, aClass, method, identifier));
}
}
myHolder.add(HighlightUtil.checkUnderscore(identifier, myLanguageLevel));
super.visitIdentifier(identifier);
}
use of com.intellij.openapi.editor.colors.TextAttributesScheme in project intellij-community by JetBrains.
the class HighlightVisitorImpl method doVisitReferenceElement.
private JavaResolveResult doVisitReferenceElement(@NotNull PsiJavaCodeReferenceElement ref) {
JavaResolveResult result = resolveOptimised(ref);
if (result == null)
return null;
PsiElement resolved = result.getElement();
PsiElement parent = ref.getParent();
if (myRefCountHolder != null) {
myRefCountHolder.registerReference(ref, result);
}
myHolder.add(HighlightUtil.checkReference(ref, result, myFile, myLanguageLevel));
if (parent instanceof PsiJavaCodeReferenceElement || ref.isQualified()) {
if (!myHolder.hasErrorResults() && resolved instanceof PsiTypeParameter) {
boolean cannotSelectFromTypeParameter = !myJavaSdkVersion.isAtLeast(JavaSdkVersion.JDK_1_7);
if (!cannotSelectFromTypeParameter) {
final PsiClass containingClass = PsiTreeUtil.getParentOfType(ref, PsiClass.class);
if (containingClass != null) {
if (PsiTreeUtil.isAncestor(containingClass.getExtendsList(), ref, false) || PsiTreeUtil.isAncestor(containingClass.getImplementsList(), ref, false)) {
cannotSelectFromTypeParameter = true;
}
}
}
if (cannotSelectFromTypeParameter) {
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).descriptionAndTooltip("Cannot select from a type parameter").range(ref).create());
}
}
}
if (!myHolder.hasErrorResults())
myHolder.add(HighlightClassUtil.checkAbstractInstantiation(ref));
if (!myHolder.hasErrorResults())
myHolder.add(HighlightClassUtil.checkExtendsDuplicate(ref, resolved, myFile));
if (!myHolder.hasErrorResults())
myHolder.add(HighlightClassUtil.checkClassExtendsForeignInnerClass(ref, resolved));
if (!myHolder.hasErrorResults())
myHolder.add(GenericsHighlightUtil.checkSelectStaticClassFromParameterizedType(resolved, ref));
if (!myHolder.hasErrorResults()) {
myHolder.add(GenericsHighlightUtil.checkParameterizedReferenceTypeArguments(resolved, ref, result.getSubstitutor(), myJavaSdkVersion));
}
if (!myHolder.hasErrorResults())
myHolder.add(GenericsHighlightUtil.checkCannotPassInner(ref));
if (resolved != null && parent instanceof PsiReferenceList) {
if (!myHolder.hasErrorResults()) {
PsiReferenceList referenceList = (PsiReferenceList) parent;
myHolder.add(HighlightUtil.checkElementInReferenceList(ref, referenceList, result));
}
}
if (parent instanceof PsiAnonymousClass && ref.equals(((PsiAnonymousClass) parent).getBaseClassReference()) && myOverrideEquivalentMethodsVisitedClasses.add((PsiClass) parent)) {
PsiClass aClass = (PsiClass) parent;
myHolder.addAll(GenericsHighlightUtil.checkOverrideEquivalentMethods(aClass));
}
if (resolved instanceof PsiVariable) {
PsiVariable variable = (PsiVariable) resolved;
PsiElement containingClass = PsiTreeUtil.getNonStrictParentOfType(ref, PsiClass.class, PsiLambdaExpression.class);
if ((containingClass instanceof PsiAnonymousClass || containingClass instanceof PsiLambdaExpression) && !PsiTreeUtil.isAncestor(containingClass, variable, false) && !(variable instanceof PsiField) && (containingClass instanceof PsiLambdaExpression || !PsiTreeUtil.isAncestor(((PsiAnonymousClass) containingClass).getArgumentList(), ref, false))) {
myHolder.add(HighlightInfo.newHighlightInfo(JavaHighlightInfoTypes.IMPLICIT_ANONYMOUS_CLASS_PARAMETER).range(ref).create());
}
if (variable instanceof PsiParameter && ref instanceof PsiExpression && PsiUtil.isAccessedForWriting((PsiExpression) ref)) {
myReassignedParameters.put((PsiParameter) variable, 2);
}
final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
if (!variable.hasModifierProperty(PsiModifier.FINAL) && isReassigned(variable)) {
myHolder.add(HighlightNamesUtil.highlightReassignedVariable(variable, ref));
} else {
PsiElement nameElement = ref.getReferenceNameElement();
if (nameElement != null) {
myHolder.add(HighlightNamesUtil.highlightVariableName(variable, nameElement, colorsScheme));
}
}
myHolder.add(HighlightNamesUtil.highlightClassNameInQualifier(ref, colorsScheme));
} else {
highlightReferencedMethodOrClassName(ref, resolved);
}
if (parent instanceof PsiNewExpression && !(resolved instanceof PsiClass) && resolved instanceof PsiNamedElement && ((PsiNewExpression) parent).getClassOrAnonymousClassReference() == ref) {
String text = JavaErrorMessages.message("cannot.resolve.symbol", ((PsiNamedElement) resolved).getName());
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(text).create());
}
if (!myHolder.hasErrorResults() && resolved instanceof PsiClass) {
final PsiClass aClass = ((PsiClass) resolved).getContainingClass();
if (aClass != null) {
final PsiElement qualifier = ref.getQualifier();
final PsiElement place;
if (qualifier instanceof PsiJavaCodeReferenceElement) {
place = ((PsiJavaCodeReferenceElement) qualifier).resolve();
} else {
if (parent instanceof PsiNewExpression) {
final PsiExpression newQualifier = ((PsiNewExpression) parent).getQualifier();
place = newQualifier == null ? ref : PsiUtil.resolveClassInType(newQualifier.getType());
} else {
place = ref;
}
}
if (place != null && PsiTreeUtil.isAncestor(aClass, place, false) && aClass.hasTypeParameters()) {
myHolder.add(HighlightClassUtil.checkCreateInnerClassFromStaticContext(ref, place, (PsiClass) resolved));
}
} else if (resolved instanceof PsiTypeParameter) {
final PsiTypeParameterListOwner owner = ((PsiTypeParameter) resolved).getOwner();
if (owner instanceof PsiClass) {
final PsiClass outerClass = (PsiClass) owner;
if (!InheritanceUtil.hasEnclosingInstanceInScope(outerClass, ref, false, false)) {
myHolder.add(HighlightClassUtil.reportIllegalEnclosingUsage(ref, null, (PsiClass) owner, ref));
}
}
}
}
if (!myHolder.hasErrorResults())
myHolder.add(HighlightUtil.checkPackageAndClassConflict(ref, myFile));
return result;
}
use of com.intellij.openapi.editor.colors.TextAttributesScheme in project intellij-community by JetBrains.
the class HighlightVisitorImpl method visitDocTagValue.
@Override
public void visitDocTagValue(PsiDocTagValue value) {
PsiReference reference = value.getReference();
if (reference != null) {
PsiElement element = reference.resolve();
final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
if (element instanceof PsiMethod) {
PsiElement nameElement = ((PsiDocMethodOrFieldRef) value).getNameElement();
if (nameElement != null) {
myHolder.add(HighlightNamesUtil.highlightMethodName((PsiMethod) element, nameElement, false, colorsScheme));
}
} else if (element instanceof PsiParameter) {
myHolder.add(HighlightNamesUtil.highlightVariableName((PsiVariable) element, value.getNavigationElement(), colorsScheme));
}
}
}
use of com.intellij.openapi.editor.colors.TextAttributesScheme in project intellij-community by JetBrains.
the class HighlightVisitorImpl method visitImportStaticReferenceElement.
@Override
public void visitImportStaticReferenceElement(@NotNull PsiImportStaticReferenceElement ref) {
final String refName = ref.getReferenceName();
final JavaResolveResult[] results = ref.multiResolve(false);
final PsiElement referenceNameElement = ref.getReferenceNameElement();
if (results.length == 0) {
final String description = JavaErrorMessages.message("cannot.resolve.symbol", refName);
assert referenceNameElement != null : ref;
final HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(referenceNameElement).descriptionAndTooltip(description).create();
QuickFixAction.registerQuickFixAction(info, QuickFixFactory.getInstance().createSetupJDKFix());
myHolder.add(info);
} else {
final PsiManager manager = ref.getManager();
for (JavaResolveResult result : results) {
final PsiElement element = result.getElement();
String description = null;
if (element instanceof PsiClass) {
final Pair<PsiImportStaticReferenceElement, PsiClass> imported = mySingleImportedClasses.get(refName);
final PsiClass aClass = imported == null ? null : imported.getSecond();
if (aClass != null && !manager.areElementsEquivalent(aClass, element)) {
//noinspection ConditionalExpressionWithIdenticalBranches
description = imported.first == null ? JavaErrorMessages.message("single.import.class.conflict", refName) : imported.first.equals(ref) ? JavaErrorMessages.message("class.is.ambiguous.in.single.static.import", refName) : JavaErrorMessages.message("class.is.already.defined.in.single.static.import", refName);
}
mySingleImportedClasses.put(refName, Pair.create(ref, (PsiClass) element));
} else if (element instanceof PsiField) {
final Pair<PsiImportStaticReferenceElement, PsiField> imported = mySingleImportedFields.get(refName);
final PsiField field = imported == null ? null : imported.getSecond();
if (field != null && !manager.areElementsEquivalent(field, element)) {
//noinspection ConditionalExpressionWithIdenticalBranches
description = imported.first.equals(ref) ? JavaErrorMessages.message("field.is.ambiguous.in.single.static.import", refName) : JavaErrorMessages.message("field.is.already.defined.in.single.static.import", refName);
}
mySingleImportedFields.put(refName, Pair.create(ref, (PsiField) element));
}
if (description != null) {
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(description).create());
}
}
}
if (!myHolder.hasErrorResults()) {
PsiElement resolved = results.length >= 1 ? results[0].getElement() : null;
if (results.length > 1) {
for (int i = 1; i < results.length; i++) {
final PsiElement element = results[i].getElement();
if (resolved instanceof PsiMethod && !(element instanceof PsiMethod) || resolved instanceof PsiVariable && !(element instanceof PsiVariable) || resolved instanceof PsiClass && !(element instanceof PsiClass)) {
resolved = null;
break;
}
}
}
final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
if (resolved instanceof PsiClass) {
myHolder.add(HighlightNamesUtil.highlightClassName((PsiClass) resolved, ref, colorsScheme));
} else {
myHolder.add(HighlightNamesUtil.highlightClassNameInQualifier(ref, colorsScheme));
if (referenceNameElement != null) {
if (resolved instanceof PsiVariable) {
myHolder.add(HighlightNamesUtil.highlightVariableName((PsiVariable) resolved, referenceNameElement, colorsScheme));
} else if (resolved instanceof PsiMethod) {
myHolder.add(HighlightNamesUtil.highlightMethodName((PsiMethod) resolved, referenceNameElement, false, colorsScheme));
}
}
}
}
}
use of com.intellij.openapi.editor.colors.TextAttributesScheme in project intellij-community by JetBrains.
the class HighlightVisitorImpl method visitMethodReferenceExpression.
@Override
public void visitMethodReferenceExpression(PsiMethodReferenceExpression expression) {
myHolder.add(checkFeature(expression, Feature.METHOD_REFERENCES));
final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
if (parent instanceof PsiExpressionStatement)
return;
final JavaResolveResult result;
final JavaResolveResult[] results;
try {
results = expression.multiResolve(true);
result = results.length == 1 ? results[0] : JavaResolveResult.EMPTY;
} catch (IndexNotReadyException e) {
return;
}
if (myRefCountHolder != null) {
myRefCountHolder.registerReference(expression, result);
}
final PsiElement method = result.getElement();
if (method != null && !result.isAccessible()) {
final String accessProblem = HighlightUtil.buildProblemWithAccessDescription(expression, result);
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(accessProblem).create();
HighlightUtil.registerAccessQuickFixAction((PsiMember) method, expression, info, result.getCurrentFileResolveScope());
myHolder.add(info);
} else {
final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
if (method instanceof PsiMethod && !expression.isConstructor()) {
PsiElement methodNameElement = expression.getReferenceNameElement();
if (methodNameElement != null) {
myHolder.add(HighlightNamesUtil.highlightMethodName((PsiMethod) method, methodNameElement, false, colorsScheme));
}
}
myHolder.add(HighlightNamesUtil.highlightClassNameInQualifier(expression, colorsScheme));
}
if (!LambdaUtil.isValidLambdaContext(parent)) {
String description = "Method reference expression is not expected here";
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create());
}
final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
if (!myHolder.hasErrorResults()) {
if (functionalInterfaceType != null) {
final boolean notFunctional = !LambdaUtil.isFunctionalType(functionalInterfaceType);
if (notFunctional) {
String description = functionalInterfaceType.getPresentableText() + " is not a functional interface";
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create());
}
}
}
if (!myHolder.hasErrorResults()) {
final PsiElement referenceNameElement = expression.getReferenceNameElement();
if (referenceNameElement instanceof PsiKeyword) {
if (!PsiMethodReferenceUtil.isValidQualifier(expression)) {
PsiElement qualifier = expression.getQualifier();
if (qualifier != null) {
String description = "Cannot find class " + qualifier.getText();
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(qualifier).descriptionAndTooltip(description).create());
}
}
}
}
if (!myHolder.hasErrorResults()) {
checkFunctionalInterfaceTypeAccessible(expression, functionalInterfaceType);
}
if (!myHolder.hasErrorResults() && functionalInterfaceType != null) {
final String errorMessage = PsiMethodReferenceUtil.checkMethodReferenceContext(expression);
if (errorMessage != null) {
final HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(errorMessage).create();
if (method instanceof PsiMethod && !((PsiMethod) method).isConstructor() && !((PsiMethod) method).hasModifierProperty(PsiModifier.ABSTRACT)) {
final boolean shouldHave = !((PsiMethod) method).hasModifierProperty(PsiModifier.STATIC);
final LocalQuickFixAndIntentionActionOnPsiElement fixStaticModifier = QuickFixFactory.getInstance().createModifierListFix((PsiModifierListOwner) method, PsiModifier.STATIC, shouldHave, false);
QuickFixAction.registerQuickFixAction(info, fixStaticModifier);
}
myHolder.add(info);
}
}
if (!myHolder.hasErrorResults()) {
PsiElement qualifier = expression.getQualifier();
if (qualifier instanceof PsiTypeElement) {
final PsiType psiType = ((PsiTypeElement) qualifier).getType();
final HighlightInfo genericArrayCreationInfo = GenericsHighlightUtil.checkGenericArrayCreation(qualifier, psiType);
if (genericArrayCreationInfo != null) {
myHolder.add(genericArrayCreationInfo);
} else {
final String wildcardMessage = PsiMethodReferenceUtil.checkTypeArguments((PsiTypeElement) qualifier, psiType);
if (wildcardMessage != null) {
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(qualifier).descriptionAndTooltip(wildcardMessage).create());
}
}
}
}
if (!myHolder.hasErrorResults()) {
myHolder.add(PsiMethodReferenceHighlightingUtil.checkRawConstructorReference(expression));
}
if (!myHolder.hasErrorResults()) {
myHolder.add(HighlightUtil.checkUnhandledExceptions(expression, expression.getTextRange()));
}
if (!myHolder.hasErrorResults()) {
final String badReturnTypeMessage = PsiMethodReferenceUtil.checkReturnType(expression, result, functionalInterfaceType);
if (badReturnTypeMessage != null) {
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(badReturnTypeMessage).create());
}
}
if (!myHolder.hasErrorResults()) {
if (results.length == 0 || results[0] instanceof MethodCandidateInfo && !((MethodCandidateInfo) results[0]).isApplicable() && functionalInterfaceType != null) {
String description = null;
if (results.length == 1) {
description = ((MethodCandidateInfo) results[0]).getInferenceErrorMessage();
}
if (expression.isConstructor()) {
final PsiClass containingClass = PsiMethodReferenceUtil.getQualifierResolveResult(expression).getContainingClass();
if (containingClass != null) {
if (!myHolder.add(HighlightClassUtil.checkInstantiationOfAbstractClass(containingClass, expression)) && !myHolder.add(GenericsHighlightUtil.checkEnumInstantiation(expression, containingClass)) && containingClass.isPhysical() && description == null) {
description = JavaErrorMessages.message("cannot.resolve.constructor", containingClass.getName());
}
}
} else if (description == null) {
description = JavaErrorMessages.message("cannot.resolve.method", expression.getReferenceName());
}
if (description != null) {
final PsiElement referenceNameElement = expression.getReferenceNameElement();
final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(results.length == 0 ? HighlightInfoType.WRONG_REF : HighlightInfoType.ERROR).descriptionAndTooltip(description).range(referenceNameElement).create();
myHolder.add(highlightInfo);
final TextRange fixRange = HighlightMethodUtil.getFixRange(referenceNameElement);
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, QuickFixFactory.getInstance().createCreateMethodFromUsageFix(expression));
}
}
}
}
Aggregations