|
18 | 18 | import com.intellij.java.language.LanguageLevel; |
19 | 19 | import com.intellij.java.language.impl.projectRoots.JavaSdkVersionUtil; |
20 | 20 | import com.intellij.java.language.projectRoots.JavaSdkVersion; |
21 | | -import consulo.annotation.access.RequiredReadAction; |
22 | 21 | import consulo.codeEditor.Editor; |
23 | 22 | import consulo.content.bundle.Sdk; |
24 | 23 | import consulo.java.language.module.extension.JavaModuleExtension; |
25 | 24 | import consulo.java.language.module.extension.JavaMutableModuleExtension; |
| 25 | +import consulo.language.editor.inspection.LocalQuickFix; |
| 26 | +import consulo.language.editor.inspection.ProblemDescriptor; |
26 | 27 | import consulo.language.editor.intention.SyntheticIntentionAction; |
27 | 28 | import consulo.language.editor.localize.CodeInsightLocalize; |
| 29 | +import consulo.language.psi.PsiElement; |
28 | 30 | import consulo.language.psi.PsiFile; |
29 | 31 | import consulo.language.util.IncorrectOperationException; |
30 | 32 | import consulo.language.util.ModuleUtilCore; |
|
42 | 44 | /** |
43 | 45 | * @author cdr |
44 | 46 | */ |
45 | | -public class IncreaseLanguageLevelFix implements SyntheticIntentionAction { |
46 | | - private static final Logger LOG = Logger.getInstance(IncreaseLanguageLevelFix.class); |
47 | | - |
48 | | - private final LanguageLevel myLevel; |
49 | | - |
50 | | - public IncreaseLanguageLevelFix(LanguageLevel targetLevel) { |
51 | | - myLevel = targetLevel; |
52 | | - } |
53 | | - |
54 | | - @Override |
55 | | - @Nonnull |
56 | | - public LocalizeValue getText() { |
57 | | - return CodeInsightLocalize.setLanguageLevelTo0(myLevel.getDescription().get()); |
58 | | - } |
59 | | - |
60 | | - private static boolean isJdkSupportsLevel(@Nullable final Sdk jdk, final LanguageLevel level) { |
61 | | - if (jdk == null) return true; |
62 | | - JavaSdkVersion version = JavaSdkVersionUtil.getJavaSdkVersion(jdk); |
63 | | - JavaSdkVersion required = JavaSdkVersion.fromLanguageLevel(level); |
64 | | - return version != null && (level.isPreview() ? version.equals(required) : version.isAtLeast(required)); |
65 | | - } |
66 | | - |
67 | | - @Override |
68 | | - public boolean isAvailable(@Nonnull final Project project, final Editor editor, final PsiFile file) { |
69 | | - final VirtualFile virtualFile = file.getVirtualFile(); |
70 | | - if (virtualFile == null) { |
71 | | - return false; |
| 47 | +public class IncreaseLanguageLevelFix implements SyntheticIntentionAction, LocalQuickFix { |
| 48 | + private static final Logger LOG = Logger.getInstance(IncreaseLanguageLevelFix.class); |
| 49 | + |
| 50 | + private final LanguageLevel myLevel; |
| 51 | + |
| 52 | + public IncreaseLanguageLevelFix(LanguageLevel targetLevel) { |
| 53 | + myLevel = targetLevel; |
| 54 | + } |
| 55 | + |
| 56 | + @Nonnull |
| 57 | + @Override |
| 58 | + public LocalizeValue getText() { |
| 59 | + return CodeInsightLocalize.setLanguageLevelTo0(myLevel.getDescription().get()); |
72 | 60 | } |
73 | 61 |
|
74 | | - final Module module = ModuleUtilCore.findModuleForFile(virtualFile, project); |
75 | | - return module != null && isLanguageLevelAcceptable(module, myLevel); |
76 | | - } |
77 | | - |
78 | | - public static boolean isLanguageLevelAcceptable(Module module, final LanguageLevel level) { |
79 | | - return isJdkSupportsLevel(ModuleUtilCore.getSdk(module, JavaModuleExtension.class), level); |
80 | | - } |
81 | | - |
82 | | - @Override |
83 | | - @RequiredUIAccess |
84 | | - public void invoke(@Nonnull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException { |
85 | | - final VirtualFile virtualFile = file.getVirtualFile(); |
86 | | - LOG.assertTrue(virtualFile != null); |
87 | | - final Module module = ModuleUtilCore.findModuleForFile(virtualFile, project); |
88 | | - if (module == null) { |
89 | | - return; |
| 62 | + @Nonnull |
| 63 | + @Override |
| 64 | + public LocalizeValue getName() { |
| 65 | + return getText(); |
90 | 66 | } |
91 | 67 |
|
92 | | - JavaModuleExtension extension = ModuleUtilCore.getExtension(module, JavaModuleExtension.class); |
93 | | - if (extension == null) { |
94 | | - return; |
| 68 | + @Override |
| 69 | + @RequiredUIAccess |
| 70 | + public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) { |
| 71 | + PsiElement element = descriptor.getPsiElement(); |
| 72 | + invoke(project, null, element.getContainingFile()); |
95 | 73 | } |
96 | 74 |
|
97 | | - final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel(); |
98 | | - JavaMutableModuleExtension mutableModuleExtension = rootModel.getExtension(JavaMutableModuleExtension.class); |
| 75 | + private static boolean isJdkSupportsLevel(@Nullable Sdk jdk, LanguageLevel level) { |
| 76 | + if (jdk == null) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + JavaSdkVersion version = JavaSdkVersionUtil.getJavaSdkVersion(jdk); |
| 80 | + JavaSdkVersion required = JavaSdkVersion.fromLanguageLevel(level); |
| 81 | + return version != null && (level.isPreview() ? version.equals(required) : version.isAtLeast(required)); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean isAvailable(@Nonnull Project project, Editor editor, PsiFile file) { |
| 86 | + VirtualFile virtualFile = file.getVirtualFile(); |
| 87 | + if (virtualFile == null) { |
| 88 | + return false; |
| 89 | + } |
99 | 90 |
|
100 | | - assert mutableModuleExtension != null; |
| 91 | + Module module = ModuleUtilCore.findModuleForFile(virtualFile, project); |
| 92 | + return module != null && isLanguageLevelAcceptable(module, myLevel); |
| 93 | + } |
101 | 94 |
|
102 | | - mutableModuleExtension.getInheritableLanguageLevel().set(null, myLevel.getName()); |
| 95 | + public static boolean isLanguageLevelAcceptable(Module module, LanguageLevel level) { |
| 96 | + return isJdkSupportsLevel(ModuleUtilCore.getSdk(module, JavaModuleExtension.class), level); |
| 97 | + } |
103 | 98 |
|
104 | | - project.getApplication().runWriteAction(rootModel::commit); |
105 | | - } |
| 99 | + @Override |
| 100 | + @RequiredUIAccess |
| 101 | + public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { |
| 102 | + VirtualFile virtualFile = file.getVirtualFile(); |
| 103 | + LOG.assertTrue(virtualFile != null); |
| 104 | + Module module = ModuleUtilCore.findModuleForFile(virtualFile, project); |
| 105 | + if (module == null) { |
| 106 | + return; |
| 107 | + } |
106 | 108 |
|
107 | | - @Override |
108 | | - public boolean startInWriteAction() { |
109 | | - return false; |
110 | | - } |
| 109 | + JavaModuleExtension extension = ModuleUtilCore.getExtension(module, JavaModuleExtension.class); |
| 110 | + if (extension == null) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel(); |
| 115 | + JavaMutableModuleExtension mutableModuleExtension = rootModel.getExtension(JavaMutableModuleExtension.class); |
| 116 | + |
| 117 | + assert mutableModuleExtension != null; |
| 118 | + |
| 119 | + mutableModuleExtension.getInheritableLanguageLevel().set(null, myLevel.getName()); |
| 120 | + |
| 121 | + project.getApplication().runWriteAction(rootModel::commit); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public boolean startInWriteAction() { |
| 126 | + return false; |
| 127 | + } |
111 | 128 | } |
0 commit comments