From 48d674e1d9f86949b4d55fce24d9fd13a85d2f6d Mon Sep 17 00:00:00 2001 From: "Kirill.Safonov" Date: Sun, 26 Sep 2010 18:19:48 +0400 Subject: [PATCH] Generate delegate methods action made language-independent in a proper way, using LangExtension --- .../generation/GenerateDelegateHandler.java | 10 +++- .../actions/GenerateDelegateAction.java | 42 -------------- .../com/intellij/lang/CodeInsightActions.java | 3 + .../generation/DelegateMethodsHandler.java | 46 ++++++++++++++++ .../actions/DelegateMethodsAction.java | 55 +++++++++++++++++++ .../src/META-INF/LangExtensionPoints.xml | 1 + .../src/idea/LangActions.xml | 1 + resources/src/META-INF/IdeaPlugin.xml | 1 + resources/src/idea/IdeaActions.xml | 1 - 9 files changed, 115 insertions(+), 45 deletions(-) delete mode 100644 java/java-impl/src/com/intellij/codeInsight/generation/actions/GenerateDelegateAction.java create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/generation/DelegateMethodsHandler.java create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/generation/actions/DelegateMethodsAction.java diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/GenerateDelegateHandler.java b/java/java-impl/src/com/intellij/codeInsight/generation/GenerateDelegateHandler.java index 741a9f0b7eb6..55378b41d8de 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/GenerateDelegateHandler.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/GenerateDelegateHandler.java @@ -15,9 +15,9 @@ */ package com.intellij.codeInsight.generation; -import com.intellij.codeInsight.CodeInsightActionHandler; import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.ide.util.MemberChooser; +import com.intellij.lang.LanguageCodeInsightActionHandler; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; @@ -43,9 +43,15 @@ import java.util.Set; /** * @author mike */ -public class GenerateDelegateHandler implements CodeInsightActionHandler { +public class GenerateDelegateHandler implements LanguageCodeInsightActionHandler { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.generation.GenerateDelegateHandler"); + @Override + public boolean isValidFor(Editor editor, PsiFile file) { + if (!(file instanceof PsiJavaFile)) return false; + return OverrideImplementUtil.getContextClass(editor.getProject(), editor, file, false) != null && isApplicable(file, editor); + } + public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) { if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) { return; diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/actions/GenerateDelegateAction.java b/java/java-impl/src/com/intellij/codeInsight/generation/actions/GenerateDelegateAction.java deleted file mode 100644 index b74f465c3503..000000000000 --- a/java/java-impl/src/com/intellij/codeInsight/generation/actions/GenerateDelegateAction.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2000-2009 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. - */ -package com.intellij.codeInsight.generation.actions; - -import com.intellij.codeInsight.CodeInsightActionHandler; -import com.intellij.codeInsight.actions.BaseCodeInsightAction; -import com.intellij.codeInsight.generation.GenerateDelegateHandler; -import com.intellij.codeInsight.generation.OverrideImplementUtil; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiJavaFile; - -/** - * @author mike - */ -public class GenerateDelegateAction extends BaseCodeInsightAction { - private final GenerateDelegateHandler myHandler = new GenerateDelegateHandler(); - - protected CodeInsightActionHandler getHandler() { - return myHandler; - } - - protected boolean isValidForFile(Project project, Editor editor, PsiFile file) { - if (!(file instanceof PsiJavaFile)) return false; - return OverrideImplementUtil.getContextClass(project, editor, file, false) != null && - myHandler.isApplicable(file, editor); - } -} diff --git a/platform/lang-api/src/com/intellij/lang/CodeInsightActions.java b/platform/lang-api/src/com/intellij/lang/CodeInsightActions.java index 088108580e79..d6d7113dc80d 100644 --- a/platform/lang-api/src/com/intellij/lang/CodeInsightActions.java +++ b/platform/lang-api/src/com/intellij/lang/CodeInsightActions.java @@ -26,6 +26,9 @@ public class CodeInsightActions { public static final LanguageExtension OVERRIDE_METHOD = new LanguageExtension("com.intellij.codeInsight.overrideMethod"); + public static final LanguageExtension + DELEGATE_METHODS = new LanguageExtension("com.intellij.codeInsight.delegateMethods"); + public static final LanguageExtension GOTO_SUPER = new LanguageExtension("com.intellij.codeInsight.gotoSuper"); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/generation/DelegateMethodsHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/generation/DelegateMethodsHandler.java new file mode 100644 index 000000000000..867c82e5b6ed --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/generation/DelegateMethodsHandler.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2010 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. + */ + +package com.intellij.codeInsight.generation; + +import com.intellij.codeInsight.CodeInsightActionHandler; +import com.intellij.lang.CodeInsightActions; +import com.intellij.lang.Language; +import com.intellij.lang.LanguageCodeInsightActionHandler; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.fileEditor.FileDocumentManager; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.intellij.psi.util.PsiUtilBase; +import org.jetbrains.annotations.NotNull; + +public class DelegateMethodsHandler implements CodeInsightActionHandler{ + public final void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull PsiFile file) { + if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)){ + return; + } + + Language language = PsiUtilBase.getLanguageAtOffset(file, editor.getCaretModel().getOffset()); + final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.DELEGATE_METHODS.forLanguage(language); + if (codeInsightActionHandler != null) { + codeInsightActionHandler.invoke(project, editor, file); + } + } + + public boolean startInWriteAction() { + return false; + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/generation/actions/DelegateMethodsAction.java b/platform/lang-impl/src/com/intellij/codeInsight/generation/actions/DelegateMethodsAction.java new file mode 100644 index 000000000000..56dd841d864b --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/generation/actions/DelegateMethodsAction.java @@ -0,0 +1,55 @@ +/* + * Copyright 2000-2010 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. + */ + +package com.intellij.codeInsight.generation.actions; + +import com.intellij.codeInsight.CodeInsightActionHandler; +import com.intellij.codeInsight.actions.BaseCodeInsightAction; +import com.intellij.codeInsight.generation.DelegateMethodsHandler; +import com.intellij.lang.CodeInsightActions; +import com.intellij.lang.Language; +import com.intellij.lang.LanguageCodeInsightActionHandler; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.intellij.psi.util.PsiUtilBase; + +public class DelegateMethodsAction extends BaseCodeInsightAction { + + protected CodeInsightActionHandler getHandler() { + return new DelegateMethodsHandler(); + } + + protected boolean isValidForFile(Project project, Editor editor, final PsiFile file) { + Language language = PsiUtilBase.getLanguageAtOffset(file, editor.getCaretModel().getOffset()); + final LanguageCodeInsightActionHandler codeInsightActionHandler = CodeInsightActions.DELEGATE_METHODS.forLanguage(language); + if (codeInsightActionHandler != null) { + return codeInsightActionHandler.isValidFor(editor, file); + } + return false; + } + + public void update(final AnActionEvent event) { + if (CodeInsightActions.DELEGATE_METHODS.hasAnyExtensions()) { + event.getPresentation().setVisible(true); + super.update(event); + } + else { + event.getPresentation().setVisible(false); + } + } +} \ No newline at end of file diff --git a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml index 97e8e7b73236..ca9adf25bf61 100644 --- a/platform/platform-resources/src/META-INF/LangExtensionPoints.xml +++ b/platform/platform-resources/src/META-INF/LangExtensionPoints.xml @@ -345,6 +345,7 @@ + diff --git a/platform/platform-resources/src/idea/LangActions.xml b/platform/platform-resources/src/idea/LangActions.xml index 4875c00769f5..4a9408ba4c1a 100644 --- a/platform/platform-resources/src/idea/LangActions.xml +++ b/platform/platform-resources/src/idea/LangActions.xml @@ -28,6 +28,7 @@ + diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 765b0bc3f67f..091a891e89e2 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -697,6 +697,7 @@ + diff --git a/resources/src/idea/IdeaActions.xml b/resources/src/idea/IdeaActions.xml index 5dbc91ed6b58..10bb54ab0b1d 100644 --- a/resources/src/idea/IdeaActions.xml +++ b/resources/src/idea/IdeaActions.xml @@ -41,7 +41,6 @@ -