Skip to content

Commit

Permalink
Merge pull request #2370 from Haehnchen/feature/form-type-guess
Browse files Browse the repository at this point in the history
form builder field completion, should support function+property types and greyout already existing fields
  • Loading branch information
Haehnchen authored May 5, 2024
2 parents ffb8557 + 40d4144 commit a365956
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import fr.adrienbrault.idea.symfony2plugin.form.FormUnderscoreMethodReference;
import fr.adrienbrault.idea.symfony2plugin.form.util.FormOptionsUtil;
import fr.adrienbrault.idea.symfony2plugin.form.util.FormUtil;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.resolver.FormFieldResolver;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import kotlin.Pair;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -60,9 +61,24 @@ protected void addCompletions(@NotNull CompletionParameters completionParameters
return;
}

Set<String> alreadyKnownFields = new HashSet<>();
String formTypeClassFromScope = FormUtil.getFormTypeClassFromScope(parent);
if (formTypeClassFromScope != null) {
PhpClass clazz = PhpElementsUtil.getClassInterface(project, formTypeClassFromScope);
if (clazz != null) {
FormFieldResolver.visitFormReferencesFields(clazz, twigTypeContainer -> alreadyKnownFields.add(twigTypeContainer.getStringElement()));
}
}

FormUnderscoreMethodReference.visitPropertyPath(
phpClass,
pair -> completionResultSet.addElement(new MyLookupElement(pair.getFirst(), "add", pair.getSecond(), phpClass.getName()))
pair -> completionResultSet.addElement(new MyLookupElement(
pair.getFirst(),
"add",
pair.getSecond(),
phpClass.getName(),
alreadyKnownFields.contains(pair.getFirst())
))
);
}
}
Expand Down Expand Up @@ -140,19 +156,28 @@ private class MyLookupElement extends com.intellij.codeInsight.lookup.LookupElem

private final PhpNamedElement phpNamedElement;
private final String typeText;
private final @NotNull boolean exists;

public MyLookupElement(@NotNull String key, @NotNull String lookupElement, @NotNull PhpNamedElement phpNamedElement, @NotNull String typeText) {
public MyLookupElement(@NotNull String key, @NotNull String lookupElement, @NotNull PhpNamedElement phpNamedElement, @NotNull String typeText, boolean exists) {
this.key = key;
this.lookupElement = lookupElement;
this.phpNamedElement = phpNamedElement;
this.typeText = typeText;
this.exists = exists;
}

@Override
public void renderElement(@NotNull LookupElementPresentation presentation) {
super.renderElement(presentation);

if (this.exists) {
presentation.setTypeText(typeText, Symfony2Icons.SYMFONY_AI_OPACITY);
presentation.setTypeGrayed(true);
} else {
presentation.setTypeText(typeText, Symfony2Icons.SYMFONY_AI);
}

presentation.setIcon(phpNamedElement.getIcon());
presentation.setTypeText(typeText, Symfony2Icons.SYMFONY);
presentation.setTypeIconRightAligned(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,21 @@ public static Collection<String> getFormTypeParentFromFormTypeImplementation(@No
}

public static Pair<String, Map<String, String>> getGuessedFormFieldParameters(@NotNull PhpIndex phpIndex, @NotNull Project project, @NotNull String key, @NotNull PhpNamedElement phpNamedElement) {
PhpType phpType = phpIndex.completeType(project, phpNamedElement.getType(), new HashSet<>());
// method / function
PhpType phpType = null;
if (phpNamedElement instanceof Function function) {
PsiElement parameter = function.getParameter(0);
if (parameter instanceof PhpTypedElement phpTypedElement) {
phpType = phpIndex.completeType(project, phpTypedElement.getType(), new HashSet<>());
}
} else {
// properties
phpType = phpIndex.completeType(project, phpNamedElement.getType(), new HashSet<>());
}

if (phpType == null) {
return new Pair<>("\\Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType", null);
}

String typeClass = null;
Map<String, String> options = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public static void visitFormReferencesFields(@NotNull PsiElement formReference,
}

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

0 comments on commit a365956

Please sign in to comment.