Skip to content

Commit b18c0ba

Browse files
committed
add generator which migrates string values of form type into class contents #623
1 parent 2fd1249 commit b18c0ba

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

META-INF/plugin.xml

+4
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,10 @@
628628
<add-to-group group-id="GenerateGroup" anchor="last" />
629629
</action>
630630

631+
<action id="Symfony2.FormTypeMigrationGenerator" class="fr.adrienbrault.idea.symfony2plugin.form.action.generator.FormTypeConstantMigrationAction" text="Class constants migration" icon="SymfonyIcons.FormType">
632+
<add-to-group group-id="GenerateGroup" anchor="last" />
633+
</action>
634+
631635
<group id="Symfony2Group" text="Symfony" popup="false">
632636
<group id="Symfony2GroupService" class="com.intellij.ide.actions.NonTrivialActionGroup" text="Service" popup="true" icon="SymfonyIcons.Symfony">
633637
<action id="Symfony2NewXmlService" class="fr.adrienbrault.idea.symfony2plugin.action.NewXmlServiceAction"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package fr.adrienbrault.idea.symfony2plugin.form.action.generator;
2+
3+
import com.intellij.codeInsight.CodeInsightActionHandler;
4+
import com.intellij.codeInsight.actions.CodeInsightAction;
5+
import com.intellij.codeInsight.hint.HintManager;
6+
import com.intellij.openapi.actionSystem.AnActionEvent;
7+
import com.intellij.openapi.editor.Editor;
8+
import com.intellij.openapi.project.Project;
9+
import com.intellij.psi.PsiElement;
10+
import com.intellij.psi.PsiFile;
11+
import com.intellij.psi.PsiRecursiveElementVisitor;
12+
import com.jetbrains.php.lang.psi.PhpCodeEditUtil;
13+
import com.jetbrains.php.lang.psi.elements.PhpClass;
14+
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
15+
import fr.adrienbrault.idea.symfony2plugin.Symfony2InterfacesUtil;
16+
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
17+
import fr.adrienbrault.idea.symfony2plugin.form.util.FormUtil;
18+
import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher;
19+
import org.apache.commons.lang.StringUtils;
20+
import org.jetbrains.annotations.NotNull;
21+
22+
import java.util.ArrayList;
23+
import java.util.Collection;
24+
25+
/**
26+
* @author Daniel Espendiller <daniel@espendiller.net>
27+
*/
28+
public class FormTypeConstantMigrationAction extends CodeInsightAction {
29+
30+
@Override
31+
public void update(AnActionEvent event) {
32+
super.update(event);
33+
event.getPresentation().setVisible(Symfony2ProjectComponent.isEnabled(event.getProject()));
34+
}
35+
36+
@Override
37+
protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
38+
PhpClass classAtCaret = PhpCodeEditUtil.findClassAtCaret(editor, file);
39+
40+
return
41+
classAtCaret != null &&
42+
new Symfony2InterfacesUtil().isInstanceOf(classAtCaret, "Symfony\\Component\\Form\\FormTypeInterface")
43+
;
44+
}
45+
46+
@NotNull
47+
@Override
48+
protected CodeInsightActionHandler getHandler() {
49+
return new MyCodeInsightActionHandler();
50+
}
51+
52+
private class MyCodeInsightActionHandler implements CodeInsightActionHandler {
53+
@Override
54+
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile psiFile) {
55+
PhpClass phpClass = PhpCodeEditUtil.findClassAtCaret(editor, psiFile);
56+
if(phpClass == null) {
57+
HintManager.getInstance().showErrorHint(editor, "No class context found");
58+
return;
59+
}
60+
61+
final Collection<StringLiteralExpression> formTypes = new ArrayList<StringLiteralExpression>();
62+
phpClass.acceptChildren(new FormTypeStringElementVisitor(formTypes));
63+
64+
if(formTypes.size() == 0) {
65+
HintManager.getInstance().showErrorHint(editor, "Nothing to do for me");
66+
return;
67+
}
68+
69+
for (StringLiteralExpression formType : formTypes) {
70+
try {
71+
FormUtil.replaceFormStringAliasWithClassConstant(formType);
72+
} catch (Exception ignored) {
73+
}
74+
}
75+
76+
}
77+
78+
@Override
79+
public boolean startInWriteAction() {
80+
return true;
81+
}
82+
83+
private class FormTypeStringElementVisitor extends PsiRecursiveElementVisitor {
84+
private final Collection<StringLiteralExpression> formTypes;
85+
86+
public FormTypeStringElementVisitor(Collection<StringLiteralExpression> formTypes) {
87+
this.formTypes = formTypes;
88+
}
89+
90+
@Override
91+
public void visitElement(PsiElement element) {
92+
if (!(element instanceof StringLiteralExpression)) {
93+
super.visitElement(element);
94+
return;
95+
}
96+
97+
String contents = ((StringLiteralExpression) element).getContents();
98+
if (StringUtils.isBlank(contents)) {
99+
super.visitElement(element);
100+
return;
101+
}
102+
103+
if (null == new MethodMatcher.StringParameterMatcher(element, 1)
104+
.withSignature(Symfony2InterfacesUtil.getFormBuilderInterface())
105+
.match()) {
106+
107+
super.visitElement(element);
108+
return;
109+
}
110+
111+
formTypes.add((StringLiteralExpression) element);
112+
113+
super.visitElement(element);
114+
}
115+
}
116+
}
117+
}

src/fr/adrienbrault/idea/symfony2plugin/form/util/FormUtil.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public static void replaceFormStringAliasWithClassConstant(@NotNull StringLitera
476476
throw new Exception("Empty content");
477477
}
478478

479-
PhpClass phpClass = PhpElementsUtil.findSubclassWithMethodReturnString(psiElement.getProject(), "Symfony\\Component\\Form\\FormTypeInterface", "getName", contents);
479+
PhpClass phpClass = getFormTypeToClass(psiElement.getProject(), contents);
480480
if(phpClass == null) {
481481
throw new Exception("No class found");
482482
}

src/icons/SymfonyIcons.java

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55

66
public class SymfonyIcons {
77
public static final Icon Symfony = Symfony2Icons.SYMFONY;
8+
public static final Icon FormType = Symfony2Icons.FORM_TYPE;
89
public static final Icon SymfonyToolWindow = Symfony2Icons.SYMFONY_TOOL_WINDOW;
910
}

0 commit comments

Comments
 (0)