Skip to content

Commit 35b14a4

Browse files
committed
refactoring form component to support more usages like extension navigation and self inheritance #1098 #695
1 parent dd6b792 commit 35b14a4

File tree

8 files changed

+147
-138
lines changed

8 files changed

+147
-138
lines changed

src/fr/adrienbrault/idea/symfony2plugin/form/FormDefaultOptionsKeyReference.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
* @author Daniel Espendiller <daniel@espendiller.net>
1515
*/
1616
public class FormDefaultOptionsKeyReference extends PsiReferenceBase<PsiElement> implements PsiReference {
17+
@NotNull
18+
final private StringLiteralExpression element;
1719

18-
private StringLiteralExpression element;
19-
private String formType;
20+
@NotNull
21+
final private String formType;
2022

21-
public FormDefaultOptionsKeyReference(@NotNull StringLiteralExpression element, String formType) {
23+
public FormDefaultOptionsKeyReference(@NotNull StringLiteralExpression element, @NotNull String formType) {
2224
super(element);
2325
this.element = element;
2426
this.formType = formType;
@@ -27,8 +29,7 @@ public FormDefaultOptionsKeyReference(@NotNull StringLiteralExpression element,
2729
@Nullable
2830
@Override
2931
public PsiElement resolve() {
30-
31-
Collection<PsiElement> defaultOptionTargets = FormOptionsUtil.getDefaultOptionTargets(element, this.formType);
32+
Collection<PsiElement> defaultOptionTargets = FormOptionsUtil.getDefaultOptionTargets(element, formType);
3233
if(defaultOptionTargets.size() > 0) {
3334
return defaultOptionTargets.iterator().next();
3435
}
@@ -40,7 +41,7 @@ public PsiElement resolve() {
4041
@NotNull
4142
@Override
4243
public Object[] getVariants() {
43-
return FormOptionsUtil.getDefaultOptionLookupElements(getElement().getProject(), this.formType).toArray();
44+
return FormOptionsUtil.getDefaultOptionLookupElements(getElement().getProject(), formType).toArray();
4445
}
4546

4647
}
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
package fr.adrienbrault.idea.symfony2plugin.form;
22

3-
import com.intellij.psi.PsiElement;
4-
import com.intellij.psi.PsiReference;
5-
import com.intellij.psi.PsiReferenceBase;
3+
import com.intellij.psi.*;
64
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
75
import fr.adrienbrault.idea.symfony2plugin.form.util.FormOptionsUtil;
86
import org.jetbrains.annotations.NotNull;
97
import org.jetbrains.annotations.Nullable;
108

11-
import java.util.Collection;
9+
import java.util.*;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.Stream;
1212

1313
/**
1414
* @author Daniel Espendiller <daniel@espendiller.net>
1515
*/
16-
public class FormExtensionKeyReference extends PsiReferenceBase<PsiElement> implements PsiReference {
17-
18-
private StringLiteralExpression element;
16+
public class FormExtensionKeyReference extends PsiPolyVariantReferenceBase<PsiElement> {
17+
@NotNull
18+
final private StringLiteralExpression element;
1919

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

25-
public FormExtensionKeyReference(@NotNull StringLiteralExpression element) {
25+
public FormExtensionKeyReference(@NotNull StringLiteralExpression element, @Nullable String formType) {
2626
super(element);
2727
this.element = element;
28-
}
29-
30-
@Nullable
31-
@Override
32-
public PsiElement resolve() {
3328

34-
Collection<PsiElement> targets = FormOptionsUtil.getFormExtensionsKeysTargets(element, formTypes);
35-
if(targets.size() > 0) {
36-
return targets.iterator().next();
29+
if(formType != null) {
30+
this.formTypes.add(formType);
3731
}
32+
}
3833

39-
return null;
34+
@NotNull
35+
@Override
36+
public ResolveResult[] multiResolve(boolean b) {
37+
return PsiElementResolveResult.createResults(
38+
FormOptionsUtil.getFormExtensionsKeysTargets(element, formTypes.toArray(new String[formTypes.size()]))
39+
);
4040
}
4141

4242
@NotNull
4343
@Override
4444
public Object[] getVariants() {
45-
return FormOptionsUtil.getFormExtensionKeysLookupElements(getElement().getProject(), formTypes).toArray();
45+
return FormOptionsUtil.getFormExtensionKeysLookupElements(getElement().getProject(), formTypes.toArray(new String[formTypes.size()])).toArray();
4646
}
47-
4847
}

src/fr/adrienbrault/idea/symfony2plugin/form/FormGotoCompletionRegistrar.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void register(GotoCompletionRegistrarParameter registrar) {
6060
return new FormBuilderAddGotoCompletionProvider(parent);
6161
});
6262

63-
/**
63+
/*
6464
* $options lookup
6565
* public function createNamedBuilder($name, $type = 'form', $data = null, array $options = array())
6666
*/
@@ -80,11 +80,9 @@ public void register(GotoCompletionRegistrarParameter registrar) {
8080
}
8181

8282
return getFormProvider((StringLiteralExpression) parent, methodMatchParameter.getParameters()[1]);
83-
8483
});
8584

86-
87-
/**
85+
/*
8886
* $this->createForm(new FormType(), $entity, array('<foo_key>' => ''));
8987
* $this->createForm('foo', $entity, array('<foo_key>'));
9088
*/
@@ -229,11 +227,13 @@ public void register(GotoCompletionRegistrarParameter registrar) {
229227
* Form options on extension or form type default options
230228
*/
231229
private static class FormOptionsGotoCompletionProvider extends GotoCompletionProvider {
232-
230+
@NotNull
233231
private final String formType;
232+
233+
@NotNull
234234
private final Collection<FormOption> options;
235235

236-
public FormOptionsGotoCompletionProvider(@NotNull PsiElement element, @NotNull String formType, FormOption... options) {
236+
FormOptionsGotoCompletionProvider(@NotNull PsiElement element, @NotNull String formType, FormOption... options) {
237237
super(element);
238238
this.formType = formType;
239239
this.options = Arrays.asList(options);
@@ -242,7 +242,6 @@ public FormOptionsGotoCompletionProvider(@NotNull PsiElement element, @NotNull S
242242
@NotNull
243243
@Override
244244
public Collection<LookupElement> getLookupElements() {
245-
246245
Collection<LookupElement> lookupElements = new ArrayList<>();
247246

248247
if(options.contains(FormOption.EXTENSION)) {
@@ -259,7 +258,6 @@ public Collection<LookupElement> getLookupElements() {
259258
@NotNull
260259
@Override
261260
public Collection<PsiElement> getPsiTargets(PsiElement psiElement) {
262-
263261
PsiElement element = psiElement.getParent();
264262
if(!(element instanceof StringLiteralExpression)) {
265263
return Collections.emptyList();
@@ -276,16 +274,14 @@ public Collection<PsiElement> getPsiTargets(PsiElement psiElement) {
276274
}
277275

278276
return targets;
279-
280277
}
281278
}
282279

283280
/**
284281
* All registered form type with their getName() return alias name
285282
*/
286283
private static class FormBuilderAddGotoCompletionProvider extends GotoCompletionProvider {
287-
288-
public FormBuilderAddGotoCompletionProvider(PsiElement element) {
284+
FormBuilderAddGotoCompletionProvider(PsiElement element) {
289285
super(element);
290286
}
291287

@@ -298,7 +294,6 @@ public Collection<LookupElement> getLookupElements() {
298294
@NotNull
299295
@Override
300296
public Collection<PsiElement> getPsiTargets(PsiElement psiElement) {
301-
302297
PsiElement element = psiElement.getParent();
303298
if(!(element instanceof StringLiteralExpression)) {
304299
return Collections.emptyList();

src/fr/adrienbrault/idea/symfony2plugin/form/FormTypeReferenceContributor.java

+8-30
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
6060

6161
ArrayHashElement arrayHash = PsiTreeUtil.getParentOfType(psiElement, ArrayHashElement.class);
6262
if(arrayHash != null && arrayHash.getKey() instanceof StringLiteralExpression) {
63-
6463
ArrayCreationExpression arrayCreation = PsiTreeUtil.getParentOfType(psiElement, ArrayCreationExpression.class);
64+
6565
if(arrayCreation == null) {
6666
return new PsiReference[0];
6767
}
@@ -81,8 +81,8 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
8181

8282
// @TODO: how to handle custom bundle fields like help_block
8383
if(keyString.equals("label") || keyString.equals("help_block") || keyString.equals("help_inline") || keyString.equals("placeholder")) {
84-
8584
// translation_domain in current array block
85+
8686
String translationDomain = FormOptionsUtil.getTranslationFromScope(arrayCreation);
8787
if(translationDomain == null) {
8888
translationDomain = "messages";
@@ -94,15 +94,11 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
9494
if(keyString.equals("class")) {
9595
return new PsiReference[]{ new EntityReference((StringLiteralExpression) psiElement, true)};
9696
}
97-
9897
}
9998

10099
return new PsiReference[0];
101-
102100
}
103-
104101
}
105-
106102
);
107103

108104
/*
@@ -115,7 +111,6 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
115111
@NotNull
116112
@Override
117113
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {
118-
119114
// match add('foo', 'type name')
120115
MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(psiElement, 1)
121116
.withSignature(FormUtil.PHP_FORM_BUILDER_SIGNATURES)
@@ -133,11 +128,8 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
133128
}
134129

135130
return new PsiReference[]{ new FormTypeReferenceRef((StringLiteralExpression) psiElement) };
136-
137131
}
138-
139132
}
140-
141133
);
142134

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

181173
return new PsiReference[]{new FormUnderscoreMethodReference((StringLiteralExpression) psiElement, phpClass)};
182174
}
183-
184175
}
185-
186176
);
187177

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

229219
if(PhpElementsUtil.getCompletableArrayCreationElement(psiElement) != null) {
230220
return new PsiReference[]{
231-
new FormExtensionKeyReference((StringLiteralExpression) psiElement),
221+
new FormExtensionKeyReference((StringLiteralExpression) psiElement, FormUtil.getFormTypeClassFromScope(psiElement)),
232222
new FormDefaultOptionsKeyReference((StringLiteralExpression) psiElement, "form"),
233223
new FormDefaultOptionsKeyReference((StringLiteralExpression) psiElement, "Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType"),
234224
};
@@ -248,8 +238,6 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
248238
@NotNull
249239
@Override
250240
public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {
251-
252-
253241
MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(psiElement, 0)
254242
.withSignature("\\Symfony\\Component\\Form\\FormInterface", "get")
255243
.withSignature("\\Symfony\\Component\\Form\\FormInterface", "has")
@@ -268,23 +256,20 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
268256
new FormFieldNameReference((StringLiteralExpression) psiElement, method)
269257
};
270258
}
271-
272259
}
273-
274260
);
275261

276-
/**
262+
/*
277263
* $options
278264
* public function buildForm(FormBuilderInterface $builder, array $options) {
279265
* $options['foo']
280266
* }
281267
*
282268
* public function setDefaultOptions(OptionsResolverInterface $resolver) {
283-
* $resolver->setDefaults(array(
269+
* $resolver->setDefaults([
284270
* 'foo' => 'bar',
285-
* ));
286-
}
287-
271+
* ]);
272+
* }
288273
*/
289274
psiReferenceRegistrar.registerReferenceProvider(
290275
PlatformPatterns.psiElement(StringLiteralExpression.class),
@@ -334,21 +319,15 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @No
334319
}
335320

336321
return new PsiReference[]{
337-
new FormExtensionKeyReference((StringLiteralExpression) psiElement),
322+
new FormExtensionKeyReference((StringLiteralExpression) psiElement, FormUtil.getFormTypeClassFromScope(psiElement)),
338323
new FormDefaultOptionsKeyReference((StringLiteralExpression) psiElement, phpClass.getPresentableFQN())
339324
};
340-
341325
}
342-
343326
}
344-
345327
);
346-
347-
348328
}
349329

350330
private static class FormTypeReferenceRef extends FormTypeReference {
351-
352331
public FormTypeReferenceRef(@NotNull StringLiteralExpression element) {
353332
super(element);
354333
}
@@ -358,6 +337,5 @@ public FormTypeReferenceRef(@NotNull StringLiteralExpression element) {
358337
public Object[] getVariants() {
359338
return new Object[0];
360339
}
361-
362340
}
363341
}

src/fr/adrienbrault/idea/symfony2plugin/form/dict/FormOption.java

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.adrienbrault.idea.symfony2plugin.form.dict;
22

3+
import com.intellij.psi.PsiElement;
34
import org.jetbrains.annotations.NotNull;
45

56
import java.util.Collection;
@@ -9,22 +10,23 @@
910
* @author Daniel Espendiller <daniel@espendiller.net>
1011
*/
1112
public class FormOption {
12-
13+
@NotNull
1314
private final String option;
15+
16+
@NotNull
1417
private final FormClass formClass;
1518

19+
@NotNull
20+
private final Collection<PsiElement> psiElements = new HashSet<>();
21+
1622
@NotNull
1723
private final Collection<FormOptionEnum> optionEnum = new HashSet<>();
1824

19-
public FormOption(@NotNull String option, @NotNull FormClass formClass) {
25+
public FormOption(@NotNull String option, @NotNull FormClass formClass, @NotNull FormOptionEnum optionEnum, @NotNull PsiElement psiElement) {
2026
this.option = option;
2127
this.formClass = formClass;
22-
this.optionEnum.add(FormOptionEnum.DEFAULT);
23-
}
2428

25-
public FormOption(@NotNull String option, @NotNull FormClass formClass, @NotNull FormOptionEnum optionEnum) {
26-
this.option = option;
27-
this.formClass = formClass;
29+
this.psiElements.add(psiElement);
2830
this.optionEnum.add(optionEnum);
2931
}
3032

@@ -42,10 +44,17 @@ public FormClass getFormClass() {
4244
public Collection<FormOptionEnum> getOptionEnum() {
4345
return optionEnum;
4446
}
45-
@NotNull
46-
public FormOption addOptionEnum(@NotNull FormOptionEnum optionEnum) {
47+
48+
public void addOptionEnum(@NotNull FormOptionEnum optionEnum) {
4749
this.optionEnum.add(optionEnum);
48-
return this;
4950
}
5051

52+
public void addTarget(@NotNull PsiElement psiElement) {
53+
this.psiElements.add(psiElement);
54+
}
55+
56+
@NotNull
57+
public Collection<PsiElement> getPsiTargets() {
58+
return psiElements;
59+
}
5160
}

0 commit comments

Comments
 (0)