|
| 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 | +} |
0 commit comments