Search in sources :

Example 1 with JsonNumberLiteral

use of com.intellij.json.psi.JsonNumberLiteral in project intellij-community by JetBrains.

the class JsonLiteralAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    final String text = JsonPsiUtil.getElementTextWithoutHostEscaping(element);
    if (element instanceof JsonStringLiteral) {
        final JsonStringLiteral stringLiteral = (JsonStringLiteral) element;
        final int elementOffset = element.getTextOffset();
        if (JsonPsiUtil.isPropertyKey(element)) {
            holder.createInfoAnnotation(element, Holder.DEBUG ? "property key" : null).setTextAttributes(JsonSyntaxHighlighterFactory.JSON_PROPERTY_KEY);
        }
        final int length = text.length();
        // Check that string literal is closed properly
        if (length <= 1 || text.charAt(0) != text.charAt(length - 1) || JsonPsiUtil.isEscapedChar(text, length - 1)) {
            holder.createErrorAnnotation(element, JsonBundle.message("syntax.error.missing.closing.quote"));
        }
        // Check escapes
        final List<Pair<TextRange, String>> fragments = stringLiteral.getTextFragments();
        for (Pair<TextRange, String> fragment : fragments) {
            final String fragmentText = fragment.getSecond();
            if (fragmentText.startsWith("\\") && fragmentText.length() > 1 && !VALID_ESCAPE.matcher(fragmentText).matches()) {
                final TextRange fragmentRange = fragment.getFirst();
                if (fragmentText.startsWith("\\u")) {
                    holder.createErrorAnnotation(fragmentRange.shiftRight(elementOffset), JsonBundle.message("syntax.error.illegal.unicode.escape.sequence"));
                } else {
                    holder.createErrorAnnotation(fragmentRange.shiftRight(elementOffset), JsonBundle.message("syntax.error.illegal.escape.sequence"));
                }
            }
        }
    } else if (element instanceof JsonNumberLiteral) {
        if (!VALID_NUMBER_LITERAL.matcher(text).matches()) {
            holder.createErrorAnnotation(element, JsonBundle.message("syntax.error.illegal.floating.point.literal"));
        }
    }
}
Also used : JsonNumberLiteral(com.intellij.json.psi.JsonNumberLiteral) TextRange(com.intellij.openapi.util.TextRange) JsonStringLiteral(com.intellij.json.psi.JsonStringLiteral) Pair(com.intellij.openapi.util.Pair)

Aggregations

JsonNumberLiteral (com.intellij.json.psi.JsonNumberLiteral)1 JsonStringLiteral (com.intellij.json.psi.JsonStringLiteral)1 Pair (com.intellij.openapi.util.Pair)1 TextRange (com.intellij.openapi.util.TextRange)1