|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | - |
17 | 16 | package com.intellij.java.impl.codeInsight.generation.actions; |
18 | 17 |
|
| 18 | +import consulo.annotation.access.RequiredReadAction; |
19 | 19 | import consulo.language.editor.action.CodeInsightActionHandler; |
20 | 20 | import consulo.language.editor.action.CodeInsightAction; |
21 | 21 | import consulo.language.editor.generation.GenerateActionPopupTemplateInjector; |
22 | 22 | import com.intellij.java.language.psi.PsiClass; |
23 | 23 | import com.intellij.java.language.psi.PsiJavaFile; |
24 | 24 | import consulo.language.editor.refactoring.ContextAwareActionHandler; |
| 25 | +import consulo.localize.LocalizeValue; |
25 | 26 | import consulo.ui.ex.action.AnAction; |
26 | 27 | import consulo.dataContext.DataContext; |
27 | 28 | import consulo.ui.ex.action.Presentation; |
|
34 | 35 | import jakarta.annotation.Nullable; |
35 | 36 |
|
36 | 37 | public class BaseGenerateAction extends CodeInsightAction implements GenerateActionPopupTemplateInjector { |
37 | | - private final CodeInsightActionHandler myHandler; |
38 | | - |
39 | | - public BaseGenerateAction(CodeInsightActionHandler handler) { |
40 | | - myHandler = handler; |
41 | | - } |
| 38 | + private final CodeInsightActionHandler myHandler; |
42 | 39 |
|
43 | | - @Override |
44 | | - protected void update(@Nonnull Presentation presentation, @Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiFile file, @Nonnull DataContext dataContext, @Nullable String actionPlace) { |
45 | | - super.update(presentation, project, editor, file, dataContext, actionPlace); |
46 | | - if (myHandler instanceof ContextAwareActionHandler && presentation.isEnabled()) { |
47 | | - presentation.setEnabled(((ContextAwareActionHandler) myHandler).isAvailableForQuickList(editor, file, dataContext)); |
| 40 | + public BaseGenerateAction(CodeInsightActionHandler handler, @Nonnull LocalizeValue text) { |
| 41 | + myHandler = handler; |
| 42 | + getTemplatePresentation().setTextValue(text); |
48 | 43 | } |
49 | | - } |
50 | 44 |
|
51 | | - @Override |
52 | | - @Nullable |
53 | | - public AnAction createEditTemplateAction(DataContext dataContext) { |
54 | | - return null; |
55 | | - } |
56 | | - |
57 | | - @Nonnull |
58 | | - @Override |
59 | | - protected final CodeInsightActionHandler getHandler() { |
60 | | - return myHandler; |
61 | | - } |
| 45 | + @Override |
| 46 | + protected void update( |
| 47 | + @Nonnull Presentation presentation, |
| 48 | + @Nonnull Project project, |
| 49 | + @Nonnull Editor editor, |
| 50 | + @Nonnull PsiFile file, |
| 51 | + @Nonnull DataContext dataContext, |
| 52 | + @Nullable String actionPlace |
| 53 | + ) { |
| 54 | + super.update(presentation, project, editor, file, dataContext, actionPlace); |
| 55 | + if (myHandler instanceof ContextAwareActionHandler contextAwareHandler && presentation.isEnabled()) { |
| 56 | + presentation.setEnabled(contextAwareHandler.isAvailableForQuickList(editor, file, dataContext)); |
| 57 | + } |
| 58 | + } |
62 | 59 |
|
63 | | - @Nullable |
64 | | - protected PsiClass getTargetClass(Editor editor, PsiFile file) { |
65 | | - int offset = editor.getCaretModel().getOffset(); |
66 | | - PsiElement element = file.findElementAt(offset); |
67 | | - if (element == null) { |
68 | | - return null; |
| 60 | + @Override |
| 61 | + @Nullable |
| 62 | + public AnAction createEditTemplateAction(DataContext dataContext) { |
| 63 | + return null; |
69 | 64 | } |
70 | | - final PsiClass target = PsiTreeUtil.getParentOfType(element, PsiClass.class); |
71 | | - return target instanceof SyntheticElement ? null : target; |
72 | | - } |
73 | 65 |
|
74 | | - @Override |
75 | | - protected boolean isValidForFile(@Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiFile file) { |
76 | | - if (!(file instanceof PsiJavaFile)) { |
77 | | - return false; |
| 66 | + @Nonnull |
| 67 | + @Override |
| 68 | + protected final CodeInsightActionHandler getHandler() { |
| 69 | + return myHandler; |
78 | 70 | } |
79 | | - if (file instanceof PsiCompiledElement) { |
80 | | - return false; |
| 71 | + |
| 72 | + @Nullable |
| 73 | + @RequiredReadAction |
| 74 | + protected PsiClass getTargetClass(Editor editor, PsiFile file) { |
| 75 | + int offset = editor.getCaretModel().getOffset(); |
| 76 | + PsiElement element = file.findElementAt(offset); |
| 77 | + if (element == null) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + PsiClass target = PsiTreeUtil.getParentOfType(element, PsiClass.class); |
| 81 | + return target instanceof SyntheticElement ? null : target; |
81 | 82 | } |
82 | 83 |
|
83 | | - PsiDocumentManager.getInstance(project).commitAllDocuments(); |
| 84 | + @Override |
| 85 | + @RequiredReadAction |
| 86 | + protected boolean isValidForFile(@Nonnull Project project, @Nonnull Editor editor, @Nonnull PsiFile file) { |
| 87 | + if (!(file instanceof PsiJavaFile)) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + if (file instanceof PsiCompiledElement) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + PsiDocumentManager.getInstance(project).commitAllDocuments(); |
84 | 95 |
|
85 | | - PsiClass targetClass = getTargetClass(editor, file); |
86 | | - return targetClass != null && isValidForClass(targetClass); |
87 | | - } |
| 96 | + PsiClass targetClass = getTargetClass(editor, file); |
| 97 | + return targetClass != null && isValidForClass(targetClass); |
| 98 | + } |
88 | 99 |
|
89 | | - protected boolean isValidForClass(final PsiClass targetClass) { |
90 | | - return !targetClass.isInterface(); |
91 | | - } |
| 100 | + protected boolean isValidForClass(PsiClass targetClass) { |
| 101 | + return !targetClass.isInterface(); |
| 102 | + } |
92 | 103 | } |
0 commit comments