Search in sources :

Example 1 with JsonStringLiteral

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

the class JsonBySchemaDocumentationProvider method generateDoc.

@Nullable
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    final JsonLikePsiWalker walker = JsonSchemaWalker.getWalker(element, myRootSchema);
    if (walker == null)
        return null;
    if (JsonSchemaFileType.INSTANCE.equals(element.getContainingFile().getFileType())) {
        final JsonProperty jsonProperty = element instanceof JsonProperty ? (JsonProperty) element : PsiTreeUtil.getParentOfType(element, JsonProperty.class);
        if (jsonProperty != null) {
            final JsonValue value = jsonProperty.getValue();
            if (value instanceof JsonObject) {
                final JsonProperty description = ((JsonObject) value).findProperty("description");
                if (description != null && description.getValue() instanceof JsonStringLiteral) {
                    return StringUtil.escapeXml(StringUtil.unquoteString(description.getValue().getText()));
                }
            }
            return null;
        }
    }
    final Ref<String> result = Ref.create();
    JsonSchemaWalker.findSchemasForDocumentation(element, walker, new JsonSchemaWalker.CompletionSchemesConsumer() {

        @Override
        public void consume(boolean isName, @NotNull JsonSchemaObject schema, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
            result.set(schema.getDescription());
        }

        @Override
        public void oneOf(boolean isName, @NotNull List<JsonSchemaObject> list, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
        //todo?
        }

        @Override
        public void anyOf(boolean isName, @NotNull List<JsonSchemaObject> list, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
        //todo?
        }
    }, myRootSchema, mySchemaFile);
    return result.get();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JsonProperty(com.intellij.json.psi.JsonProperty) JsonValue(com.intellij.json.psi.JsonValue) JsonObject(com.intellij.json.psi.JsonObject) JsonStringLiteral(com.intellij.json.psi.JsonStringLiteral) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with JsonStringLiteral

use of com.intellij.json.psi.JsonStringLiteral 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)

Example 3 with JsonStringLiteral

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

the class JsonStringPropertyInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    PsiElement element = context.getFile().findElementAt(context.getStartOffset());
    JsonStringLiteral literal = PsiTreeUtil.getParentOfType(element, JsonStringLiteral.class, false);
    if (literal == null)
        return;
    JsonProperty property = ObjectUtils.tryCast(literal.getParent(), JsonProperty.class);
    if (property == null)
        return;
    final TextRange toDelete;
    String textToInsert = "";
    TextRange literalRange = literal.getTextRange();
    if (literal.getValue().equals(myNewValue)) {
        toDelete = new TextRange(literalRange.getEndOffset(), literalRange.getEndOffset());
    } else {
        toDelete = literalRange;
        textToInsert = StringUtil.wrapWithDoubleQuote(myNewValue);
    }
    int newCaretOffset = literalRange.getStartOffset() + 1 + myNewValue.length();
    boolean showAutoPopup = false;
    if (property.getNameElement().equals(literal)) {
        if (property.getValue() == null) {
            textToInsert += ":\"\"";
            // "package<caret offset>":"<new caret offset>"
            newCaretOffset += 3;
            if (needCommaAfter(property)) {
                textToInsert += ",";
            }
            showAutoPopup = true;
        }
    }
    context.getDocument().replaceString(toDelete.getStartOffset(), toDelete.getEndOffset(), textToInsert);
    context.getEditor().getCaretModel().moveToOffset(newCaretOffset);
    reformat(context, toDelete.getStartOffset(), toDelete.getStartOffset() + textToInsert.length());
    if (showAutoPopup) {
        AutoPopupController.getInstance(context.getProject()).autoPopupMemberLookup(context.getEditor(), null);
    }
}
Also used : JsonProperty(com.intellij.json.psi.JsonProperty) TextRange(com.intellij.openapi.util.TextRange) JsonStringLiteral(com.intellij.json.psi.JsonStringLiteral) PsiElement(com.intellij.psi.PsiElement)

Aggregations

JsonStringLiteral (com.intellij.json.psi.JsonStringLiteral)3 JsonProperty (com.intellij.json.psi.JsonProperty)2 TextRange (com.intellij.openapi.util.TextRange)2 JsonNumberLiteral (com.intellij.json.psi.JsonNumberLiteral)1 JsonObject (com.intellij.json.psi.JsonObject)1 JsonValue (com.intellij.json.psi.JsonValue)1 Pair (com.intellij.openapi.util.Pair)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 Nullable (org.jetbrains.annotations.Nullable)1