mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-highlighting] getIncreaseLanguageLevelFixes moved into HighlightFixUtil
Part of IDEA-365344 Create a new Java error highlighter with minimal dependencies (PSI only) GitOrigin-RevId: d505298b86d6f9ddf19003c7dbf4f93da43998a8
This commit is contained in:
committed by
intellij-monorepo-bot
parent
f58076f23f
commit
3ac1ccb7ea
+33
@@ -16,7 +16,10 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersion;
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersionUtil;
|
||||
import com.intellij.openapi.projectRoots.JavaVersionService;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.controlFlow.*;
|
||||
import com.intellij.psi.infos.CandidateInfo;
|
||||
@@ -865,6 +868,36 @@ public final class HighlightFixUtil {
|
||||
registerChangeParameterClassFix(lType, rType, sink);
|
||||
}
|
||||
|
||||
private static @NotNull LanguageLevel getApplicableLevel(@NotNull PsiFile file, @NotNull JavaFeature feature) {
|
||||
LanguageLevel standardLevel = feature.getStandardLevel();
|
||||
LanguageLevel featureLevel = feature.getMinimumLevel();
|
||||
if (featureLevel.isPreview()) {
|
||||
JavaSdkVersion sdkVersion = JavaSdkVersionUtil.getJavaSdkVersion(file);
|
||||
if (sdkVersion != null) {
|
||||
if (standardLevel != null && sdkVersion.isAtLeast(JavaSdkVersion.fromLanguageLevel(standardLevel))) {
|
||||
return standardLevel;
|
||||
}
|
||||
LanguageLevel previewLevel = sdkVersion.getMaxLanguageLevel().getPreviewLevel();
|
||||
if (previewLevel != null && previewLevel.isAtLeast(featureLevel)) {
|
||||
return previewLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
return featureLevel;
|
||||
}
|
||||
|
||||
public static @NotNull List<CommonIntentionAction> getIncreaseLanguageLevelFixes(
|
||||
@NotNull PsiElement element, @NotNull JavaFeature feature) {
|
||||
if (PsiUtil.isAvailable(feature, element)) return List.of();
|
||||
if (feature.isLimited()) return List.of(); //no reason for applying it because it can be outdated
|
||||
LanguageLevel applicableLevel = getApplicableLevel(element.getContainingFile(), feature);
|
||||
if (applicableLevel == LanguageLevel.JDK_X) return List.of(); // do not suggest to use experimental level
|
||||
QuickFixFactory factory = QuickFixFactory.getInstance();
|
||||
return List.of(factory.createIncreaseLanguageLevelFix(applicableLevel),
|
||||
factory.createUpgradeSdkFor(applicableLevel),
|
||||
factory.createShowModulePropertiesFix(element));
|
||||
}
|
||||
|
||||
private static final class ReturnModel {
|
||||
final PsiReturnStatement myStatement;
|
||||
final PsiType myType;
|
||||
|
||||
+4
-16
@@ -19,7 +19,6 @@ import com.intellij.openapi.module.LanguageLevelUtil;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersion;
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersionUtil;
|
||||
import com.intellij.openapi.roots.impl.FilePropertyPusher;
|
||||
import com.intellij.openapi.roots.impl.JavaLanguageLevelPusher;
|
||||
import com.intellij.openapi.util.*;
|
||||
@@ -539,26 +538,15 @@ public final class HighlightUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void registerIncreaseLanguageLevelFixes(@NotNull PsiElement element,
|
||||
@NotNull JavaFeature feature,
|
||||
HighlightInfo.Builder info) {
|
||||
static void registerIncreaseLanguageLevelFixes(@NotNull PsiElement element,
|
||||
@NotNull JavaFeature feature,
|
||||
HighlightInfo.Builder info) {
|
||||
if (info == null) return;
|
||||
for (CommonIntentionAction action : getIncreaseLanguageLevelFixes(element, feature)) {
|
||||
for (CommonIntentionAction action : HighlightFixUtil.getIncreaseLanguageLevelFixes(element, feature)) {
|
||||
info.registerFix(action.asIntention(), null, null, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
public static @NotNull List<CommonIntentionAction> getIncreaseLanguageLevelFixes(
|
||||
@NotNull PsiElement element, @NotNull JavaFeature feature) {
|
||||
if (PsiUtil.isAvailable(feature, element)) return List.of();
|
||||
if (feature.isLimited()) return List.of(); //no reason for applying it because it can be outdated
|
||||
LanguageLevel applicableLevel = getApplicableLevel(element.getContainingFile(), feature);
|
||||
if (applicableLevel == LanguageLevel.JDK_X) return List.of(); // do not suggest to use experimental level
|
||||
return List.of(getFixFactory().createIncreaseLanguageLevelFix(applicableLevel),
|
||||
getFixFactory().createUpgradeSdkFor(applicableLevel),
|
||||
getFixFactory().createShowModulePropertiesFix(element));
|
||||
}
|
||||
|
||||
private static @NotNull @NlsContexts.DetailedDescription String getUnsupportedFeatureMessage(@NotNull JavaFeature feature,
|
||||
@NotNull LanguageLevel level,
|
||||
@NotNull PsiFile file) {
|
||||
|
||||
+3
-3
@@ -97,8 +97,8 @@ final class JavaErrorFixProvider {
|
||||
}
|
||||
|
||||
JavaErrorFixProvider() {
|
||||
multi(UNSUPPORTED_FEATURE, error -> HighlightUtil.getIncreaseLanguageLevelFixes(error.psi(), error.context()));
|
||||
multi(PREVIEW_API_USAGE, error -> HighlightUtil.getIncreaseLanguageLevelFixes(error.psi(), error.context().feature()));
|
||||
multi(UNSUPPORTED_FEATURE, error -> HighlightFixUtil.getIncreaseLanguageLevelFixes(error.psi(), error.context()));
|
||||
multi(PREVIEW_API_USAGE, error -> HighlightFixUtil.getIncreaseLanguageLevelFixes(error.psi(), error.context().feature()));
|
||||
JavaFixProvider<PsiElement, Object> genericRemover = error -> myFactory.createDeleteFix(error.psi());
|
||||
for (JavaErrorKind<?, ?> kind : List.of(ANNOTATION_MEMBER_THROWS_NOT_ALLOWED, ANNOTATION_ATTRIBUTE_DUPLICATE,
|
||||
ANNOTATION_NOT_ALLOWED_EXTENDS, ANNOTATION_NOT_ALLOWED_IN_PERMIT_LIST,
|
||||
@@ -595,7 +595,7 @@ final class JavaErrorFixProvider {
|
||||
String text = QuickFixBundle.message("remove.modifier.fix", name, VisibilityUtil.toPresentableText(PsiModifier.ABSTRACT));
|
||||
return myFactory.createAddMethodBodyFix(method, text);
|
||||
});
|
||||
multi(CALL_CONSTRUCTOR_MUST_BE_FIRST_STATEMENT, error -> HighlightUtil.getIncreaseLanguageLevelFixes(
|
||||
multi(CALL_CONSTRUCTOR_MUST_BE_FIRST_STATEMENT, error -> HighlightFixUtil.getIncreaseLanguageLevelFixes(
|
||||
error.psi(), JavaFeature.STATEMENTS_BEFORE_SUPER));
|
||||
fix(STRING_TEMPLATE_PROCESSOR_MISSING, error -> new MissingStrProcessorFix(error.psi()));
|
||||
fixes(UNARY_OPERATOR_NOT_APPLICABLE, (error, sink) -> {
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@ public final class JavaFutureKeywordUseFixProvider extends UnresolvedReferenceQu
|
||||
private static void registerIncreaseLevelFixes(@NotNull PsiJavaCodeReferenceElement ref,
|
||||
@NotNull JavaFeature feature,
|
||||
@NotNull QuickFixActionRegistrar registrar) {
|
||||
for (CommonIntentionAction fix : HighlightUtil.getIncreaseLanguageLevelFixes(ref, feature)) {
|
||||
for (CommonIntentionAction fix : HighlightFixUtil.getIncreaseLanguageLevelFixes(ref, feature)) {
|
||||
registrar.register(fix.asIntention());
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ public final class SealedClassUnresolvedReferenceFixProvider extends UnresolvedR
|
||||
@Override
|
||||
public void registerFixes(@NotNull PsiJavaCodeReferenceElement ref, @NotNull QuickFixActionRegistrar registrar) {
|
||||
if (!PsiUtil.isAvailable(JavaFeature.SEALED_CLASSES, ref) && ref.textMatches(PsiKeyword.SEALED)) {
|
||||
for (CommonIntentionAction intention : HighlightUtil.getIncreaseLanguageLevelFixes(ref, JavaFeature.SEALED_CLASSES)) {
|
||||
for (CommonIntentionAction intention : HighlightFixUtil.getIncreaseLanguageLevelFixes(ref, JavaFeature.SEALED_CLASSES)) {
|
||||
registrar.register(intention.asIntention());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user