Update JavaFxEventHandlerReference to new JvmCommonIntentionActionsFactory API (IDEA-172229)

This commit is contained in:
Nicolay Mitropolsky
2017-07-05 19:37:24 +03:00
parent dc8e583b53
commit 7c464c65a1
17 changed files with 56 additions and 37 deletions
@@ -40,12 +40,12 @@ import java.util.Set;
public class JavaFXQuickfixTest extends LightCodeInsightFixtureTestCase {
public static final DefaultLightProjectDescriptor JAVA_FX_WITH_GROOVY_DESCRIPTOR = new DefaultLightProjectDescriptor() {
@Override
public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
AbstractJavaFXTestCase.addJavaFxJarAsLibrary(module, model);
PsiTestUtil.addLibrary(module, model, "javafx", PluginPathManager.getPluginHomePath("javaFX") + "/testData", "groovy-1.8.0.jar");
super.configureModule(module, model, contentEntry);
}
};
PsiTestUtil.addLibrary(module, model, "javafx", PluginPathManager.getPluginHomePath("javaFX") + "/testData", "groovy-1.8.0.jar");
super.configureModule(module, model, contentEntry);
}
};
@NotNull
@Override
@@ -106,7 +106,8 @@ public class JavaFXQuickfixTest extends LightCodeInsightFixtureTestCase {
}
public void testCreateMethodEscalateVisibility() throws Exception {
doTestWithDefaultVisibility("Create method 'void onAction(ActionEvent)'", "CreateMethod", VisibilityUtil.ESCALATE_VISIBILITY, ".java");
doTestWithDefaultVisibility("Create method 'void onAction(ActionEvent)'", "CreateMethod", VisibilityUtil.ESCALATE_VISIBILITY,
".java");
}
public void testCreateFieldEmptyName() throws Exception {
@@ -122,7 +123,8 @@ public class JavaFXQuickfixTest extends LightCodeInsightFixtureTestCase {
assertNotNull(intention);
Set<String> languages = JavaFxInjectPageLanguageIntention.getAvailableLanguages(getProject());
assertContainsElements(languages, "groovy");
JavaFxInjectPageLanguageIntention languageIntention = (JavaFxInjectPageLanguageIntention)((IntentionActionDelegate)intention).getDelegate();
JavaFxInjectPageLanguageIntention languageIntention =
(JavaFxInjectPageLanguageIntention)((IntentionActionDelegate)intention).getDelegate();
languageIntention.registerPageLanguage(getProject(), (XmlFile)myFixture.getFile(), "groovy");
myFixture.checkResultByFile(getTestName(true) + ".fxml", getTestName(true) + "_after.fxml", true);
}
@@ -168,7 +170,7 @@ public class JavaFXQuickfixTest extends LightCodeInsightFixtureTestCase {
super.setUp();
myFixture.enableInspections(new JavaFxUnresolvedFxIdReferenceInspection());
}
@NotNull
@Override
protected String getTestDataPath() {
@@ -16,12 +16,16 @@
package org.jetbrains.plugins.javaFX.fxml.refs;
import com.intellij.codeInsight.daemon.QuickFixActionRegistrar;
import com.intellij.codeInsight.daemon.impl.quickfix.CreateMethodQuickFix;
import com.intellij.codeInsight.intention.JvmCommonIntentionActionsFactory;
import com.intellij.codeInsight.intention.MethodInsertionInfo;
import com.intellij.codeInsight.quickfix.UnresolvedReferenceQuickFixProvider;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.codeStyle.SuggestedNameInfo;
import com.intellij.psi.codeStyle.VariableKind;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.psi.xml.XmlAttribute;
@@ -34,6 +38,7 @@ import org.jetbrains.plugins.javaFX.fxml.JavaFxCommonNames;
import org.jetbrains.plugins.javaFX.fxml.JavaFxPsiUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class JavaFxEventHandlerReference extends PsiReferenceBase<XmlAttributeValue> {
@@ -59,8 +64,8 @@ public class JavaFxEventHandlerReference extends PsiReferenceBase<XmlAttributeVa
final List<PsiMethod> availableHandlers = new ArrayList<>();
for (PsiMethod psiMethod : myController.getAllMethods()) {
if (isHandlerMethodSignature(psiMethod, myController) && JavaFxPsiUtil.isVisibleInFxml(psiMethod)) {
availableHandlers.add(psiMethod);
}
availableHandlers.add(psiMethod);
}
}
return availableHandlers.isEmpty() ? EMPTY_ARRAY : ArrayUtil.toObjectArray(availableHandlers);
}
@@ -92,30 +97,58 @@ public class JavaFxEventHandlerReference extends PsiReferenceBase<XmlAttributeVa
return new TextRange(range.getStartOffset() + 1, range.getEndOffset());
}
public static class JavaFxUnresolvedReferenceHandlerQuickfixProvider extends UnresolvedReferenceQuickFixProvider<JavaFxEventHandlerReference> {
public static class JavaFxUnresolvedReferenceHandlerQuickfixProvider
extends UnresolvedReferenceQuickFixProvider<JavaFxEventHandlerReference> {
@Override
public void registerFixes(@NotNull final JavaFxEventHandlerReference ref, @NotNull final QuickFixActionRegistrar registrar) {
if (ref.myController != null && ref.myEventHandler == null) {
final CreateMethodQuickFix quickFix = CreateMethodQuickFix.createFix(ref.myController, getHandlerSignature(ref), "");
if (quickFix != null) {
registrar.register(quickFix);
}
JvmCommonIntentionActionsFactory intentionActionsFactory =
JvmCommonIntentionActionsFactory.forLanguage(ref.myController.getLanguage());
if (intentionActionsFactory == null) return;
String javaSignature = getHandlerSignature(ref);
PsiMethod javaMethod = JavaPsiFacade.getElementFactory(ref.myController.getProject())
.createMethodFromText(javaSignature, ref.myController);
MethodInsertionInfo.Method method =
MethodInsertionInfo.simpleMethodInfo(ref.myController,
javaMethod.getName(),
javaMethod.getModifierList().getText(),
javaMethod.getReturnType(),
Arrays.asList(javaMethod.getParameterList().getParameters()));
intentionActionsFactory.createAddCallableMemberActions(method).forEach(registrar::register);
}
}
private static String getHandlerSignature(JavaFxEventHandlerReference ref) {
final XmlAttributeValue element = ref.getElement();
String canonicalText = JavaFxCommonNames.JAVAFX_EVENT;
PsiType eventType = getEventType(element);
final String modifiers = getModifiers(element.getProject());
return modifiers + " void " + element.getValue().substring(1) + "(" +
eventType.getCanonicalText() + " " + suggestParamName(element.getProject(), eventType) +
")";
}
private static String suggestParamName(Project project, PsiType eventType) {
SuggestedNameInfo suggestedInfo = JavaCodeStyleManager.getInstance(project)
.suggestVariableName(VariableKind.PARAMETER, null, null, eventType);
if (suggestedInfo.names.length < 1) {
return "e";
}
return suggestedInfo.names[0];
}
@NotNull
private static PsiType getEventType(XmlAttributeValue element) {
final PsiElement parent = element.getParent();
if (parent instanceof XmlAttribute) {
final PsiClassType eventType = JavaFxPsiUtil.getDeclaredEventType((XmlAttribute)parent);
if (eventType != null) {
canonicalText = eventType.getCanonicalText();
return eventType;
}
}
final String modifiers = getModifiers(element.getProject());
return modifiers + " void " + element.getValue().substring(1) + "(" + canonicalText + " e)";
return PsiType.getTypeByName(JavaFxCommonNames.JAVAFX_EVENT, element.getProject(), element.getResolveScope());
}
@NotNull
@@ -7,6 +7,5 @@ public class QuickfixHalfRaw {
@FXML TableView<Pair> table;
public void onSort(SortEvent<TableView<Pair>> tableViewSortEvent) {
}
}
@@ -2,6 +2,5 @@ import javafx.scene.control.TableColumn.CellEditEvent;
public class QuickfixNoFieldNested {
public void onColumnEditStart(CellEditEvent cellEditEvent) {
}
}
@@ -3,6 +3,5 @@ import javafx.scene.control.TableView;
public class QuickfixNoField {
public void onSort(SortEvent<TableView> tableViewSortEvent) {
}
}
@@ -6,6 +6,5 @@ public class QuickfixRaw {
@FXML TableView table;
public void onSort(SortEvent<TableView> tableViewSortEvent) {
}
}
@@ -8,6 +8,5 @@ public class QuickfixSpecific {
TableView<Pair<Integer, String>> table;
public void onSort(SortEvent<TableView<Pair<Integer, String>>> tableViewSortEvent) {
}
}
@@ -2,6 +2,5 @@ import javafx.scene.input.MouseEvent;
public class QuickfixSuper{
public void click(MouseEvent mouseEvent) {
}
}
@@ -9,6 +9,5 @@ public class CreateControllerMethodGeneric {
TableView<Map<Integer, String>> table;
public void onSort(SortEvent<TableView<Map<Integer, String>>> tableViewSortEvent) {
}
}
@@ -9,6 +9,5 @@ public class CreateControllerMethodHalfRaw {
TableView<Map> table;
public void onSort(SortEvent<TableView<Map>> tableViewSortEvent) {
}
}
@@ -1,7 +1,5 @@
import javafx.event.ActionEvent
public class CreateControllerMethodInGroovy {
public void bar(ActionEvent actionEvent) {
}
public void bar(ActionEvent actionEvent) {}
}
@@ -2,6 +2,5 @@ import javafx.event.ActionEvent;
public class CreateControllerMethod {
public void bar(ActionEvent actionEvent) {
}
}
@@ -4,6 +4,5 @@ import javafx.fxml.FXML;
public class CreateMethod {
@FXML
private void onAction(ActionEvent actionEvent) {
}
}
@@ -4,6 +4,5 @@ import javafx.fxml.FXML;
public class CreateMethod {
@FXML
void onAction(ActionEvent actionEvent) {
}
}
@@ -4,6 +4,5 @@ import javafx.fxml.FXML;
public class CreateMethod {
@FXML
private void onAction(ActionEvent actionEvent) {
}
}
@@ -4,6 +4,5 @@ import javafx.fxml.FXML;
public class CreateMethod {
@FXML
protected void onAction(ActionEvent actionEvent) {
}
}
@@ -2,6 +2,5 @@ import javafx.event.ActionEvent;
public class CreateMethod {
public void onAction(ActionEvent actionEvent) {
}
}