Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

form builder field completion, should support function+property types and greyout already existing fields #2370

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading