Skip to content
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 @@ -14,11 +14,13 @@
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class FormDefaultOptionsKeyReference extends PsiReferenceBase<PsiElement> implements PsiReference {
@NotNull
final private StringLiteralExpression element;

private StringLiteralExpression element;
private String formType;
@NotNull
final private String formType;

public FormDefaultOptionsKeyReference(@NotNull StringLiteralExpression element, String formType) {
public FormDefaultOptionsKeyReference(@NotNull StringLiteralExpression element, @NotNull String formType) {
super(element);
this.element = element;
this.formType = formType;
Expand All @@ -27,8 +29,7 @@ public FormDefaultOptionsKeyReference(@NotNull StringLiteralExpression element,
@Nullable
@Override
public PsiElement resolve() {

Collection<PsiElement> defaultOptionTargets = FormOptionsUtil.getDefaultOptionTargets(element, this.formType);
Collection<PsiElement> defaultOptionTargets = FormOptionsUtil.getDefaultOptionTargets(element, formType);
if(defaultOptionTargets.size() > 0) {
return defaultOptionTargets.iterator().next();
}
Expand All @@ -40,7 +41,7 @@ public PsiElement resolve() {
@NotNull
@Override
public Object[] getVariants() {
return FormOptionsUtil.getDefaultOptionLookupElements(getElement().getProject(), this.formType).toArray();
return FormOptionsUtil.getDefaultOptionLookupElements(getElement().getProject(), formType).toArray();
}

}
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
package fr.adrienbrault.idea.symfony2plugin.form;

import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.psi.*;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.form.util.FormOptionsUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class FormExtensionKeyReference extends PsiReferenceBase<PsiElement> implements PsiReference {

private StringLiteralExpression element;
public class FormExtensionKeyReference extends PsiPolyVariantReferenceBase<PsiElement> {
@NotNull
final private StringLiteralExpression element;

private String[] formTypes = new String[] {
"form",
"Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType",
};
@NotNull
final private Set<String> formTypes = Stream
.of("form", "Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType")
.collect(Collectors.toCollection(HashSet::new));

public FormExtensionKeyReference(@NotNull StringLiteralExpression element) {
public FormExtensionKeyReference(@NotNull StringLiteralExpression element, @Nullable String formType) {
super(element);
this.element = element;
}

@Nullable
@Override
public PsiElement resolve() {

Collection<PsiElement> targets = FormOptionsUtil.getFormExtensionsKeysTargets(element, formTypes);
if(targets.size() > 0) {
return targets.iterator().next();
if(formType != null) {
this.formTypes.add(formType);
}
}

return null;
@NotNull
@Override
public ResolveResult[] multiResolve(boolean b) {
return PsiElementResolveResult.createResults(
FormOptionsUtil.getFormExtensionsKeysTargets(element, formTypes.toArray(new String[formTypes.size()]))
);
}

@NotNull
@Override
public Object[] getVariants() {
return FormOptionsUtil.getFormExtensionKeysLookupElements(getElement().getProject(), formTypes).toArray();
return FormOptionsUtil.getFormExtensionKeysLookupElements(getElement().getProject(), formTypes.toArray(new String[formTypes.size()])).toArray();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void register(GotoCompletionRegistrarParameter registrar) {
return new FormBuilderAddGotoCompletionProvider(parent);
});

/**
/*
* $options lookup
* public function createNamedBuilder($name, $type = 'form', $data = null, array $options = array())
*/
Expand All @@ -80,11 +80,9 @@ public void register(GotoCompletionRegistrarParameter registrar) {
}

return getFormProvider((StringLiteralExpression) parent, methodMatchParameter.getParameters()[1]);

});


/**
/*
* $this->createForm(new FormType(), $entity, array('<foo_key>' => ''));
* $this->createForm('foo', $entity, array('<foo_key>'));
*/
Expand Down Expand Up @@ -229,11 +227,13 @@ public void register(GotoCompletionRegistrarParameter registrar) {
* Form options on extension or form type default options
*/
private static class FormOptionsGotoCompletionProvider extends GotoCompletionProvider {

@NotNull
private final String formType;

@NotNull
private final Collection<FormOption> options;

public FormOptionsGotoCompletionProvider(@NotNull PsiElement element, @NotNull String formType, FormOption... options) {
FormOptionsGotoCompletionProvider(@NotNull PsiElement element, @NotNull String formType, FormOption... options) {
super(element);
this.formType = formType;
this.options = Arrays.asList(options);
Expand All @@ -242,7 +242,6 @@ public FormOptionsGotoCompletionProvider(@NotNull PsiElement element, @NotNull S
@NotNull
@Override
public Collection<LookupElement> getLookupElements() {

Collection<LookupElement> lookupElements = new ArrayList<>();

if(options.contains(FormOption.EXTENSION)) {
Expand All @@ -259,7 +258,6 @@ public Collection<LookupElement> getLookupElements() {
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(PsiElement psiElement) {

PsiElement element = psiElement.getParent();
if(!(element instanceof StringLiteralExpression)) {
return Collections.emptyList();
Expand All @@ -276,16 +274,14 @@ public Collection<PsiElement> getPsiTargets(PsiElement psiElement) {
}

return targets;

}
}

/**
* All registered form type with their getName() return alias name
*/
private static class FormBuilderAddGotoCompletionProvider extends GotoCompletionProvider {

public FormBuilderAddGotoCompletionProvider(PsiElement element) {
FormBuilderAddGotoCompletionProvider(PsiElement element) {
super(element);
}

Expand All @@ -298,7 +294,6 @@ public Collection<LookupElement> getLookupElements() {
@NotNull
@Override
public Collection<PsiElement> getPsiTargets(PsiElement psiElement) {

PsiElement element = psiElement.getParent();
if(!(element instanceof StringLiteralExpression)) {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No

ArrayHashElement arrayHash = PsiTreeUtil.getParentOfType(psiElement, ArrayHashElement.class);
if(arrayHash != null && arrayHash.getKey() instanceof StringLiteralExpression) {

ArrayCreationExpression arrayCreation = PsiTreeUtil.getParentOfType(psiElement, ArrayCreationExpression.class);

if(arrayCreation == null) {
return new PsiReference[0];
}
Expand All @@ -81,8 +81,8 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No

// @TODO: how to handle custom bundle fields like help_block
if(keyString.equals("label") || keyString.equals("help_block") || keyString.equals("help_inline") || keyString.equals("placeholder")) {

// translation_domain in current array block

String translationDomain = FormOptionsUtil.getTranslationFromScope(arrayCreation);
if(translationDomain == null) {
translationDomain = "messages";
Expand All @@ -94,15 +94,11 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
if(keyString.equals("class")) {
return new PsiReference[]{ new EntityReference((StringLiteralExpression) psiElement, true)};
}

}

return new PsiReference[0];

}

}

);

/*
Expand All @@ -115,7 +111,6 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {

// match add('foo', 'type name')
MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(psiElement, 1)
.withSignature(FormUtil.PHP_FORM_BUILDER_SIGNATURES)
Expand All @@ -133,11 +128,8 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
}

return new PsiReference[]{ new FormTypeReferenceRef((StringLiteralExpression) psiElement) };

}

}

);

// FormBuilderInterface::add('underscore_method')
Expand Down Expand Up @@ -180,9 +172,7 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No

return new PsiReference[]{new FormUnderscoreMethodReference((StringLiteralExpression) psiElement, phpClass)};
}

}

);

// TODO: migrate to FormGotoCompletionRegistrar for better performance as lazy condition
Expand Down Expand Up @@ -228,7 +218,7 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No

if(PhpElementsUtil.getCompletableArrayCreationElement(psiElement) != null) {
return new PsiReference[]{
new FormExtensionKeyReference((StringLiteralExpression) psiElement),
new FormExtensionKeyReference((StringLiteralExpression) psiElement, FormUtil.getFormTypeClassFromScope(psiElement)),
new FormDefaultOptionsKeyReference((StringLiteralExpression) psiElement, "form"),
new FormDefaultOptionsKeyReference((StringLiteralExpression) psiElement, "Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType"),
};
Expand All @@ -248,8 +238,6 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {


MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(psiElement, 0)
.withSignature("\\Symfony\\Component\\Form\\FormInterface", "get")
.withSignature("\\Symfony\\Component\\Form\\FormInterface", "has")
Expand All @@ -268,23 +256,20 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
new FormFieldNameReference((StringLiteralExpression) psiElement, method)
};
}

}

);

/**
/*
* $options
* public function buildForm(FormBuilderInterface $builder, array $options) {
* $options['foo']
* }
*
* public function setDefaultOptions(OptionsResolverInterface $resolver) {
* $resolver->setDefaults(array(
* $resolver->setDefaults([
* 'foo' => 'bar',
* ));
}

* ]);
* }
*/
psiReferenceRegistrar.registerReferenceProvider(
PlatformPatterns.psiElement(StringLiteralExpression.class),
Expand Down Expand Up @@ -334,21 +319,15 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
}

return new PsiReference[]{
new FormExtensionKeyReference((StringLiteralExpression) psiElement),
new FormExtensionKeyReference((StringLiteralExpression) psiElement, FormUtil.getFormTypeClassFromScope(psiElement)),
new FormDefaultOptionsKeyReference((StringLiteralExpression) psiElement, phpClass.getPresentableFQN())
};

}

}

);


}

private static class FormTypeReferenceRef extends FormTypeReference {

public FormTypeReferenceRef(@NotNull StringLiteralExpression element) {
super(element);
}
Expand All @@ -358,6 +337,5 @@ public FormTypeReferenceRef(@NotNull StringLiteralExpression element) {
public Object[] getVariants() {
return new Object[0];
}

}
}
29 changes: 19 additions & 10 deletions src/fr/adrienbrault/idea/symfony2plugin/form/dict/FormOption.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.adrienbrault.idea.symfony2plugin.form.dict;

import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
Expand All @@ -9,22 +10,23 @@
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class FormOption {

@NotNull
private final String option;

@NotNull
private final FormClass formClass;

@NotNull
private final Collection<PsiElement> psiElements = new HashSet<>();

@NotNull
private final Collection<FormOptionEnum> optionEnum = new HashSet<>();

public FormOption(@NotNull String option, @NotNull FormClass formClass) {
public FormOption(@NotNull String option, @NotNull FormClass formClass, @NotNull FormOptionEnum optionEnum, @NotNull PsiElement psiElement) {
this.option = option;
this.formClass = formClass;
this.optionEnum.add(FormOptionEnum.DEFAULT);
}

public FormOption(@NotNull String option, @NotNull FormClass formClass, @NotNull FormOptionEnum optionEnum) {
this.option = option;
this.formClass = formClass;
this.psiElements.add(psiElement);
this.optionEnum.add(optionEnum);
}

Expand All @@ -42,10 +44,17 @@ public FormClass getFormClass() {
public Collection<FormOptionEnum> getOptionEnum() {
return optionEnum;
}
@NotNull
public FormOption addOptionEnum(@NotNull FormOptionEnum optionEnum) {

public void addOptionEnum(@NotNull FormOptionEnum optionEnum) {
this.optionEnum.add(optionEnum);
return this;
}

public void addTarget(@NotNull PsiElement psiElement) {
this.psiElements.add(psiElement);
}

@NotNull
public Collection<PsiElement> getPsiTargets() {
return psiElements;
}
}
Loading