Make IncreaseLanguageLevelFix undoable and high-priority

Otherwise the first fix displayed is 'Module settings' which is probably not what people want.

GitOrigin-RevId: 4d37327a620638d171965a988397656479148c4b
This commit is contained in:
Tagir Valeev
2020-04-30 06:26:10 +00:00
committed by intellij-monorepo-bot
parent 19cc7702ec
commit 642e4911cd
@@ -1,16 +1,21 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.java.JavaBundle;
import com.intellij.openapi.command.undo.BasicUndoableAction;
import com.intellij.openapi.command.undo.UndoManager;
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.ex.JavaSdkUtil;
import com.intellij.openapi.roots.JavaProjectModelModificationService;
import com.intellij.openapi.roots.LanguageLevelModuleExtensionImpl;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.AcceptedLanguageLevelsSettings;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiElement;
@@ -23,7 +28,7 @@ import org.jetbrains.annotations.Nullable;
/**
* @author cdr
*/
public class IncreaseLanguageLevelFix implements IntentionAction, LocalQuickFix {
public class IncreaseLanguageLevelFix implements IntentionAction, LocalQuickFix, HighPriorityAction {
private final LanguageLevel myLevel;
public IncreaseLanguageLevelFix(@NotNull LanguageLevel targetLevel) {
@@ -66,8 +71,23 @@ public class IncreaseLanguageLevelFix implements IntentionAction, LocalQuickFix
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
Module module = ModuleUtilCore.findModuleForPsiElement(file);
LanguageLevel oldLevel = LanguageLevelModuleExtensionImpl.getInstance(module).getLanguageLevel();
if (module != null) {
JavaProjectModelModificationService.getInstance(project).changeLanguageLevel(module, myLevel);
VirtualFile vFile = file.getVirtualFile();
if (oldLevel != null) {
UndoManager.getInstance(project).undoableActionPerformed(new BasicUndoableAction(vFile) {
@Override
public void undo() {
JavaProjectModelModificationService.getInstance(project).changeLanguageLevel(module, oldLevel);
}
@Override
public void redo() {
JavaProjectModelModificationService.getInstance(project).changeLanguageLevel(module, myLevel);
}
});
}
}
}