-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathTemplateMissingAnnotationPhpAttributeLocalInspection.java
More file actions
146 lines (121 loc) · 6.11 KB
/
TemplateMissingAnnotationPhpAttributeLocalInspection.java
File metadata and controls
146 lines (121 loc) · 6.11 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
package fr.adrienbrault.idea.symfony2plugin.templating.inspection;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment;
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpAttribute;
import com.jetbrains.php.lang.psi.elements.PhpAttributesList;
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
import de.espend.idea.php.annotation.dict.PhpDocTagAnnotation;
import de.espend.idea.php.annotation.util.AnnotationUtil;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PhpPsiAttributesUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class TemplateMissingAnnotationPhpAttributeLocalInspection extends LocalInspectionTool {
@NotNull
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}
return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (element instanceof PhpDocTag) {
annotate((PhpDocTag) element, holder);
}
if (element instanceof PhpAttribute) {
String fqn = ((PhpAttribute) element).getFQN();
if (fqn != null && PhpElementsUtil.isEqualClassName(fqn, TwigUtil.TEMPLATE_ANNOTATION_CLASS)) {
annotate((PhpAttribute) element, holder);
}
}
super.visitElement(element);
}
};
}
private void annotate(@NotNull PhpAttribute phpAttribute, @NotNull ProblemsHolder holder) {
LinkedHashSet<String> templateNames = new LinkedHashSet<>();
boolean isEmptyTemplateAndGuess = phpAttribute.getArguments().isEmpty();
if (isEmptyTemplateAndGuess) {
PsiElement phpAttributesList = phpAttribute.getParent();
if (phpAttributesList instanceof PhpAttributesList) {
PsiElement method = phpAttributesList.getParent();
if (method instanceof Method) {
templateNames.addAll(Arrays.asList(TwigUtil.getControllerMethodShortcut((Method) method)));
}
}
} else {
String attributeDefaultValue = PhpPsiAttributesUtil.getAttributeValueByNameAsStringWithDefaultParameterFallback(phpAttribute, "template");
if (attributeDefaultValue != null) {
templateNames.add(attributeDefaultValue);
}
}
if(!templateNames.isEmpty()) {
attachProblemForMissingTemplatesWithSuggestions(phpAttribute, holder, templateNames, isEmptyTemplateAndGuess);
}
}
private void annotate(@NotNull PhpDocTag phpDocTag, @NotNull ProblemsHolder holder) {
PhpDocTagAnnotation phpDocAnnotationContainer = AnnotationUtil.getPhpDocAnnotationContainer(phpDocTag);
if (phpDocAnnotationContainer == null || !PhpElementsUtil.isEqualClassName(phpDocAnnotationContainer.getPhpClass(), TwigUtil.TEMPLATE_ANNOTATION_CLASS)) {
return;
}
PhpPsiElement phpDocAttrList = phpDocTag.getFirstPsiChild();
if(phpDocAttrList == null) {
return;
}
LinkedHashSet<String> templateNames = new LinkedHashSet<>();
boolean isEmptyTemplateAndGuess = false;
@Nullable String matcher = AnnotationUtil.getPropertyValueOrDefault(phpDocTag, "template");
if (matcher != null) {
templateNames.add(matcher);
} else {
isEmptyTemplateAndGuess = true;
// find template name on last method
PhpDocComment docComment = PsiTreeUtil.getParentOfType(phpDocTag, PhpDocComment.class);
if(null == docComment) {
return;
}
Method method = PsiTreeUtil.getNextSiblingOfType(docComment, Method.class);
if(null == method) {
return;
}
templateNames.addAll(Arrays.asList(TwigUtil.getControllerMethodShortcut(method)));
}
if(!templateNames.isEmpty()) {
attachProblemForMissingTemplatesWithSuggestions(phpDocTag, holder, templateNames, isEmptyTemplateAndGuess);
}
}
private void attachProblemForMissingTemplatesWithSuggestions(@NotNull PsiElement target, @NotNull ProblemsHolder holder, @NotNull LinkedHashSet<String> templateNames, boolean isEmptyTemplateAndGuess) {
if(templateNames.isEmpty()) {
return;
}
for (String templateName : templateNames) {
if (!TwigUtil.getTemplateFiles(holder.getProject(), templateName).isEmpty()) {
return;
}
}
// find html target, as this this our first priority for end users condition
// or fallback on first item
String[] templates = templateNames.stream()
.filter(s -> s.toLowerCase().endsWith(".html.twig")).toArray(String[]::new);
Collection<LocalQuickFix> quickFixes = new ArrayList<>();
quickFixes.add(new TemplateCreateByNameLocalQuickFix(templates));
if (!isEmptyTemplateAndGuess && templates.length > 0) {
// use first as underscore is higher priority and common way by framework bundle
quickFixes.add(new TemplateGuessTypoQuickFix(templates[0]));
}
holder.registerProblem(target, "Twig: Missing Template", quickFixes.toArray(new LocalQuickFix[0]));
}
}