-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add form Twig fields widget generator
- Loading branch information
Showing
4 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/main/java/fr/adrienbrault/idea/symfony2plugin/twig/action/TwigFormFieldGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.twig.action; | ||
|
||
import com.intellij.codeInsight.CodeInsightActionHandler; | ||
import com.intellij.codeInsight.actions.CodeInsightAction; | ||
import com.intellij.openapi.command.WriteCommandAction; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.ui.popup.JBPopupFactory; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.impl.source.html.HtmlFileImpl; | ||
import com.jetbrains.php.PhpIndex; | ||
import com.jetbrains.php.completion.insert.PhpInsertHandlerUtil; | ||
import com.jetbrains.php.lang.psi.elements.PhpClass; | ||
import com.jetbrains.php.lang.psi.resolve.types.PhpType; | ||
import com.jetbrains.twig.TwigFile; | ||
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; | ||
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigTypeResolveUtil; | ||
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil; | ||
import fr.adrienbrault.idea.symfony2plugin.templating.variable.dict.PsiVariable; | ||
import fr.adrienbrault.idea.symfony2plugin.templating.variable.resolver.FormFieldResolver; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
public class TwigFormFieldGenerator extends CodeInsightAction { | ||
@Override | ||
protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { | ||
return Symfony2ProjectComponent.isEnabled(project) && ( | ||
file instanceof TwigFile | ||
|| (file instanceof HtmlFileImpl && file.getName().toLowerCase().endsWith(".twig")) | ||
|| TwigUtil.getInjectedTwigElement(file, editor) != null | ||
); | ||
} | ||
|
||
protected CodeInsightActionHandler getHandler() { | ||
return new TwigFormFieldGenerator.MyCodeInsightActionHandler(); | ||
} | ||
|
||
private static class MyCodeInsightActionHandler implements CodeInsightActionHandler { | ||
@Override | ||
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile psiFile) { | ||
PsiElement psiElement = TwigUtil.getInjectedTwigElement(psiFile, editor); | ||
if (psiElement == null) { | ||
return; | ||
} | ||
|
||
Collection<JBFormFieldItem> phpClasses = new ArrayList<>(); | ||
|
||
for (Map.Entry<String, PsiVariable> entry : TwigTypeResolveUtil.collectScopeVariables(psiElement).entrySet()) { | ||
PhpType phpType = PhpIndex.getInstance(project).completeType(project, PhpType.from(entry.getValue().getTypes().toArray(new String[0])), new HashSet<>()); | ||
if (phpType.types().noneMatch(s -> s.equals("\\Symfony\\Component\\Form\\FormView"))) { | ||
continue; | ||
} | ||
|
||
PsiElement element = entry.getValue().getElement(); | ||
if (element == null) { | ||
continue; | ||
} | ||
|
||
for (PhpClass phpClass : FormFieldResolver.getFormTypeFromFormFactory(element)) { | ||
phpClasses.add(new JBFormFieldItem(entry.getKey(), phpClass)); | ||
} | ||
} | ||
|
||
if (phpClasses.size() == 1) { | ||
extracted(project, editor, phpClasses.stream().iterator().next()); | ||
return; | ||
} | ||
|
||
JBPopupFactory.getInstance().createPopupChooserBuilder(new ArrayList<>(phpClasses)) | ||
.setTitle("Symfony: Select FormType") | ||
.setItemChosenCallback(strings -> WriteCommandAction.runWriteCommandAction(project, "", null, () -> { | ||
extracted(project, editor, strings); | ||
})) | ||
.createPopup() | ||
.showInBestPositionFor(editor); | ||
} | ||
|
||
private static void extracted(@NotNull Project project, @NotNull Editor editor, @NotNull TwigFormFieldGenerator.JBFormFieldItem next) { | ||
Collection<String> fields = new HashSet<>(); | ||
FormFieldResolver.visitFormReferencesFields(next.phpClass(), twigTypeContainer -> fields.add(twigTypeContainer.getStringElement())); | ||
|
||
JBPopupFactory.getInstance().createPopupChooserBuilder(new ArrayList<>(fields)) | ||
.setTitle(String.format("Symfony: Select Form Fields \"%s\"", StringUtils.abbreviate(next.phpClass.getName(), 20))) | ||
.setItemsChosenCallback(strings -> WriteCommandAction.runWriteCommandAction(project, "", null, () -> { | ||
StringBuilder s = new StringBuilder(); | ||
|
||
for (String string : strings) { | ||
s.append(String.format("{{ form_widget(%s.%s) }}\n", next.key, string)); | ||
} | ||
|
||
PhpInsertHandlerUtil.insertStringAtCaret(editor, s.toString()); | ||
})) | ||
.createPopup() | ||
.showInBestPositionFor(editor); | ||
} | ||
} | ||
|
||
public record JBFormFieldItem(@NotNull String key, @NotNull PhpClass phpClass) { | ||
@Override | ||
public String toString() { | ||
return key + " => " + phpClass.getName(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters