|
20 | 20 | * Annotator is mostly used to check semantic rules which can not be easily checked during parsing. |
21 | 21 | */ |
22 | 22 | public class LatteAnnotator implements Annotator { |
23 | | - @Override |
24 | | - public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { |
25 | | - if (element instanceof LatteMacroClassic) { |
26 | | - checkMacroClassic((LatteMacroClassic) element, holder); |
27 | | - |
28 | | - } else if (element instanceof LatteNetteAttr) { |
29 | | - checkNetteAttr((LatteNetteAttr) element, holder); |
30 | | - |
31 | | - } else if (element instanceof LeafPsiElement && element.getParent().getLastChild() instanceof PsiErrorElement) { |
32 | | - LeafPsiElement leaf = (LeafPsiElement) element; |
33 | | - if (leaf.getElementType() == LatteTypes.T_MACRO_OPEN_TAG_OPEN || leaf.getElementType() == LatteTypes.T_MACRO_CLOSE_TAG_OPEN) { |
34 | | - createErrorAnnotation(holder, "Malformed tag. Missing closing }"); |
35 | | - } |
36 | | - } |
37 | | - } |
38 | | - |
39 | | - private void checkNetteAttr(@NotNull LatteNetteAttr element, @NotNull AnnotationHolder holder) { |
40 | | - PsiElement attrName = element.getAttrName(); |
41 | | - String tagName = attrName.getText(); |
42 | | - boolean prefixed = false; |
43 | | - |
44 | | - if (tagName.startsWith("n:inner-")) { |
45 | | - prefixed = true; |
46 | | - tagName = tagName.substring(8); |
47 | | - } else if (tagName.startsWith("n:tag-")) { |
48 | | - prefixed = true; |
49 | | - tagName = tagName.substring(6); |
50 | | - } else { |
51 | | - tagName = tagName.substring(2); |
52 | | - } |
53 | | - |
54 | | - Project project = element.getProject(); |
55 | | - LatteTagSettings macro = LatteConfiguration.getInstance(project).getTag(tagName); |
56 | | - if (macro == null || macro.getType() == LatteTagSettings.Type.UNPAIRED) { |
57 | | - AnnotationBuilder builder = holder.newAnnotation(HighlightSeverity.ERROR, "Unknown attribute tag " + attrName.getText()) |
58 | | - .range(attrName) |
59 | | - .withFix(new AddCustomPairMacro(tagName)); |
60 | | - if (!prefixed) { |
61 | | - builder = builder.withFix(new AddCustomAttrOnlyMacro(tagName)); |
62 | | - } |
63 | | - builder.create(); |
64 | | - |
65 | | - } else if (prefixed && macro.getType() != LatteTagSettings.Type.PAIR && macro.getType() != LatteTagSettings.Type.AUTO_EMPTY) { |
66 | | - createErrorAnnotation(holder, attrName, "Attribute tag n:" + tagName + " can not be used with prefix."); |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - private void checkMacroClassic(@NotNull LatteMacroClassic element, @NotNull AnnotationHolder holder) { |
71 | | - LatteMacroTag openTag = element.getOpenTag(); |
72 | | - LatteMacroTag closeTag = element.getCloseTag(); |
73 | | - |
74 | | - String openTagName = openTag.getMacroName(); |
75 | | - LatteTagSettings macro = LatteConfiguration.getInstance(element.getProject()).getTag(openTagName); |
76 | | - if (macro == null || macro.getType() == LatteTagSettings.Type.ATTR_ONLY) { |
77 | | - boolean isOk = false; |
78 | | - LatteMacroContent content = openTag.getMacroContent(); |
79 | | - if (content != null) { |
80 | | - LattePhpContent phpContent = content.getFirstPhpContent(); |
81 | | - if (phpContent != null && phpContent.getFirstChild() instanceof LattePhpVariable) { |
82 | | - isOk = true; |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - if (!isOk) { |
87 | | - if (macro != null) { |
88 | | - createErrorAnnotation(holder, openTag, "Can not use n:" + openTagName + " attribute as normal tag"); |
89 | | - if (closeTag != null) { |
90 | | - createErrorAnnotation(holder, closeTag, "Tag n:" + openTagName + " can not be used as pair tag"); |
91 | | - } |
92 | | - |
93 | | - } else { |
94 | | - AnnotationBuilder annotation = holder.newAnnotation(HighlightSeverity.ERROR, "Unknown tag {" + openTagName + "}").range(openTag); |
95 | | - annotation = annotation.withFix(new AddCustomPairMacro(openTagName)); |
96 | | - annotation = annotation.withFix(new AddCustomUnpairedMacro(openTagName)); |
97 | | - annotation.create(); |
98 | | - } |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - String closeTagName = closeTag != null ? closeTag.getMacroName() : null; |
103 | | - if (closeTagName != null && !closeTagName.isEmpty() && !closeTagName.equals(openTagName)) { |
104 | | - createErrorAnnotation(holder, closeTag, "Unexpected {/" + closeTagName + "}, expected {/" + openTagName + "}"); |
105 | | - } |
106 | | - |
107 | | - if ( |
108 | | - macro != null |
109 | | - && closeTag == null |
110 | | - && ((element instanceof LattePairMacro && macro.getType() == LatteTagSettings.Type.AUTO_EMPTY) || macro.getType() == LatteTagSettings.Type.PAIR) |
111 | | - ) { |
112 | | - final int[] unclosed = {0}; |
113 | | - openTag.getParent().acceptChildren(new PsiRecursiveElementWalkingVisitor() { |
114 | | - @Override |
115 | | - public void visitElement(@NotNull PsiElement element) { |
116 | | - if (element instanceof LattePairMacro) { |
117 | | - LatteMacroTag tag = ((LattePairMacro) element).getOpenTag(); |
118 | | - if (tag.getMacroName().equals("block") && ((LattePairMacro) element).getCloseTag() == null) { |
119 | | - unclosed[0]++; |
120 | | - } else { |
121 | | - super.visitElement(element); |
122 | | - } |
123 | | - |
124 | | - } else { |
125 | | - super.visitElement(element); |
126 | | - } |
127 | | - } |
128 | | - }); |
129 | | - //PsiElement el = PsiTreeUtil.getChildOfAnyType(openTag.getParent(), LattePairMacro.class); |
130 | | - if (!macro.isTagBlock() || unclosed[0] > 0) { |
131 | | - createErrorAnnotation(holder, openTag, "Unclosed tag " + openTagName); |
132 | | - } |
133 | | - } |
134 | | - } |
135 | | - |
136 | | - private void createErrorAnnotation(final @NotNull AnnotationHolder holder, final @NotNull String message) { |
137 | | - holder.newAnnotation(HighlightSeverity.ERROR, message).create(); |
138 | | - } |
139 | | - |
140 | | - private void createErrorAnnotation( |
141 | | - final @NotNull AnnotationHolder holder, |
142 | | - final @NotNull PsiElement element, |
143 | | - final @NotNull String message |
144 | | - ) { |
145 | | - holder.newAnnotation(HighlightSeverity.ERROR, message).range(element).create(); |
146 | | - } |
| 23 | + @Override |
| 24 | + public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { |
| 25 | + if (element instanceof LatteMacroClassic) { |
| 26 | + checkMacroClassic((LatteMacroClassic) element, holder); |
| 27 | + |
| 28 | + } else if (element instanceof LatteNetteAttr) { |
| 29 | + checkNetteAttr((LatteNetteAttr) element, holder); |
| 30 | + |
| 31 | + } else if (element instanceof LeafPsiElement && element.getParent().getLastChild() instanceof PsiErrorElement) { |
| 32 | + LeafPsiElement leaf = (LeafPsiElement) element; |
| 33 | + if (leaf.getElementType() == LatteTypes.T_MACRO_OPEN_TAG_OPEN || leaf.getElementType() == LatteTypes.T_MACRO_CLOSE_TAG_OPEN) { |
| 34 | + createErrorAnnotation(holder, "Malformed tag. Missing closing }"); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private void checkNetteAttr(@NotNull LatteNetteAttr element, @NotNull AnnotationHolder holder) { |
| 40 | + PsiElement attrName = element.getAttrName(); |
| 41 | + String tagName = attrName.getText(); |
| 42 | + boolean prefixed = false; |
| 43 | + |
| 44 | + if (tagName.startsWith("n:inner-")) { |
| 45 | + prefixed = true; |
| 46 | + tagName = tagName.substring(8); |
| 47 | + } else if (tagName.startsWith("n:tag-")) { |
| 48 | + prefixed = true; |
| 49 | + tagName = tagName.substring(6); |
| 50 | + } else { |
| 51 | + tagName = tagName.substring(2); |
| 52 | + } |
| 53 | + |
| 54 | + Project project = element.getProject(); |
| 55 | + LatteTagSettings macro = LatteConfiguration.getInstance(project).getTag(tagName); |
| 56 | + if (macro == null || macro.getType() == LatteTagSettings.Type.UNPAIRED) { |
| 57 | + AnnotationBuilder builder = holder.newAnnotation(HighlightSeverity.ERROR, "Unknown attribute tag " + attrName.getText()) |
| 58 | + .range(attrName) |
| 59 | + .withFix(new AddCustomPairMacro(tagName)); |
| 60 | + if (!prefixed) { |
| 61 | + builder = builder.withFix(new AddCustomAttrOnlyMacro(tagName)); |
| 62 | + } |
| 63 | + builder.create(); |
| 64 | + |
| 65 | + } else if (prefixed && macro.getType() != LatteTagSettings.Type.PAIR && macro.getType() != LatteTagSettings.Type.AUTO_EMPTY) { |
| 66 | + createErrorAnnotation(holder, attrName, "Attribute tag n:" + tagName + " can not be used with prefix."); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private void checkMacroClassic(@NotNull LatteMacroClassic element, @NotNull AnnotationHolder holder) { |
| 71 | + LatteMacroTag openTag = element.getOpenTag(); |
| 72 | + LatteMacroTag closeTag = element.getCloseTag(); |
| 73 | + |
| 74 | + String openTagName = openTag.getMacroName(); |
| 75 | + LatteTagSettings macro = LatteConfiguration.getInstance(element.getProject()).getTag(openTagName); |
| 76 | + if (macro == null || macro.getType() == LatteTagSettings.Type.ATTR_ONLY) { |
| 77 | + boolean isOk = false; |
| 78 | + LatteMacroContent content = openTag.getMacroContent(); |
| 79 | + if (content != null) { |
| 80 | + LattePhpContent phpContent = content.getFirstPhpContent(); |
| 81 | + if (phpContent != null && phpContent.getFirstChild() instanceof LattePhpVariable) { |
| 82 | + isOk = true; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (!isOk) { |
| 87 | + if (macro != null) { |
| 88 | + createErrorAnnotation(holder, openTag, "Can not use n:" + openTagName + " attribute as normal tag"); |
| 89 | + if (closeTag != null) { |
| 90 | + createErrorAnnotation(holder, closeTag, "Tag n:" + openTagName + " can not be used as pair tag"); |
| 91 | + } |
| 92 | + |
| 93 | + } else { |
| 94 | + AnnotationBuilder annotation = holder.newAnnotation(HighlightSeverity.ERROR, "Unknown tag {" + openTagName + "}").range(openTag); |
| 95 | + annotation = annotation.withFix(new AddCustomPairMacro(openTagName)); |
| 96 | + annotation = annotation.withFix(new AddCustomUnpairedMacro(openTagName)); |
| 97 | + annotation.create(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + String closeTagName = closeTag != null ? closeTag.getMacroName() : null; |
| 103 | + if (closeTagName != null && !closeTagName.isEmpty() && !closeTagName.equals(openTagName)) { |
| 104 | + createErrorAnnotation(holder, closeTag, "Unexpected {/" + closeTagName + "}, expected {/" + openTagName + "}"); |
| 105 | + } |
| 106 | + |
| 107 | + if ( |
| 108 | + macro != null |
| 109 | + && closeTag == null |
| 110 | + && ((element instanceof LattePairMacro && macro.getType() == LatteTagSettings.Type.AUTO_EMPTY) || macro.getType() == LatteTagSettings.Type.PAIR) |
| 111 | + ) { |
| 112 | + final int[] unclosed = {0}; |
| 113 | + openTag.getParent().acceptChildren(new PsiRecursiveElementWalkingVisitor() { |
| 114 | + @Override |
| 115 | + public void visitElement(@NotNull PsiElement element) { |
| 116 | + if (element instanceof LattePairMacro) { |
| 117 | + LatteMacroTag tag = ((LattePairMacro) element).getOpenTag(); |
| 118 | + if (tag.getMacroName().equals("block") && ((LattePairMacro) element).getCloseTag() == null) { |
| 119 | + unclosed[0]++; |
| 120 | + } else { |
| 121 | + super.visitElement(element); |
| 122 | + } |
| 123 | + |
| 124 | + } else { |
| 125 | + super.visitElement(element); |
| 126 | + } |
| 127 | + } |
| 128 | + }); |
| 129 | + //PsiElement el = PsiTreeUtil.getChildOfAnyType(openTag.getParent(), LattePairMacro.class); |
| 130 | + if (!macro.isTagBlock() || unclosed[0] > 0) { |
| 131 | + createErrorAnnotation(holder, openTag, "Unclosed tag " + openTagName); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private void createErrorAnnotation(final @NotNull AnnotationHolder holder, final @NotNull String message) { |
| 137 | + holder.newAnnotation(HighlightSeverity.ERROR, message).create(); |
| 138 | + } |
| 139 | + |
| 140 | + private void createErrorAnnotation( |
| 141 | + final @NotNull AnnotationHolder holder, |
| 142 | + final @NotNull PsiElement element, |
| 143 | + final @NotNull String message |
| 144 | + ) { |
| 145 | + holder.newAnnotation(HighlightSeverity.ERROR, message).range(element).create(); |
| 146 | + } |
147 | 147 | } |
0 commit comments