javafx: create method according to target class lang (IDEA-103367)

(cherry picked from commit a229a8bcba0a5a8630daf4933ff48061f73106ea)
This commit is contained in:
anna
2013-03-21 10:40:36 +01:00
parent 2ab2ca0c28
commit b099d8cf99
6 changed files with 52 additions and 38 deletions
@@ -50,7 +50,7 @@ public class CreateMethodQuickFix extends IntentionAndQuickFixAction {
@NotNull
public String getName() {
String signature = PsiFormatUtil.formatMethod(createMethod(myTargetClass.getProject()), PsiSubstitutor.EMPTY,
String signature = PsiFormatUtil.formatMethod(createMethod(), PsiSubstitutor.EMPTY,
PsiFormatUtilBase.SHOW_NAME |
PsiFormatUtilBase.SHOW_TYPE |
PsiFormatUtilBase.SHOW_PARAMETERS |
@@ -69,7 +69,7 @@ public class CreateMethodQuickFix extends IntentionAndQuickFixAction {
public void applyFix(Project project, PsiFile file, @Nullable Editor editor) {
if (!CodeInsightUtilBase.preparePsiElementForWrite(myTargetClass.getContainingFile())) return;
PsiMethod method = createMethod(project);
PsiMethod method = createMethod();
List<Pair<PsiExpression, PsiType>> arguments =
ContainerUtil.map2List(method.getParameterList().getParameters(), new Function<PsiParameter, Pair<PsiExpression, PsiType>>() {
@Override
@@ -82,8 +82,11 @@ public class CreateMethodQuickFix extends IntentionAndQuickFixAction {
CreateMethodFromUsageFix.doCreate(myTargetClass, method, arguments, PsiSubstitutor.EMPTY, ExpectedTypeInfo.EMPTY_ARRAY, method);
}
private PsiMethod createMethod(Project project) {
PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
private PsiMethod createMethod() {
JVMElementFactory elementFactory = JVMElementFactories.getFactory(myTargetClass.getLanguage(), myTargetClass.getProject());
if (elementFactory == null) {
elementFactory = JavaPsiFacade.getElementFactory(myTargetClass.getProject());
}
String methodText = mySignature + (myTargetClass.isInterface() ? ";" : "{" + myBody + "}");
return elementFactory.createMethodFromText(methodText, null);
}
@@ -92,7 +95,7 @@ public class CreateMethodQuickFix extends IntentionAndQuickFixAction {
public static CreateMethodQuickFix createFix(@NotNull PsiClass targetClass, @NonNls final String signature, @NonNls final String body) {
CreateMethodQuickFix fix = new CreateMethodQuickFix(targetClass, signature, body);
try {
fix.createMethod(targetClass.getProject());
fix.createMethod();
return fix;
}
catch (IncorrectOperationException e) {
+1
View File
@@ -12,6 +12,7 @@
<orderEntry type="module" module-name="platform-impl" scope="TEST" />
<orderEntry type="module" module-name="testFramework-java" scope="TEST" />
<orderEntry type="module" module-name="xml" scope="TEST" />
<orderEntry type="module" module-name="jetgroovy" scope="TEST" />
</component>
</module>
@@ -15,53 +15,52 @@
*/
package org.jetbrains.plugins.javaFX.fxml;
import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.PsiTestUtil;
import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class JavaFXQuickfixTest extends LightCodeInsightFixtureTestCase {
public static final DefaultLightProjectDescriptor JAVA_FX_WITH_GROOVY_DESCRIPTOR = new DefaultLightProjectDescriptor() {
@Override
public void configureModule(Module module, ModifiableRootModel model, ContentEntry contentEntry) {
PsiTestUtil.addLibrary(module, model, "javafx", PluginPathManager.getPluginHomePath("javaFX") + "/testData", "jfxrt.jar");
PsiTestUtil.addLibrary(module, model, "groovy", PluginPathManager.getPluginHomePath("groovy") + "/testdata/mockGroovyLib1.8", "groovy-1.8.0-beta-2.jar");
super.configureModule(module, model, contentEntry);
}
};
public class JavaFXQuickfixTest extends DaemonAnalyzerTestCase {
@NotNull
@Override
protected void setUpModule() {
super.setUpModule();
PsiTestUtil.addLibrary(getModule(), "javafx", PluginPathManager.getPluginHomePath("javaFX") + "/testData", "jfxrt.jar");
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_FX_WITH_GROOVY_DESCRIPTOR;
}
public void testCreateControllerMethod() throws Exception {
doTest("Create Method 'void bar(ActionEvent)'", true);
doTest("Create Method 'void bar(ActionEvent)'", ".java");
}
public void testCreateControllerMethodInGroovy() throws Exception {
doTest("Create Method 'void bar(ActionEvent)'", ".groovy");
}
public void testCreateField() throws Exception {
doTest("Create Field 'btn'", true);
doTest("Create Field 'btn'", ".java");
}
private void doTest(final String actionName) throws Exception {
doTest(actionName, false);
}
private void doTest(final String actionName, boolean changeEditor) throws Exception {
configureByFiles(null, getTestName(true) + ".fxml", getTestName(false) + ".java");
final List<HighlightInfo> infos = doHighlighting();
findAndInvokeIntentionAction(infos, actionName, getEditor(), getFile());
if (changeEditor) {
final PsiElement targetMethod = TargetElementUtilBase.findTargetElement(getEditor(), TargetElementUtilBase.REFERENCED_ELEMENT_ACCEPTED);
assertNotNull(targetMethod);
final VirtualFile file = targetMethod.getContainingFile().getVirtualFile();
assertNotNull(file);
final Editor editor = FileEditorManager.getInstance(getProject()).openTextEditor(new OpenFileDescriptor(getProject(), file), true);
assertNotNull(editor);
setActiveEditor(editor);
}
checkResultByFile(getTestName(false) + "_after.java");
private void doTest(final String actionName, final String extension) throws Exception {
String path = getTestName(true) + ".fxml";
final IntentionAction intention =
myFixture.getAvailableIntention(actionName, path, getTestName(false) + extension);
assertNotNull(intention);
myFixture.launchAction(intention);
myFixture.checkResultByFile(getTestName(false) + extension, getTestName(false) + "_after" + extension, true);
}
@NotNull
@@ -0,0 +1 @@
public class CreateControllerMethodInGroovy {}
@@ -0,0 +1,5 @@
public class CreateControllerMethodInGroovy {
public void bar(ActionEvent actionEvent) {
//To change body of created methods use File | Settings | File Templates.
}
}
@@ -0,0 +1,5 @@
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="CreateControllerMethodInGroovy">
<Button onAction="#b<caret>ar"/>
</GridPane>