Generate delegate methods action made language-independent in a proper way, using LangExtension

This commit is contained in:
Kirill.Safonov
2010-09-26 18:19:48 +04:00
parent 33fcbb6516
commit 48d674e1d9
9 changed files with 115 additions and 45 deletions
@@ -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;
@@ -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);
}
}
@@ -26,6 +26,9 @@ public class CodeInsightActions {
public static final LanguageExtension<LanguageCodeInsightActionHandler>
OVERRIDE_METHOD = new LanguageExtension<LanguageCodeInsightActionHandler>("com.intellij.codeInsight.overrideMethod");
public static final LanguageExtension<LanguageCodeInsightActionHandler>
DELEGATE_METHODS = new LanguageExtension<LanguageCodeInsightActionHandler>("com.intellij.codeInsight.delegateMethods");
public static final LanguageExtension<LanguageCodeInsightActionHandler>
GOTO_SUPER = new LanguageExtension<LanguageCodeInsightActionHandler>("com.intellij.codeInsight.gotoSuper");
@@ -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;
}
}
@@ -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);
}
}
}
@@ -345,6 +345,7 @@
<extensionPoint name="codeInsight.implementMethod" beanClass="com.intellij.lang.LanguageExtensionPoint"/>
<extensionPoint name="codeInsight.overrideMethod" beanClass="com.intellij.lang.LanguageExtensionPoint"/>
<extensionPoint name="codeInsight.delegateMethods" beanClass="com.intellij.lang.LanguageExtensionPoint"/>
<extensionPoint name="codeInsight.gotoSuper" beanClass="com.intellij.lang.LanguageExtensionPoint"/>
<extensionPoint name="codeInsight.lineMarkerProvider" beanClass="com.intellij.lang.LanguageExtensionPoint"/>
<extensionPoint name="codeInsight.surroundWithRangeAdjuster" interface="com.intellij.codeInsight.generation.surroundWith.SurroundWithRangeAdjuster"/>
@@ -28,6 +28,7 @@
<group id="GenerateGroup">
<action id="OverrideMethods" class="com.intellij.codeInsight.generation.actions.OverrideMethodsAction"/>
<action id="ImplementMethods" class="com.intellij.codeInsight.generation.actions.ImplementMethodsAction"/>
<action id="DelegateMethods" class="com.intellij.codeInsight.generation.actions.DelegateMethodsAction"/>
</group>
<action id="ShowIntentionActions" class="com.intellij.codeInsight.intention.actions.ShowIntentionActionsAction"/>
+1
View File
@@ -697,6 +697,7 @@
<codeInsight.overrideMethod language="JAVA" implementationClass="com.intellij.codeInsight.generation.JavaOverrideMethodsHandler"/>
<codeInsight.implementMethod language="JAVA" implementationClass="com.intellij.codeInsight.generation.JavaImplementMethodsHandler"/>
<codeInsight.delegateMethods language="JAVA" implementationClass="com.intellij.codeInsight.generation.GenerateDelegateHandler"/>
<codeInsight.gotoSuper language="JAVA" implementationClass="com.intellij.codeInsight.navigation.JavaGotoSuperHandler"/>
<codeInsight.lineMarkerProvider language="JAVA" implementationClass="com.intellij.codeInsight.daemon.impl.JavaLineMarkerProvider"/>
<codeInsight.lineMarkerProvider language="JAVA" implementationClass="com.intellij.codeInsight.daemon.impl.IconLineMarkerProvider"/>
-1
View File
@@ -41,7 +41,6 @@
</group>
<group id="JavaGenerateGroup2">
<action id="DelegateMethods" class="com.intellij.codeInsight.generation.actions.GenerateDelegateAction"/>
<action id="GenerateSuperMethodCall" class="com.intellij.codeInsight.generation.actions.GenerateSuperMethodCallAction"/>
<add-to-group group-id="GenerateGroup" anchor="last"/>