Search in sources :

Example 1 with JsonProperty

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

the class JsonPropertyNameReference method isReferenceTo.

@Override
public boolean isReferenceTo(PsiElement element) {
    if (!(element instanceof JsonProperty)) {
        return false;
    }
    // May reference to the property with the same name for compatibility with JavaScript JSON support
    final JsonProperty otherProperty = (JsonProperty) element;
    final PsiElement selfResolve = resolve();
    return otherProperty.getName().equals(getCanonicalText()) && selfResolve != otherProperty;
}
Also used : JsonProperty(com.intellij.json.psi.JsonProperty) PsiElement(com.intellij.psi.PsiElement)

Example 2 with JsonProperty

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

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

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

the class JsonPsiUtilTest method testGetOtherSiblingPropertyNames.

public void testGetOtherSiblingPropertyNames() throws Exception {
    myFixture.configureByText(JsonFileType.INSTANCE, "{\"firs<caret>t\" : 1, \"second\" : 2}");
    PsiElement atCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
    JsonProperty property = PsiTreeUtil.getParentOfType(atCaret, JsonProperty.class);
    assertNotNull(property);
    assertEquals(Collections.singleton("second"), JsonPsiUtil.getOtherSiblingPropertyNames(property));
}
Also used : JsonProperty(com.intellij.json.psi.JsonProperty) PsiElement(com.intellij.psi.PsiElement)

Example 5 with JsonProperty

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

JsonProperty (com.intellij.json.psi.JsonProperty)8 PsiElement (com.intellij.psi.PsiElement)6 JsonElement (com.intellij.json.psi.JsonElement)2 JsonObject (com.intellij.json.psi.JsonObject)2 JsonStringLiteral (com.intellij.json.psi.JsonStringLiteral)2 JsonValue (com.intellij.json.psi.JsonValue)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 Ref (com.intellij.openapi.util.Ref)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 JsonSchemaObject (com.jetbrains.jsonSchema.impl.JsonSchemaObject)1 JsonSchemaWalker (com.jetbrains.jsonSchema.impl.JsonSchemaWalker)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1