mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
[java] suggest to upgrade jdk if language feature from newer version is used (IDEA-256074)
GitOrigin-RevId: 82cd81b54f9ba59def65a5639e683f2a29e6432c
This commit is contained in:
committed by
intellij-monorepo-bot
parent
999c684682
commit
9a6025f68d
@@ -232,6 +232,9 @@ public abstract class QuickFixFactory {
|
||||
@NotNull
|
||||
public abstract IntentionAction createIncreaseLanguageLevelFix(@NotNull LanguageLevel level);
|
||||
|
||||
@NotNull
|
||||
public abstract IntentionAction createUpgradeSdkFor(@NotNull LanguageLevel level);
|
||||
|
||||
@NotNull
|
||||
public abstract IntentionAction createChangeParameterClassFix(@NotNull PsiClass aClass, @NotNull PsiClassType type);
|
||||
|
||||
|
||||
@@ -3413,7 +3413,9 @@ public final class HighlightUtil {
|
||||
@NotNull HighlightingFeature feature,
|
||||
@NotNull QuickFixActionRegistrar registrar) {
|
||||
if (feature.isAvailable(element)) return;
|
||||
registrar.register(getFixFactory().createIncreaseLanguageLevelFix(getApplicableLevel(element.getContainingFile(), feature)));
|
||||
LanguageLevel applicableLevel = getApplicableLevel(element.getContainingFile(), feature);
|
||||
registrar.register(getFixFactory().createIncreaseLanguageLevelFix(applicableLevel));
|
||||
registrar.register(getFixFactory().createUpgradeSdkFor(applicableLevel));
|
||||
registrar.register(getFixFactory().createShowModulePropertiesFix(element));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInsight.daemon.impl.analysis;
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.projectRoots.JavaSdkType;
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersion;
|
||||
import com.intellij.openapi.projectRoots.JavaSdkVersionUtil;
|
||||
import com.intellij.openapi.projectRoots.ex.JavaSdkUtil;
|
||||
import com.intellij.openapi.roots.ui.configuration.SdkPopupFactory;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class UpgradeSdkFix implements IntentionAction {
|
||||
private final LanguageLevel myLevel;
|
||||
|
||||
public UpgradeSdkFix(@NotNull LanguageLevel targetLevel) {
|
||||
myLevel = targetLevel;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return JavaBundle.message("intention.name.upgrade.jdk.to", myLevel.toJavaVersion().feature);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return JavaBundle.message("intention.family.name.upgrade.jdk");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
Module module = ModuleUtilCore.findModuleForFile(file);
|
||||
return module != null && !JavaSdkUtil.isLanguageLevelAcceptable(project, module, myLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
JavaSdkVersion required = JavaSdkVersion.fromLanguageLevel(myLevel);
|
||||
SdkPopupFactory
|
||||
.newBuilder()
|
||||
.withProject(project)
|
||||
.withSdkTypeFilter(type -> type instanceof JavaSdkType)
|
||||
.withSdkFilter(sdk -> JavaSdkVersionUtil.getJavaSdkVersion(sdk).isAtLeast(required))
|
||||
.updateSdkForFile(file)
|
||||
.buildPopup()
|
||||
.showInBestPositionFor(editor);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInsight.daemon.impl.*;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.IncreaseLanguageLevelFix;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.UpgradeSdkFix;
|
||||
import com.intellij.codeInsight.daemon.impl.quickfix.*;
|
||||
import com.intellij.codeInsight.daemon.quickFix.CreateClassOrPackageFix;
|
||||
import com.intellij.codeInsight.daemon.quickFix.CreateFieldOrPropertyFix;
|
||||
@@ -377,6 +378,11 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
|
||||
return new IncreaseLanguageLevelFix(level);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntentionAction createUpgradeSdkFor(@NotNull LanguageLevel level) {
|
||||
return new UpgradeSdkFix(level);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IntentionAction createChangeParameterClassFix(@NotNull PsiClass aClass, @NotNull PsiClassType type) {
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
public class Lambda {
|
||||
void m() {
|
||||
va<caret>r x = "";
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,9 @@
|
||||
/*
|
||||
* Copyright 2000-2017 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.
|
||||
*/
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.java.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
@@ -42,4 +29,10 @@ public class IncreaseLanguageLevelFixTest extends JavaCodeInsightFixtureTestCase
|
||||
myFixture.launchAction(fix);
|
||||
assertEquals(LanguageLevel.JDK_1_8, PsiUtil.getLanguageLevel(myFixture.getFile()));
|
||||
}
|
||||
|
||||
public void testUpgradeJdk() {
|
||||
myFixture.configureByFile("UpgradeJdk.java");
|
||||
assertEquals(LanguageLevel.JDK_1_6, PsiUtil.getLanguageLevel(myFixture.getFile()));
|
||||
assertNotNull(myFixture.findSingleIntention(JavaBundle.message("intention.name.upgrade.jdk.to", "10")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1736,7 +1736,10 @@ intention.family.name.set.explicit.variable.type=Set explicit variable type
|
||||
intention.name.set.variable.type=Set variable type to ''{0}''
|
||||
introduce.parameter.inlay.title.delegate=Delegate
|
||||
introduce.parameter.inlay.tooltip.delegate=Delegate via overloading method
|
||||
introduce.parameter.advertisement.text=Press {0} to delegate via overloading method or {1} to show more options
|
||||
introduce.parameter.advertisement.text=Press {0} to delegate via overloading method or {1} to show more options
|
||||
progress.title.collect.method.overriders=Collect method overriders...
|
||||
unresolved.class.reference.repair.message=Try to resolve class reference
|
||||
remove.var.keyword.text=Remove 'var'
|
||||
|
||||
intention.family.name.upgrade.jdk=Upgrade JDK
|
||||
intention.name.upgrade.jdk.to=Upgrade JDK to {0}+
|
||||
Reference in New Issue
Block a user