Search in sources :

Example 1 with JsonObject

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

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

the class JsonSchemaInsideSchemaResolver method resolveInSchemaRecursively.

public PsiElement resolveInSchemaRecursively() {
    final Ref<PsiElement> ref = new Ref<>();
    final JsonSchemaWalker.CompletionSchemesConsumer consumer = new JsonSchemaWalker.CompletionSchemesConsumer() {

        @Override
        public void consume(boolean isName, @NotNull JsonSchemaObject schema, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
            if (!ref.isNull())
                return;
            final PsiFile file = PsiManager.getInstance(myProject).findFile(mySchemaFile);
            if (file == null)
                return;
            final JsonObject jsonObject = schema.getPeerPointer().getElement();
            if (jsonObject != null && jsonObject.isValid()) {
                if (jsonObject.getParent() instanceof JsonProperty)
                    ref.set(((JsonProperty) jsonObject.getParent()).getNameElement());
                else
                    ref.set(jsonObject);
            }
        }

        @Override
        public void oneOf(boolean isName, @NotNull List<JsonSchemaObject> list, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
            list.stream().findFirst().ifPresent(object -> consume(isName, object, schemaFile, steps));
        }

        @Override
        public void anyOf(boolean isName, @NotNull List<JsonSchemaObject> list, @NotNull VirtualFile schemaFile, @NotNull List<JsonSchemaWalker.Step> steps) {
            list.stream().findFirst().ifPresent(object -> consume(isName, object, schemaFile, steps));
        }
    };
    JsonSchemaService.Impl.getEx(myProject).visitSchemaObject(mySchemaFile, object -> {
        JsonSchemaWalker.extractSchemaVariants(myProject, consumer, mySchemaFile, object, true, mySteps, false);
        return true;
    });
    return ref.get();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JsonProperty(com.intellij.json.psi.JsonProperty) JsonObject(com.intellij.json.psi.JsonObject) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) JsonSchemaWalker(com.jetbrains.jsonSchema.impl.JsonSchemaWalker) List(java.util.List) PsiFile(com.intellij.psi.PsiFile) JsonSchemaObject(com.jetbrains.jsonSchema.impl.JsonSchemaObject) PsiElement(com.intellij.psi.PsiElement)

Example 3 with JsonObject

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

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

the class JsonPsiUtilTest method checkAddProperty.

private void checkAddProperty(@NotNull String before, @NotNull String after, final boolean first) {
    getCustomCodeStyleSettings().OBJECT_WRAPPING = CommonCodeStyleSettings.DO_NOT_WRAP;
    myFixture.configureByText(JsonFileType.INSTANCE, before);
    final PsiElement atCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    final JsonObject jsonObject = PsiTreeUtil.getParentOfType(atCaret, JsonObject.class);
    assertNotNull(jsonObject);
    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
        JsonPsiUtil.addProperty(jsonObject, new JsonElementGenerator(getProject()).createProperty("foo", "null"), first);
    });
    myFixture.checkResult(after);
}
Also used : JsonElementGenerator(com.intellij.json.psi.JsonElementGenerator) JsonObject(com.intellij.json.psi.JsonObject) PsiElement(com.intellij.psi.PsiElement)

Aggregations

JsonObject (com.intellij.json.psi.JsonObject)4 JsonProperty (com.intellij.json.psi.JsonProperty)2 JsonValue (com.intellij.json.psi.JsonValue)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 Nullable (org.jetbrains.annotations.Nullable)2 JsonElementGenerator (com.intellij.json.psi.JsonElementGenerator)1 JsonFile (com.intellij.json.psi.JsonFile)1 JsonStringLiteral (com.intellij.json.psi.JsonStringLiteral)1 Ref (com.intellij.openapi.util.Ref)1 JsonSchemaObject (com.jetbrains.jsonSchema.impl.JsonSchemaObject)1 JsonSchemaWalker (com.jetbrains.jsonSchema.impl.JsonSchemaWalker)1 List (java.util.List)1 NotNull (org.jetbrains.annotations.NotNull)1