|
1 | 1 | package fr.adrienbrault.idea.symfony2plugin.navigation.controller; |
2 | 2 |
|
3 | | -import com.intellij.openapi.util.Pair; |
4 | | -import com.intellij.psi.PsiElement; |
5 | 3 | import com.intellij.psi.PsiFile; |
6 | | -import com.intellij.psi.util.PsiTreeUtil; |
7 | | -import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment; |
8 | | -import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag; |
9 | | -import com.jetbrains.php.lang.psi.elements.Method; |
10 | | -import com.jetbrains.php.lang.psi.elements.PhpClass; |
11 | 4 | import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons; |
12 | | -import fr.adrienbrault.idea.symfony2plugin.config.SymfonyPhpReferenceContributor; |
13 | 5 | import fr.adrienbrault.idea.symfony2plugin.dic.RelatedPopupGotoLineMarker; |
14 | 6 | import fr.adrienbrault.idea.symfony2plugin.extension.ControllerActionGotoRelatedCollector; |
15 | 7 | import fr.adrienbrault.idea.symfony2plugin.extension.ControllerActionGotoRelatedCollectorParameter; |
| 8 | +import fr.adrienbrault.idea.symfony2plugin.templating.util.PhpMethodVariableResolveUtil; |
16 | 9 | import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil; |
17 | | -import fr.adrienbrault.idea.symfony2plugin.util.AnnotationBackportUtil; |
18 | | -import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher; |
19 | | -import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
20 | 10 | import icons.TwigIcons; |
21 | | -import org.jetbrains.annotations.NotNull; |
22 | 11 |
|
23 | | -import java.util.*; |
24 | | -import java.util.function.Consumer; |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.Set; |
25 | 14 |
|
26 | 15 | /** |
27 | 16 | * @author Daniel Espendiller <daniel@espendiller.net> |
28 | 17 | */ |
29 | 18 | public class TemplatesControllerRelatedGotoCollector implements ControllerActionGotoRelatedCollector { |
30 | | - |
31 | 19 | @Override |
32 | 20 | public void collectGotoRelatedItems(ControllerActionGotoRelatedCollectorParameter parameter) { |
33 | 21 | Set<String> uniqueTemplates = new HashSet<>(); |
34 | 22 |
|
35 | | - Method method = parameter.getMethod(); |
36 | | - |
37 | | - visitMethodTemplateNames(method, pair -> { |
38 | | - String templateName = pair.getFirst(); |
39 | | - |
40 | | - if(!uniqueTemplates.contains(templateName)) { |
41 | | - uniqueTemplates.add(templateName); |
42 | | - |
43 | | - for (PsiElement psiElement : pair.getSecond()) { |
44 | | - parameter.add(new RelatedPopupGotoLineMarker.PopupGotoRelatedItem( |
45 | | - psiElement, |
46 | | - TwigUtil.getFoldingTemplateNameOrCurrent(templateName)).withIcon(TwigIcons.TwigFileIcon, Symfony2Icons.TWIG_LINE_MARKER) |
47 | | - ); |
48 | | - } |
49 | | - } |
| 23 | + PhpMethodVariableResolveUtil.visitRenderTemplateFunctions(parameter.getMethod(), triple -> { |
| 24 | + uniqueTemplates.add(triple.getFirst()); |
50 | 25 | }); |
51 | 26 |
|
52 | | - for(PsiElement psiElement: parameter.getParameterLists()) { |
53 | | - MethodMatcher.MethodMatchParameter matchedSignature = MethodMatcher.getMatchedSignatureWithDepth(psiElement, SymfonyPhpReferenceContributor.TEMPLATE_SIGNATURES); |
54 | | - if (matchedSignature != null) { |
55 | | - String resolveString = PhpElementsUtil.getStringValue(psiElement); |
56 | | - if(resolveString != null && !uniqueTemplates.contains(resolveString)) { |
57 | | - uniqueTemplates.add(resolveString); |
58 | | - for(PsiFile templateTarget: TwigUtil.getTemplatePsiElements(parameter.getProject(), resolveString)) { |
59 | | - parameter.add(new RelatedPopupGotoLineMarker.PopupGotoRelatedItem(templateTarget, resolveString).withIcon(TwigIcons.TwigFileIcon, Symfony2Icons.TWIG_LINE_MARKER)); |
60 | | - } |
61 | | - } |
| 27 | + for (String uniqueTemplate : uniqueTemplates) { |
| 28 | + for(PsiFile templateTarget: TwigUtil.getTemplatePsiElements(parameter.getProject(), uniqueTemplate)) { |
| 29 | + parameter.add(new RelatedPopupGotoLineMarker.PopupGotoRelatedItem(templateTarget, uniqueTemplate).withIcon(TwigIcons.TwigFileIcon, Symfony2Icons.TWIG_LINE_MARKER)); |
62 | 30 | } |
63 | 31 | } |
64 | 32 | } |
65 | | - |
66 | | - /** |
67 | | - * Visit every possible template on given method: eg Annotations @Template() |
68 | | - */ |
69 | | - public static void visitMethodTemplateNames(@NotNull Method method, @NotNull Consumer<Pair<String, Collection<PsiElement>>> consumer) { |
70 | | - // on @Template annotation |
71 | | - PhpDocComment phpDocComment = method.getDocComment(); |
72 | | - if(phpDocComment != null) { |
73 | | - Collection<PhpDocTag> phpDocTags = AnnotationBackportUtil.filterValidDocTags(PsiTreeUtil.findChildrenOfType(phpDocComment, PhpDocTag.class)); |
74 | | - if(phpDocTags.size() > 0) { |
75 | | - // cache use map for this phpDocComment |
76 | | - Map<String, String> importMap = AnnotationBackportUtil.getUseImportMap(phpDocComment); |
77 | | - if(importMap.size() > 0) { |
78 | | - for(PhpDocTag phpDocTag: phpDocTags) { |
79 | | - // resolve annotation and check for template |
80 | | - PhpClass phpClass = AnnotationBackportUtil.getAnnotationReference(phpDocTag, importMap); |
81 | | - if(phpClass != null && PhpElementsUtil.isEqualClassName(phpClass, TwigUtil.TEMPLATE_ANNOTATION_CLASS)) { |
82 | | - Pair<String, Collection<PsiElement>> templateAnnotationFiles = TwigUtil.getTemplateAnnotationFiles(phpDocTag); |
83 | | - if(templateAnnotationFiles != null) { |
84 | | - consumer.accept(Pair.create(templateAnnotationFiles.getFirst(), templateAnnotationFiles.getSecond())); |
85 | | - } |
86 | | - } |
87 | | - } |
88 | | - } |
89 | | - } |
90 | | - } |
91 | | - |
92 | | - // on method name |
93 | | - for (String templateName : TwigUtil.getControllerMethodShortcut(method)) { |
94 | | - consumer.accept(Pair.create( |
95 | | - templateName, |
96 | | - new HashSet<>(TwigUtil.getTemplatePsiElements(method.getProject(), templateName)) |
97 | | - )); |
98 | | - } |
99 | | - } |
100 | 33 | } |
0 commit comments