Skip to content

Commit 40d4144

Browse files
committed
form builder field completion, should support function+property types and greyout already existing fields
1 parent ffb8557 commit 40d4144

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/completion/PhpIncompleteCompletionContributor.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import fr.adrienbrault.idea.symfony2plugin.form.FormUnderscoreMethodReference;
2121
import fr.adrienbrault.idea.symfony2plugin.form.util.FormOptionsUtil;
2222
import fr.adrienbrault.idea.symfony2plugin.form.util.FormUtil;
23+
import fr.adrienbrault.idea.symfony2plugin.templating.variable.resolver.FormFieldResolver;
2324
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
2425
import kotlin.Pair;
2526
import org.apache.commons.lang3.StringUtils;
@@ -60,9 +61,24 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
6061
return;
6162
}
6263

64+
Set<String> alreadyKnownFields = new HashSet<>();
65+
String formTypeClassFromScope = FormUtil.getFormTypeClassFromScope(parent);
66+
if (formTypeClassFromScope != null) {
67+
PhpClass clazz = PhpElementsUtil.getClassInterface(project, formTypeClassFromScope);
68+
if (clazz != null) {
69+
FormFieldResolver.visitFormReferencesFields(clazz, twigTypeContainer -> alreadyKnownFields.add(twigTypeContainer.getStringElement()));
70+
}
71+
}
72+
6373
FormUnderscoreMethodReference.visitPropertyPath(
6474
phpClass,
65-
pair -> completionResultSet.addElement(new MyLookupElement(pair.getFirst(), "add", pair.getSecond(), phpClass.getName()))
75+
pair -> completionResultSet.addElement(new MyLookupElement(
76+
pair.getFirst(),
77+
"add",
78+
pair.getSecond(),
79+
phpClass.getName(),
80+
alreadyKnownFields.contains(pair.getFirst())
81+
))
6682
);
6783
}
6884
}
@@ -140,19 +156,28 @@ private class MyLookupElement extends com.intellij.codeInsight.lookup.LookupElem
140156

141157
private final PhpNamedElement phpNamedElement;
142158
private final String typeText;
159+
private final @NotNull boolean exists;
143160

144-
public MyLookupElement(@NotNull String key, @NotNull String lookupElement, @NotNull PhpNamedElement phpNamedElement, @NotNull String typeText) {
161+
public MyLookupElement(@NotNull String key, @NotNull String lookupElement, @NotNull PhpNamedElement phpNamedElement, @NotNull String typeText, boolean exists) {
145162
this.key = key;
146163
this.lookupElement = lookupElement;
147164
this.phpNamedElement = phpNamedElement;
148165
this.typeText = typeText;
166+
this.exists = exists;
149167
}
150168

151169
@Override
152170
public void renderElement(@NotNull LookupElementPresentation presentation) {
153171
super.renderElement(presentation);
172+
173+
if (this.exists) {
174+
presentation.setTypeText(typeText, Symfony2Icons.SYMFONY_AI_OPACITY);
175+
presentation.setTypeGrayed(true);
176+
} else {
177+
presentation.setTypeText(typeText, Symfony2Icons.SYMFONY_AI);
178+
}
179+
154180
presentation.setIcon(phpNamedElement.getIcon());
155-
presentation.setTypeText(typeText, Symfony2Icons.SYMFONY);
156181
presentation.setTypeIconRightAligned(true);
157182
}
158183

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,21 @@ public static Collection<String> getFormTypeParentFromFormTypeImplementation(@No
760760
}
761761

762762
public static Pair<String, Map<String, String>> getGuessedFormFieldParameters(@NotNull PhpIndex phpIndex, @NotNull Project project, @NotNull String key, @NotNull PhpNamedElement phpNamedElement) {
763-
PhpType phpType = phpIndex.completeType(project, phpNamedElement.getType(), new HashSet<>());
763+
// method / function
764+
PhpType phpType = null;
765+
if (phpNamedElement instanceof Function function) {
766+
PsiElement parameter = function.getParameter(0);
767+
if (parameter instanceof PhpTypedElement phpTypedElement) {
768+
phpType = phpIndex.completeType(project, phpTypedElement.getType(), new HashSet<>());
769+
}
770+
} else {
771+
// properties
772+
phpType = phpIndex.completeType(project, phpNamedElement.getType(), new HashSet<>());
773+
}
774+
775+
if (phpType == null) {
776+
return new Pair<>("\\Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType", null);
777+
}
764778

765779
String typeClass = null;
766780
Map<String, String> options = new HashMap<>();

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/variable/resolver/FormFieldResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static void visitFormReferencesFields(@NotNull PsiElement formReference,
188188
}
189189

190190
/**
191-
* Field all form fields in given PhpClass which should already be just a form type
191+
* Visit all form fields in given PhpClass which are already a form type
192192
*/
193193
public static void visitFormReferencesFields(@NotNull PhpClass phpClass, @NotNull Consumer<TwigTypeContainer> consumer) {
194194
visitFormReferencesFields(phpClass.getProject(), Collections.singleton(phpClass), consumer);

0 commit comments

Comments
 (0)