Search in sources :

Example 6 with JsonProperty

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

the class JsonQualifiedNameProvider method getQualifiedName.

@Nullable
@Override
public String getQualifiedName(PsiElement element) {
    if (!(element instanceof JsonElement)) {
        return null;
    }
    final LinkedList<String> qualifiers = new LinkedList<>();
    JsonProperty parentProperty = PsiTreeUtil.getNonStrictParentOfType(element, JsonProperty.class);
    while (parentProperty != null) {
        qualifiers.addFirst(parentProperty.getName());
        parentProperty = PsiTreeUtil.getParentOfType(parentProperty, JsonProperty.class);
    }
    return qualifiers.isEmpty() ? null : StringUtil.join(qualifiers, ".");
}
Also used : JsonProperty(com.intellij.json.psi.JsonProperty) JsonElement(com.intellij.json.psi.JsonElement) LinkedList(java.util.LinkedList) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with JsonProperty

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

the class JsonSchemaCrossReferencesTest method checkNavigationIntoDefinition.

private void checkNavigationIntoDefinition(String name) {
    int offset = myEditor.getCaretModel().getPrimaryCaret().getOffset();
    final PsiElement element = myFile.findElementAt(offset);
    Assert.assertNotNull(element);
    final PsiReference referenceAt = myFile.findReferenceAt(offset);
    Assert.assertNotNull(referenceAt);
    final PsiElement resolve = referenceAt.resolve();
    Assert.assertNotNull(resolve);
    Assert.assertEquals("\"" + name + "\"", resolve.getText());
    final PsiElement parent = resolve.getParent();
    Assert.assertTrue(parent instanceof JsonProperty);
    Assert.assertEquals(name, ((JsonProperty) parent).getName());
    Assert.assertTrue(parent.getParent().getParent() instanceof JsonProperty);
    Assert.assertEquals("definitions", ((JsonProperty) parent.getParent().getParent()).getName());
}
Also used : JsonProperty(com.intellij.json.psi.JsonProperty) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Example 8 with JsonProperty

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

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