Search in sources :

Example 1 with JsonValue

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

the class JsonSchemaReader method create.

@Nullable
public static JsonSchemaReader create(@NotNull Project project, @NotNull VirtualFile key) {
    final PsiFile psiFile = PsiManager.getInstance(project).findFile(key);
    if (!(psiFile instanceof JsonFile))
        return null;
    final List<JsonValue> values = ((JsonFile) psiFile).getAllTopLevelValues();
    if (values.size() != 1 || !(values.get(0) instanceof JsonObject))
        return null;
    return new JsonSchemaReader((JsonObject) values.get(0));
}
Also used : JsonValue(com.intellij.json.psi.JsonValue) JsonFile(com.intellij.json.psi.JsonFile) JsonObject(com.intellij.json.psi.JsonObject) PsiFile(com.intellij.psi.PsiFile) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with JsonValue

use of com.intellij.json.psi.JsonValue 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 3 with JsonValue

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

the class JsonSurroundDescriptor method getElementsToSurround.

@NotNull
@Override
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
    PsiElement firstElement = file.findElementAt(startOffset);
    PsiElement lastElement = file.findElementAt(endOffset - 1);
    // Extend selection beyond possible delimiters
    while (firstElement != null && (firstElement instanceof PsiWhiteSpace || firstElement.getNode().getElementType() == JsonElementTypes.COMMA)) {
        firstElement = firstElement.getNextSibling();
    }
    while (lastElement != null && (lastElement instanceof PsiWhiteSpace || lastElement.getNode().getElementType() == JsonElementTypes.COMMA)) {
        lastElement = lastElement.getPrevSibling();
    }
    if (firstElement != null) {
        startOffset = firstElement.getTextRange().getStartOffset();
    }
    if (lastElement != null) {
        endOffset = lastElement.getTextRange().getEndOffset();
    }
    final JsonElement property = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, JsonProperty.class);
    if (property != null) {
        final List<JsonElement> properties = ContainerUtil.newArrayList(property);
        PsiElement nextSibling = property.getNextSibling();
        while (nextSibling != null && nextSibling.getTextRange().getEndOffset() <= endOffset) {
            if (nextSibling instanceof JsonProperty) {
                properties.add((JsonProperty) nextSibling);
            }
            nextSibling = nextSibling.getNextSibling();
        }
        return properties.toArray(new PsiElement[properties.size()]);
    }
    final JsonValue value = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, JsonValue.class);
    if (value != null) {
        return new PsiElement[] { value };
    }
    return PsiElement.EMPTY_ARRAY;
}
Also used : JsonProperty(com.intellij.json.psi.JsonProperty) JsonElement(com.intellij.json.psi.JsonElement) JsonValue(com.intellij.json.psi.JsonValue) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

JsonValue (com.intellij.json.psi.JsonValue)3 JsonObject (com.intellij.json.psi.JsonObject)2 JsonProperty (com.intellij.json.psi.JsonProperty)2 Nullable (org.jetbrains.annotations.Nullable)2 JsonElement (com.intellij.json.psi.JsonElement)1 JsonFile (com.intellij.json.psi.JsonFile)1 JsonStringLiteral (com.intellij.json.psi.JsonStringLiteral)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 NotNull (org.jetbrains.annotations.NotNull)1