-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathTwigTemplateMissingInspection.java
More file actions
97 lines (81 loc) · 3.65 KB
/
TwigTemplateMissingInspection.java
File metadata and controls
97 lines (81 loc) · 3.65 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
package fr.adrienbrault.idea.symfony2plugin.templating.inspection;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.patterns.ElementPattern;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.jetbrains.twig.TwigTokenTypes;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
/**
* {% include 'f<caret>.html.twig' %}
* {{ include('f<caret>.html.twig') }}
* {% embed 'f<caret>.html.twig' %}
* ... and so on
*
*
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class TwigTemplateMissingInspection extends LocalInspectionTool {
@NotNull
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if(!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}
return new MyPsiElementVisitor(holder);
}
private static class MyPsiElementVisitor extends PsiElementVisitor {
@NotNull
private final ProblemsHolder holder;
private ElementPattern<?> templateFileReferencePattern;
private ElementPattern<?> includeFunctionPattern;
MyPsiElementVisitor(@NotNull ProblemsHolder holder) {
this.holder = holder;
}
@Override
public void visitElement(PsiElement element) {
if (!(element instanceof LeafPsiElement) || element.getNode().getElementType() != TwigTokenTypes.STRING_TEXT) {
super.visitElement(element);
return;
}
if((getTemplateFileReferencePattern().accepts(element) || getIncludeFunctionPattern().accepts(element)) && TwigUtil.isValidStringWithoutInterpolatedOrConcat(element)) {
invoke(element, holder);
}
super.visitElement(element);
}
private ElementPattern<?> getTemplateFileReferencePattern() {
return templateFileReferencePattern != null ? templateFileReferencePattern : (templateFileReferencePattern = TwigPattern.getTemplateFileReferenceTagPattern());
}
private ElementPattern<?> getIncludeFunctionPattern() {
return includeFunctionPattern != null ? includeFunctionPattern : (includeFunctionPattern = TwigPattern.getPrintBlockOrTagFunctionPattern("include", "source"));
}
}
private static void invoke(@NotNull final PsiElement element, @NotNull ProblemsHolder holder) {
String templateName = element.getText();
if (StringUtils.isBlank(templateName)) {
return;
}
Collection<VirtualFile> psiElements = TwigUtil.getTemplateFiles(element.getProject(), templateName);
if(!psiElements.isEmpty()) {
return;
}
LocalQuickFix[] templateCreateByNameLocalQuickFix = new LocalQuickFix[]{
new TemplateCreateByNameLocalQuickFix(templateName),
new TemplateGuessTypoQuickFix(templateName)
};
holder.registerProblem(
element,
"Twig: Missing Template",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
templateCreateByNameLocalQuickFix
);
}
}