IncreaseLanguageLevelFix: suggest to accept level if not yet
authorRoman Ivanov <roman.ivanov@jetbrains.com>
Thu, 13 Aug 2020 00:21:25 +0000 (02:21 +0200)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Thu, 13 Aug 2020 07:26:45 +0000 (07:26 +0000)
GitOrigin-RevId: bf75af630d5877474f327365bb52ae6458feb732

java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/IncreaseLanguageLevelFix.java

index 4c1033d684204686b3f91b85f05cc8862dc33964..d1c39304b441b9c2ed6e088eff206d7940f65d95 100644 (file)
@@ -6,6 +6,7 @@ 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.application.WriteAction;
 import com.intellij.openapi.command.undo.BasicUndoableAction;
 import com.intellij.openapi.command.undo.UndoManager;
 import com.intellij.openapi.editor.Editor;
@@ -54,6 +55,7 @@ public class IncreaseLanguageLevelFix implements IntentionAction, LocalQuickFix,
   @Override
   public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
     PsiElement element = descriptor.getPsiElement();
+    if (!isAvailable(project, null, element.getContainingFile())) return;
     invoke(project, null, element.getContainingFile());
   }
 
@@ -61,31 +63,39 @@ public class IncreaseLanguageLevelFix implements IntentionAction, LocalQuickFix,
   public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
     Module module = ModuleUtilCore.findModuleForFile(file);
     return module != null &&
-           JavaSdkUtil.isLanguageLevelAcceptable(project, module, myLevel) &&
-           AcceptedLanguageLevelsSettings.isLanguageLevelAccepted(myLevel);
+           JavaSdkUtil.isLanguageLevelAcceptable(project, module, myLevel)
+           && (editor != null || AcceptedLanguageLevelsSettings.isLanguageLevelAccepted(myLevel));
   }
 
   @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);
-          }
-        });
+    if (!AcceptedLanguageLevelsSettings.isLanguageLevelAccepted(myLevel)) {
+      assert editor != null;
+      if (AcceptedLanguageLevelsSettings.checkAccepted(editor.getComponent(), myLevel) == null) {
+        return;
       }
     }
+    WriteAction.run(() -> {
+      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);
+            }
+          });
+        }
+      }
+    });
   }
 
   @Nullable
@@ -96,6 +106,6 @@ public class IncreaseLanguageLevelFix implements IntentionAction, LocalQuickFix,
 
   @Override
   public boolean startInWriteAction() {
-    return true;
+    return false;
   }
 }
\ No newline at end of file