|
| 1 | +package io.protostuff.jetbrains.plugin.annotator; |
| 2 | + |
| 3 | +import com.google.common.annotations.VisibleForTesting; |
| 4 | +import com.intellij.codeInspection.ProblemHighlightType; |
| 5 | +import com.intellij.lang.ASTNode; |
| 6 | +import com.intellij.lang.annotation.Annotation; |
| 7 | +import com.intellij.lang.annotation.AnnotationHolder; |
| 8 | +import com.intellij.lang.annotation.Annotator; |
| 9 | +import com.intellij.psi.PsiElement; |
| 10 | +import com.intellij.psi.PsiErrorElement; |
| 11 | +import com.intellij.psi.PsiRecursiveElementVisitor; |
| 12 | +import io.protostuff.compiler.model.Field; |
| 13 | +import io.protostuff.jetbrains.plugin.psi.*; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | +import org.jetbrains.annotations.Nullable; |
| 16 | + |
| 17 | +import java.util.*; |
| 18 | + |
| 19 | +import static io.protostuff.jetbrains.plugin.ProtostuffBundle.message; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Kostiantyn Shchepanovskyi |
| 23 | + */ |
| 24 | +public class ProtoErrorsAnnotator implements Annotator { |
| 25 | + |
| 26 | + private final int MIN_TAG = 1; |
| 27 | + private final int MAX_TAG = Field.MAX_TAG_VALUE; |
| 28 | + private final int SYS_RESERVED_START = 19000; |
| 29 | + private final int SYS_RESERVED_END = 19999; |
| 30 | + |
| 31 | + private AnnotationHolder holder; |
| 32 | + |
| 33 | + @Override |
| 34 | + public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) { |
| 35 | + boolean check = hasErrors(element); |
| 36 | + if (!check) { |
| 37 | + this.holder = holder; |
| 38 | + if (element instanceof MessageNode) { |
| 39 | + MessageNode message = (MessageNode) element; |
| 40 | + Collection<MessageField> fields = message.getFields(); |
| 41 | + checkInvalidFieldTags(fields); |
| 42 | + checkDuplicateFieldTags(fields); |
| 43 | + checkDuplicateFieldNames(fields); |
| 44 | + checkReservedFieldTags(message, fields); |
| 45 | + checkReservedFieldNames(message, fields); |
| 46 | + } else if (element instanceof EnumNode) { |
| 47 | + EnumNode anEnum = (EnumNode) element; |
| 48 | + List<EnumConstantNode> constants = anEnum.getConstants(); |
| 49 | + checkDuplicateEnumConstantNames(constants); |
| 50 | + checkDuplicateEnumConstantValues(anEnum, constants); |
| 51 | + } else if (element instanceof ServiceNode) { |
| 52 | + ServiceNode service = (ServiceNode) element; |
| 53 | + List<RpcMethodNode> rpcMethods = service.getRpcMethods(); |
| 54 | + checkDuplicateServiceMethodNames(rpcMethods); |
| 55 | + } |
| 56 | + this.holder = null; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void checkDuplicateServiceMethodNames(List<RpcMethodNode> rpcMethods) { |
| 61 | + Map<String, RpcMethodNode> methodByName = new HashMap<>(); |
| 62 | + for (RpcMethodNode methods : rpcMethods) { |
| 63 | + String name = methods.getMethodName(); |
| 64 | + if (methodByName.containsKey(name)) { |
| 65 | + String message = message("error.duplicate.method.name", name); |
| 66 | + markError(methods.getNode(), methodByName.get(name).getMethodNameNode(), message); |
| 67 | + markError(methods.getNode(), methods.getMethodNameNode(), message); |
| 68 | + } |
| 69 | + methodByName.put(name, methods); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void checkDuplicateEnumConstantValues(EnumNode anEnum, List<EnumConstantNode> constants) { |
| 74 | + if (anEnum.allowAlias()) { |
| 75 | + return; |
| 76 | + } |
| 77 | + Map<Integer, EnumConstantNode> fieldByTag = new HashMap<>(); |
| 78 | + for (EnumConstantNode constant : constants) { |
| 79 | + int tag = constant.getConstantValue(); |
| 80 | + if (fieldByTag.containsKey(tag)) { |
| 81 | + String message = message("error.duplicate.constant.value", tag); |
| 82 | + markError(constant.getNode(), fieldByTag.get(tag).getConstantValueNode(), message); |
| 83 | + markError(constant.getNode(), constant.getConstantValueNode(), message); |
| 84 | + } |
| 85 | + fieldByTag.put(tag, constant); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private void checkDuplicateEnumConstantNames(List<EnumConstantNode> constants) { |
| 90 | + Map<String, EnumConstantNode> fieldByName = new HashMap<>(); |
| 91 | + for (EnumConstantNode constant : constants) { |
| 92 | + String name = constant.getConstantName(); |
| 93 | + if (fieldByName.containsKey(name)) { |
| 94 | + String message = message("error.duplicate.constant.name", name); |
| 95 | + markError(constant.getNode(), fieldByName.get(name).getConstantNameNode(), message); |
| 96 | + markError(constant.getNode(), constant.getConstantNameNode(), message); |
| 97 | + } |
| 98 | + fieldByName.put(name, constant); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private boolean hasErrors(PsiElement element) { |
| 103 | + if (element instanceof AntlrParserRuleNode) { |
| 104 | + AntlrParserRuleNode node = (AntlrParserRuleNode) element; |
| 105 | + return node.hasSyntaxErrors(); |
| 106 | + } else { |
| 107 | + final boolean[] hasErrors = {false}; |
| 108 | + new PsiRecursiveElementVisitor() { |
| 109 | + @Override |
| 110 | + public void visitErrorElement(PsiErrorElement element) { |
| 111 | + hasErrors[0] = true; |
| 112 | + } |
| 113 | + }; |
| 114 | + return hasErrors[0]; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private void checkReservedFieldTags(MessageNode message, Collection<MessageField> fields) { |
| 119 | + List<RangeNode> ranges = message.getReservedFieldRanges(); |
| 120 | + for (MessageField field : fields) { |
| 121 | + int tag = field.getTag(); |
| 122 | + for (RangeNode range : ranges) { |
| 123 | + if (range.contains(tag)) { |
| 124 | + markError(field.getNode(), field.getTagNode(), message("error.reserved.tag.value", tag)); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void checkReservedFieldNames(MessageNode message, Collection<MessageField> fields) { |
| 131 | + Set<String> names = message.getReservedFieldNames(); |
| 132 | + for (MessageField field : fields) { |
| 133 | + String name = field.getFieldName(); |
| 134 | + if (names.contains(name)) { |
| 135 | + markError(field.getNode(), field.getFieldNameNode(), message("error.reserved.field.name", name)); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private void checkInvalidFieldTags(Collection<MessageField> fields) { |
| 141 | + for (MessageField field : fields) { |
| 142 | + int tag = field.getTag(); |
| 143 | + if (!isValidTagValue(tag)) { |
| 144 | + markError(field.getNode(), field.getTagNode(), message("error.invalid.tag.not.in.range", |
| 145 | + tag, MIN_TAG, SYS_RESERVED_START, SYS_RESERVED_END, MAX_TAG)); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void markError(ASTNode parent, @Nullable ASTNode node, String message) { |
| 151 | + Annotation annotation; |
| 152 | + if (node == null) { |
| 153 | + annotation = holder.createErrorAnnotation(parent, message); |
| 154 | + } else { |
| 155 | + annotation = holder.createErrorAnnotation(node, message); |
| 156 | + } |
| 157 | + annotation.setHighlightType(ProblemHighlightType.GENERIC_ERROR); |
| 158 | + } |
| 159 | + |
| 160 | + @VisibleForTesting |
| 161 | + boolean isValidTagValue(int tag) { |
| 162 | + return tag >= MIN_TAG && tag <= MAX_TAG |
| 163 | + && !(tag >= SYS_RESERVED_START && tag <= SYS_RESERVED_END); |
| 164 | + } |
| 165 | + |
| 166 | + private void checkDuplicateFieldTags(Collection<MessageField> fields) { |
| 167 | + Map<Integer, MessageField> fieldByTag = new HashMap<>(); |
| 168 | + for (MessageField field : fields) { |
| 169 | + int tag = field.getTag(); |
| 170 | + if (fieldByTag.containsKey(tag)) { |
| 171 | + String message = message("error.duplicate.field.tag", tag); |
| 172 | + markError(field.getNode(), fieldByTag.get(tag).getTagNode(), message); |
| 173 | + markError(field.getNode(), field.getTagNode(), message); |
| 174 | + } |
| 175 | + fieldByTag.put(tag, field); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private void checkDuplicateFieldNames(Collection<MessageField> fields) { |
| 180 | + Map<String, MessageField> fieldByName = new HashMap<>(); |
| 181 | + for (MessageField field : fields) { |
| 182 | + String name = field.getFieldName(); |
| 183 | + if (fieldByName.containsKey(name)) { |
| 184 | + String message = message("error.duplicate.field.name", name); |
| 185 | + markError(field.getNode(), fieldByName.get(name).getFieldNameNode(), message); |
| 186 | + markError(field.getNode(), field.getFieldNameNode(), message); |
| 187 | + } |
| 188 | + fieldByName.put(name, field); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments