-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathTwigAssetMissingInspection.java
More file actions
69 lines (56 loc) · 2.49 KB
/
TwigAssetMissingInspection.java
File metadata and controls
69 lines (56 loc) · 2.49 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
package fr.adrienbrault.idea.symfony2plugin.templating.inspection;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.patterns.ElementPattern;
import com.intellij.codeInspection.ProblemsHolder;
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 org.jspecify.annotations.NonNull;
/**
* asset('<caret>')
*
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class TwigAssetMissingInspection 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 {
private final ProblemsHolder holder;
private ElementPattern<PsiElement> assetPattern;
MyPsiElementVisitor(ProblemsHolder holder) {
this.holder = holder;
}
@Override
public void visitElement(@NonNull PsiElement element) {
if (!(element instanceof LeafPsiElement) || element.getNode().getElementType() != TwigTokenTypes.STRING_TEXT) {
super.visitElement(element);
return;
}
if(getAssetPattern().accepts(element) && TwigUtil.isValidStringWithoutInterpolatedOrConcat(element)) {
invoke(element, holder);
}
super.visitElement(element);
}
private void invoke(@NotNull PsiElement element, @NotNull ProblemsHolder holder) {
String asset = element.getText();
if(StringUtils.isBlank(asset) || !TwigUtil.resolveAssetsFiles(element.getProject(), asset).isEmpty()) {
return;
}
holder.registerProblem(element, "Missing asset");
}
private ElementPattern<PsiElement> getAssetPattern() {
return assetPattern != null ? assetPattern : (assetPattern = TwigPattern.getAutocompletableAssetPattern());
}
}
}