-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathTwigTemplateGoToDeclarationHandler.java
More file actions
677 lines (561 loc) · 27.9 KB
/
TwigTemplateGoToDeclarationHandler.java
File metadata and controls
677 lines (561 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
package fr.adrienbrault.idea.symfony2plugin.templating;
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.Field;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.twig.TwigLanguage;
import com.jetbrains.twig.TwigTokenTypes;
import com.jetbrains.twig.elements.TwigBlockTag;
import com.jetbrains.twig.elements.TwigElementTypes;
import com.jetbrains.twig.elements.TwigPsiReference;
import com.jetbrains.twig.elements.TwigVariableReference;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.assetMapper.AssetMapperUtil;
import fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper;
import fr.adrienbrault.idea.symfony2plugin.templating.dict.TwigExtension;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigConstantEnumResolver;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigExtensionParser;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigTypeResolveUtil;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.TwigTypeContainer;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.resolver.holder.FormFieldDataHolder;
import fr.adrienbrault.idea.symfony2plugin.translation.dict.TranslationUtil;
import fr.adrienbrault.idea.symfony2plugin.twig.utils.TwigBlockUtil;
import fr.adrienbrault.idea.symfony2plugin.twig.variable.collector.ControllerDocVariableCollector;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import fr.adrienbrault.idea.symfony2plugin.util.UxUtil;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Adrien Brault <adrien.brault@gmail.com>
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class TwigTemplateGoToDeclarationHandler implements GotoDeclarationHandler {
@Nullable
@Override
public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int offset, Editor editor) {
if (!Symfony2ProjectComponent.isEnabled(psiElement) || !PlatformPatterns.psiElement().withLanguage(TwigLanguage.INSTANCE).accepts(psiElement)) {
return null;
}
Collection<PsiElement> targets = new ArrayList<>();
if (TwigPattern.getBlockTagPattern().accepts(psiElement)) {
targets.addAll(TwigBlockUtil.getBlockOverwriteTargets(psiElement));
}
if (TwigPattern.getPathAfterLeafPattern().accepts(psiElement) || TwigPattern.getPathAfterLeafForIdentifierPattern().accepts(psiElement)) {
targets.addAll(getRouteParameterGoTo(psiElement));
}
if (TwigPattern.getTemplateFileReferenceTagPattern().accepts(psiElement)
|| TwigPattern.getPrintBlockOrTagFunctionPattern("include", "source").accepts(psiElement)
|| TwigPattern.getPrintBlockOrTagFunctionSecondParameterPattern("block").accepts(psiElement))
{
// support: {% include() %}, {{ include() }}
targets.addAll(getTwigFiles(psiElement, offset));
} else if (PlatformPatterns.psiElement(TwigTokenTypes.STRING_TEXT).withText(PlatformPatterns.string().endsWith(".twig")).accepts(psiElement)) {
// provide global twig file resolving
// just if we dont match against known file references pattern
targets.addAll(getTwigFiles(psiElement, offset));
}
if (TwigPattern.getAutocompletableRoutePattern().accepts(psiElement)) {
targets.addAll(getRouteGoTo(psiElement));
}
// app.request.attributes.get('_route') == '<caret>'
// app.request.attributes.get('_route') != '<caret>'
// app.request.attributes.get('_route') is same as('<caret>')
// app.request.attributes.get('_route') in ['<caret>']
if ((TwigPattern.getTwigRouteComparePattern().accepts(psiElement)
|| TwigPattern.getTwigRouteSameAsPattern().accepts(psiElement)
|| TwigPattern.getTwigRouteInArrayPattern().accepts(psiElement))
&& TwigPattern.isRouteCompareContext(psiElement))
{
targets.addAll(getRouteGoTo(psiElement));
}
// app.request.attributes.get('_route') starts with '<caret>'
// app.request.attributes.get('_route') starts with('<caret>')
// The string is a prefix — navigate to all routes whose names start with it
if (TwigPattern.getTwigRouteStartsWithPattern().accepts(psiElement)
&& TwigPattern.isRouteCompareContext(psiElement))
{
targets.addAll(getRouteStartsWithGoTo(psiElement));
}
// {{ component('<caret>'}) }}
// {% component FOO
if (TwigPattern.getComponentPattern().accepts(psiElement) || TwigPattern.getArgumentAfterTagNamePattern("component").accepts(psiElement)) {
targets.addAll(getComponentGoTo(psiElement));
}
// find trans('', {}, '|')
// tricky way to get the function string trans(...)
if (TwigPattern.getTransDomainPattern().accepts(psiElement)) {
PsiElement psiElementTrans = PsiElementUtils.getPrevSiblingOfType(psiElement, PlatformPatterns.psiElement(TwigTokenTypes.IDENTIFIER).withText(PlatformPatterns.string().oneOf("trans", "transchoice")));
if (psiElementTrans != null && TwigUtil.getTwigMethodString(psiElementTrans) != null) {
targets.addAll(getTranslationDomainGoto(psiElement));
}
}
// {% trans from "app" %}
// {% transchoice from "app" %}
if (TwigPattern.getTranslationTokenTagFromPattern().accepts(psiElement)) {
targets.addAll(getTranslationDomainGoto(psiElement));
}
if (TwigPattern.getTranslationKeyPattern("trans", "transchoice").accepts(psiElement)) {
targets.addAll(getTranslationKeyGoTo(psiElement));
}
if (TwigPattern.getPrintBlockOrTagFunctionPattern("controller").accepts(psiElement) || TwigPattern.getStringAfterTagNamePattern("render").accepts(psiElement)) {
targets.addAll(getControllerGoTo(psiElement));
}
if (TwigPattern.getPrintBlockOrTagFunctionPattern("importmap").accepts(psiElement)) {
targets.addAll(getImportmapGoTo(psiElement));
}
if (TwigPattern.getTransDefaultDomainPattern().accepts(psiElement)) {
targets.addAll(TranslationUtil.getDomainPsiFiles(psiElement.getProject(), psiElement.getText()));
}
// {{ 'test'|<caret> }}
// {% apply <caret> %}foobar{% endapply %}
if (PlatformPatterns.or(TwigPattern.getFilterPattern(), TwigPattern.getApplyFilterPattern()).accepts(psiElement)) {
targets.addAll(getFilterGoTo(psiElement));
}
// {% if foo is ... %}
// {% if foo is not ... %}
if(PlatformPatterns.or(TwigPattern.getAfterIsTokenPattern(), TwigPattern.getAfterIsTokenWithOneIdentifierLeafPattern()).accepts(psiElement)) {
targets.addAll(getAfterIsToken(psiElement));
}
// {{ goto<caret>_me() }}
// {% if goto<caret>_me() %}
// {% set foo = foo<caret>_test() %}
// {{ macro.test() }}
if (TwigPattern.getPrintBlockFunctionPattern().accepts(psiElement)) {
targets.addAll(this.getMacros(psiElement));
targets.addAll(getFunctions(psiElement));
}
// {{ _self.input() }}
if (TwigPattern.getSelfMacroFunctionPattern().accepts(psiElement) || TwigPattern.getSelfMacroIdentifierPattern().accepts(psiElement)) {
targets.addAll(this.getSelfMacros(psiElement));
}
// {% from 'boo.html.twig' import goto_me %}
if (TwigPattern.getTemplateImportFileReferenceTagPattern().accepts(psiElement)) {
targets.addAll(this.getMacros(psiElement));
}
// {{ foo.fo<caret>o }}
if (TwigPattern.getTypeCompletionPattern().accepts(psiElement)
|| TwigPattern.getPrintBlockFunctionPattern().accepts(psiElement)
|| TwigPattern.getVariableTypePattern().accepts(psiElement))
{
targets.addAll(getTypeGoto(psiElement));
}
if (TwigPattern.getTwigDocBlockMatchPattern(ControllerDocVariableCollector.DOC_PATTERN).accepts(psiElement)) {
targets.addAll(getControllerNameGoto(psiElement));
}
// {{ parent() }}
if (TwigPattern.getParentFunctionPattern().accepts(psiElement)) {
targets.addAll(getParentGoto(psiElement));
}
// constant('Post::PUBLISHED')
if (TwigPattern.getPrintBlockOrTagFunctionPattern("constant").accepts(psiElement)) {
targets.addAll(getConstantGoto(psiElement));
}
// enum('App\\Config\\SomeOption')
// enum_cases('App\\Config\\SomeOption')
if (TwigPattern.getPrintBlockOrTagFunctionPattern("enum", "enum_cases").accepts(psiElement)) {
targets.addAll(getEnumGoto(psiElement));
}
// {# @var user \Foo #}
if (TwigPattern.getTwigTypeDocBlockPattern().accepts(psiElement)) {
targets.addAll(getVarClassGoto(psiElement));
}
// {% types { user: '\Foo' } %}
if (TwigPattern.getTypesTagTypeStringPattern().accepts(psiElement)) {
targets.addAll(getTypesTagClassGoto(psiElement));
}
// {# @see Foo.html.twig #}
// {# @see \Class #}
if (TwigPattern.getTwigDocSeePattern().accepts(psiElement)) {
targets.addAll(getSeeDocTagTargets(psiElement));
}
// {% FOO_TOKEN %}
if (TwigPattern.getTagTokenBlockPattern().accepts(psiElement)) {
targets.addAll(getTokenTargets(psiElement));
}
return targets.toArray(new PsiElement[0]);
}
/**
* {% if foo is ... %}
*/
@NotNull
private Collection<PsiElement> getAfterIsToken(@NotNull PsiElement psiElement) {
// find text after if statement
PsiElement actualElement = psiElement.getParent() instanceof TwigVariableReference ? psiElement.getParent() : psiElement;
String text = StringUtils.trim(
PhpElementsUtil.getPrevSiblingAsTextUntil(actualElement, TwigPattern.getAfterIsTokenTextPattern(), false) + actualElement.getText()
);
if(StringUtils.isBlank(text)) {
return Collections.emptyList();
}
Set<String> items = new HashSet<>(
Collections.singletonList(text)
);
// support atleat one identifier after current caret position
// "divisi<caret>ble by"
PsiElement whitespace = psiElement.getNextSibling();
if(whitespace instanceof PsiWhiteSpace) {
PsiElement nextSibling = whitespace.getNextSibling();
IElementType elementType = nextSibling == null ? null : nextSibling.getNode().getElementType();
if (elementType == TwigTokenTypes.IDENTIFIER || elementType == TwigElementTypes.VARIABLE_REFERENCE) {
String identifier = nextSibling.getText();
if (StringUtils.isNotBlank(identifier)) {
items.add(text + " " + identifier);
}
}
}
Collection<PsiElement> psiElements = new ArrayList<>();
for (Map.Entry<String, TwigExtension> entry : TwigExtensionParser.getSimpleTest(psiElement.getProject()).entrySet()) {
for (String item : items) {
if(entry.getKey().equalsIgnoreCase(item)) {
psiElements.addAll(Arrays.asList(
PhpElementsUtil.getPsiElementsBySignature(psiElement.getProject(), entry.getValue().getSignature()))
);
}
}
}
return psiElements;
}
@NotNull
private Collection<PsiElement> getRouteParameterGoTo(@NotNull PsiElement psiElement) {
String routeName = TwigUtil.getMatchingRouteNameOnParameter(psiElement);
if(routeName == null) {
return Collections.emptyList();
}
return Arrays.asList(
RouteHelper.getRouteParameterPsiElements(psiElement.getProject(), routeName, psiElement.getText())
);
}
@NotNull
private Collection<PsiElement> getControllerGoTo(@NotNull PsiElement psiElement) {
String text = PsiElementUtils.trimQuote(psiElement.getText());
return Arrays.asList(RouteHelper.getMethodsOnControllerShortcut(psiElement.getProject(), text));
}
private Collection<PsiFile> getImportmapGoTo(@NotNull PsiElement psiElement) {
String text = PsiElementUtils.trimQuote(psiElement.getText());
if (text.isBlank()) {
return Collections.emptyList();
}
return PsiElementUtils.convertVirtualFilesToPsiFiles(
psiElement.getProject(),
AssetMapperUtil.getEntrypointModuleReferences(psiElement.getProject(), text)
);
}
@NotNull
private Collection<PsiElement> getTwigFiles(@NotNull PsiElement psiElement, int offset) {
int calulatedOffset = offset - psiElement.getTextRange().getStartOffset();
if (calulatedOffset < 0) {
calulatedOffset = 0;
}
return TwigUtil.getTemplateNavigationOnOffset(
psiElement.getProject(),
psiElement.getText(),
calulatedOffset
);
}
@NotNull
public static Collection<PsiElement> getFilterGoTo(@NotNull PsiElement psiElement) {
Map<String, TwigExtension> filters = TwigExtensionParser.getFilters(psiElement.getProject());
if(!filters.containsKey(psiElement.getText())) {
return Collections.emptyList();
}
String signature = filters.get(psiElement.getText()).getSignature();
if(signature == null) {
return Collections.emptyList();
}
return Arrays.asList(PhpElementsUtil.getPsiElementsBySignature(psiElement.getProject(), signature));
}
@NotNull
private Collection<PsiElement> getRouteStartsWithGoTo(@NotNull PsiElement psiElement) {
String prefix = PsiElementUtils.getText(psiElement);
if (StringUtils.isBlank(prefix)) {
return Collections.emptyList();
}
Collection<PsiElement> targets = new ArrayList<>();
for (String routeName : RouteHelper.getAllRoutes(psiElement.getProject()).keySet()) {
if (routeName.startsWith(prefix)) {
targets.addAll(RouteHelper.getRouteDefinitionTargets(psiElement.getProject(), routeName));
}
}
return targets;
}
private Collection<PsiElement> getRouteGoTo(@NotNull PsiElement psiElement) {
String text = PsiElementUtils.getText(psiElement);
if (StringUtils.isBlank(text)) {
return Collections.emptyList();
}
PsiElement[] methods = RouteHelper.getMethods(psiElement.getProject(), RouteHelper.unescapeRouteName(text));
if (methods.length > 0) {
return Arrays.asList(methods);
}
return RouteHelper.getRouteDefinitionTargets(psiElement.getProject(), text);
}
private Collection<? extends PsiElement> getComponentGoTo(@NotNull PsiElement psiElement) {
String text = PsiElementUtils.getText(psiElement);
if (StringUtils.isBlank(text)) {
return Collections.emptyList();
}
Project project = psiElement.getProject();
return new ArrayList<>() {{
addAll(UxUtil.getComponentTemplates(project, text));
addAll(UxUtil.getTwigComponentPhpClasses(project, text));
}};
}
@NotNull
private Collection<PsiElement> getTranslationKeyGoTo(@NotNull PsiElement psiElement) {
String translationKey = psiElement.getText();
return Arrays.asList(
TranslationUtil.getTranslationPsiElements(psiElement.getProject(), translationKey, TwigUtil.getPsiElementTranslationDomain(psiElement))
);
}
@NotNull
private Collection<PsiElement> getTranslationDomainGoto(@NotNull PsiElement psiElement) {
String text = PsiElementUtils.trimQuote(psiElement.getText());
if(StringUtils.isBlank(text)) {
return Collections.emptyList();
}
return new ArrayList<>(TranslationUtil.getDomainPsiFiles(psiElement.getProject(), text));
}
@NotNull
private Collection<PsiElement> getConstantGoto(@NotNull PsiElement psiElement) {
return TwigConstantEnumResolver.getConstantTargets(psiElement);
}
@NotNull
private Collection<PsiElement> getEnumGoto(@NotNull PsiElement psiElement) {
return new ArrayList<>(TwigConstantEnumResolver.getEnumTargets(psiElement));
}
/**
* Extract class from inline variables
*
* {# @var \AppBundle\Entity\Foo variable #}
* {# @var variable \AppBundle\Entity\Foo #}
*/
@NotNull
private Collection<PhpClass> getVarClassGoto(@NotNull PsiElement psiElement) {
String comment = psiElement.getText();
if(StringUtils.isBlank(comment)) {
return Collections.emptyList();
}
for(String pattern: TwigTypeResolveUtil.DOC_TYPE_PATTERN_SINGLE) {
Matcher matcher = Pattern.compile(pattern).matcher(comment);
if (matcher.find()) {
String className = matcher.group("class");
if(StringUtils.isNotBlank(className)) {
return PhpElementsUtil.getClassesInterface(psiElement.getProject(), StringUtils.stripEnd(className, "[]"));
}
}
}
return Collections.emptyList();
}
/**
* Extract class from {% types %} tag
*
* {% types { user: '\AppBundle\Entity\Foo' } %}
* {% types { user?: '\AppBundle\Entity\Foo[]' } %}
*/
@NotNull
private Collection<PhpClass> getTypesTagClassGoto(@NotNull PsiElement psiElement) {
String typeString = psiElement.getText();
if (StringUtils.isBlank(typeString)) {
return Collections.emptyList();
}
// Remove quotes and unescape backslashes
String className = PsiElementUtils.trimQuote(typeString);
if (StringUtils.isBlank(className)) {
return Collections.emptyList();
}
// Unescape double backslashes: \\App\\User -> \App\User
className = className.replace("\\\\", "\\");
// Strip array notation: Foo[] -> Foo
className = StringUtils.stripEnd(className, "[]");
return PhpElementsUtil.getClassesInterface(psiElement.getProject(), className);
}
@NotNull
private Collection<PsiElement> getSeeDocTagTargets(@NotNull PsiElement psiElement) {
String comment = psiElement.getText();
if(StringUtils.isBlank(comment)) {
return Collections.emptyList();
}
Collection<PsiElement> psiElements = new ArrayList<>();
for(String pattern: new String[] {TwigPattern.DOC_SEE_REGEX, TwigUtil.DOC_SEE_REGEX_WITHOUT_SEE}) {
Matcher matcher = Pattern.compile(pattern).matcher(comment);
if (!matcher.find()) {
continue;
}
String content = matcher.group(1);
if(content.toLowerCase().endsWith(".twig")) {
psiElements.addAll(TwigUtil.getTemplatePsiElements(psiElement.getProject(), content));
}
psiElements.addAll(PhpElementsUtil.getClassesInterface(psiElement.getProject(), content));
ContainerUtil.addAll(psiElements, RouteHelper.getMethodsOnControllerShortcut(psiElement.getProject(), content));
PsiDirectory parent = psiElement.getContainingFile().getParent();
if(parent != null) {
VirtualFile relativeFile = VfsUtil.findRelativeFile(parent.getVirtualFile(), content.replace("\\", "/").split("/"));
if(relativeFile != null) {
ContainerUtil.addIfNotNull(psiElements, PsiManager.getInstance(psiElement.getProject()).findFile(relativeFile));
}
}
Matcher methodMatcher = Pattern.compile("([\\w\\\\-]+):+([\\w_\\-]+)").matcher(content);
if (methodMatcher.find()) {
for (PhpClass phpClass : PhpIndex.getInstance(psiElement.getProject()).getAnyByFQN(methodMatcher.group(1))) {
ContainerUtil.addIfNotNull(psiElements, phpClass.findMethodByName(methodMatcher.group(2)));
}
}
}
return psiElements;
}
@NotNull
public static Collection<PsiElement> getTypeGoto(@NotNull PsiElement psiElement) {
Collection<PsiElement> targetPsiElements = new HashSet<>();
// Twig extension is given more uselessly then useful targets, so drop this core contribution
//if (psiElement.getParent() instanceof TwigPsiReference) {
// PsiElement defaultResult = ((TwigPsiReference) psiElement.getParent()).resolve();
// if (defaultResult != null && defaultResult != psiElement.getParent()) return Collections.singleton(defaultResult);
//}
// class, class.method, class.method.method
// click on first item is our class name
Collection<String> beforeLeaf = TwigTypeResolveUtil.formatPsiTypeName(psiElement);
if(beforeLeaf.isEmpty()) {
Collection<TwigTypeContainer> twigTypeContainers = TwigTypeResolveUtil.resolveTwigMethodName(psiElement, TwigTypeResolveUtil.formatPsiTypeNameWithCurrent(psiElement));
for(TwigTypeContainer twigTypeContainer: twigTypeContainers) {
if(twigTypeContainer.getPhpNamedElement() != null) {
targetPsiElements.add(twigTypeContainer.getPhpNamedElement());
}
}
} else {
Collection<TwigTypeContainer> types = TwigTypeResolveUtil.resolveTwigMethodName(psiElement, beforeLeaf);
String text = psiElement.getText();
if(StringUtils.isNotBlank(text)) {
// provide method / field goto
for(TwigTypeContainer twigTypeContainer: types) {
if(twigTypeContainer.getPhpNamedElement() != null) {
targetPsiElements.addAll(TwigTypeResolveUtil.getTwigPhpNameTargets(twigTypeContainer.getPhpNamedElement(), text));
}
// form
// @TODO: provide extension
if (text.equals(twigTypeContainer.getStringElement())) {
FormFieldDataHolder formFieldDataHolder = twigTypeContainer.getFormFieldDataHolder();
if (formFieldDataHolder != null) {
// @TODO: resolve the to field itself
PhpClass phpClass = PhpElementsUtil.getClassInterface(psiElement.getProject(), formFieldDataHolder.ownerFormTypeFqn());
if (phpClass != null) {
targetPsiElements.add(phpClass);
}
}
}
}
}
}
return targetPsiElements;
}
@NotNull
public static Collection<PsiElement> getFunctions(@NotNull PsiElement psiElement) {
Map<String, TwigExtension> functions = TwigExtensionParser.getFunctions(psiElement.getProject());
String funcName = psiElement.getText();
if(!functions.containsKey(funcName)) {
return Collections.emptyList();
}
return Arrays.asList(PhpElementsUtil.getPsiElementsBySignature(psiElement.getProject(), functions.get(funcName).getSignature()));
}
@NotNull
private Collection<PsiElement> getMacros(@NotNull PsiElement psiElement) {
String funcName = psiElement.getText();
// check for complete file as namespace import {% import "file" as foo %}
// {% import _self as foobar %}
// {{ foobar.bar }}
PsiElement prevSibling = psiElement.getPrevSibling();
if(prevSibling != null && prevSibling.getNode().getElementType() == TwigTokenTypes.DOT) {
PsiElement identifier = prevSibling.getPrevSibling();
if(identifier == null || identifier.getNode().getElementType() != TwigElementTypes.VARIABLE_REFERENCE) {
return Collections.emptyList();
}
return TwigUtil.getImportedMacrosNamespaces(
psiElement.getContainingFile(),
identifier.getText() + "." + funcName
);
}
// {% from _self import foobar as input, foobar %}
return TwigUtil.getImportedMacros(psiElement.getContainingFile(), funcName);
}
private Collection<PsiElement> getSelfMacros(@NotNull PsiElement psiElement) {
Collection <PsiElement> psiElements = new ArrayList<>();
String funcName = psiElement.getText();
TwigUtil.visitMacros(psiElement.getContainingFile(), pair -> {
if (funcName.equals(pair.getFirst().name())) {
psiElements.add(pair.getSecond());
}
});
return psiElements;
}
@NotNull
private Collection<PsiElement> getControllerNameGoto(@NotNull PsiElement psiElement) {
Pattern pattern = Pattern.compile(ControllerDocVariableCollector.DOC_PATTERN);
Matcher matcher = pattern.matcher(psiElement.getText());
if (!matcher.find()) {
return Collections.emptyList();
}
String controllerName = matcher.group(1);
return Arrays.asList(RouteHelper.getMethodsOnControllerShortcut(psiElement.getProject(), controllerName));
}
@NotNull
private Collection<PsiElement> getParentGoto(@NotNull PsiElement psiElement) {
// find printblock
PsiElement functionCall = psiElement.getParent();
PsiElement printBlock = functionCall != null ? functionCall.getParent() : null;
if(printBlock == null || !PlatformPatterns.psiElement(TwigElementTypes.PRINT_BLOCK).accepts(printBlock)) {
return Collections.emptyList();
}
// printblock need to be child block statement
PsiElement blockStatement = printBlock.getParent();
if(blockStatement == null || !PlatformPatterns.psiElement(TwigElementTypes.BLOCK_STATEMENT).accepts(blockStatement)) {
return Collections.emptyList();
}
// BlockTag is first child of block statement
PsiElement blockTag = blockStatement.getFirstChild();
if(!(blockTag instanceof TwigBlockTag)) {
return Collections.emptyList();
}
String blockName = ((TwigBlockTag) blockTag).getName();
if(blockName == null) {
return Collections.emptyList();
}
return TwigBlockUtil.getBlockOverwriteTargets(psiElement.getContainingFile(), blockName, false);
}
@NotNull
private Collection<? extends PsiElement> getTokenTargets(@NotNull PsiElement psiElement) {
String tagName = psiElement.getText();
if(StringUtils.isBlank(tagName)) {
return Collections.emptyList();
}
Collection<PsiElement> targets = new ArrayList<>();
TwigUtil.visitTokenParsers(psiElement.getProject(), pair -> {
// support direct tag name or ending tag
// {% tag_name %}
// {% endtag_name %}
String currentTagName = pair.getFirst();
if(tagName.equalsIgnoreCase(currentTagName) || (tagName.toLowerCase().startsWith("end") && currentTagName.equalsIgnoreCase(tagName.substring(3)))) {
targets.add(pair.getSecond());
}
});
return targets;
}
@Nullable
@Override
public String getActionText(@NotNull DataContext dataContext) {
return null;
}
}