Java: improve error highlighting for unknown annotation method

GitOrigin-RevId: 6fdb6e2f397280cf4795cbd09ddcc1461a5897ff
This commit is contained in:
Bas Leijdekkers
2024-07-30 16:42:57 +02:00
committed by intellij-monorepo-bot
parent 1bdb332af2
commit a35a9f91ba
6 changed files with 17 additions and 28 deletions

View File

@@ -71,25 +71,18 @@ public final class AnnotationsHighlightUtil {
if (ref == null) return null;
PsiMethod method = (PsiMethod)ref.resolve();
if (method == null) {
if (pair.getName() != null) {
String description = JavaErrorBundle.message("annotation.unknown.method", ref.getCanonicalText());
HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF)
.range(ref.getElement(), ref.getRangeInElement())
.descriptionAndTooltip(description);
IntentionAction action = QuickFixFactory.getInstance().createCreateAnnotationMethodFromUsageFix(pair);
builder.registerFix(action, null, null, null, null);
return builder;
}
else {
String description = JavaErrorBundle.message("annotation.missing.method", ref.getCanonicalText());
PsiElement element = ref.getElement();
HighlightInfo.Builder highlightInfo =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element).descriptionAndTooltip(description);
boolean noName = pair.getName() == null;
String description = JavaErrorBundle.message("annotation.unknown.method", ref.getCanonicalText());
HighlightInfo.Builder highlightInfo = HighlightInfo.newHighlightInfo(noName ? HighlightInfoType.ERROR : HighlightInfoType.WRONG_REF)
.range(ref.getElement())
.descriptionAndTooltip(description);
if (noName) {
for (IntentionAction action : QuickFixFactory.getInstance().createAddAnnotationAttributeNameFixes(pair)) {
highlightInfo.registerFix(action, null, null, null, null);
}
return highlightInfo;
}
highlightInfo.registerFix(QuickFixFactory.getInstance().createCreateAnnotationMethodFromUsageFix(pair), null, null, null, null);
return highlightInfo;
}
else {
PsiType returnType = method.getReturnType();

View File

@@ -1038,14 +1038,11 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitNameValuePair(@NotNull PsiNameValuePair pair) {
add(AnnotationsHighlightUtil.checkNameValuePair(pair));
if (!hasErrorResults()) {
PsiIdentifier nameId = pair.getNameIdentifier();
if (nameId != null) {
HighlightInfo.Builder result = HighlightInfo.newHighlightInfo(JavaHighlightInfoTypes.ANNOTATION_ATTRIBUTE_NAME).range(nameId);
add(result);
}
PsiIdentifier nameId = pair.getNameIdentifier();
if (nameId != null) {
add(HighlightInfo.newHighlightInfo(JavaHighlightInfoTypes.ANNOTATION_ATTRIBUTE_NAME).range(nameId));
}
add(AnnotationsHighlightUtil.checkNameValuePair(pair));
}
@Override