Search in sources :

Example 31 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JSONModelParser method setupContext.

/**
 * setupContext method
 */
private void setupContext(IStructuredDocumentRegion startStructuredDocumentRegion) {
    int offset = startStructuredDocumentRegion.getStart();
    if (offset < 0)
        return;
    JSONNodeImpl root = (JSONNodeImpl) this.context.getRootNode();
    if (root == null)
        return;
    if (offset == 0) {
        // at the beginning of document
        IJSONNode child = root.getFirstChild();
        if (child != null)
            this.context.setCurrentNode(child);
        else
            this.context.setParentNode(root);
        return;
    }
    // Get the JSON Node at the specified position in the document, this
    // position is the start offset of the flatNode
    JSONNodeImpl node = (JSONNodeImpl) root.getNodeAt(offset);
    if (node == null) {
        // might be at the end of document
        this.context.setParentNode(root);
        this.context.setLast();
        return;
    }
    if (node.getStartStructuredDocumentRegion() != null) {
        if (offset == node.getStartStructuredDocumentRegion().getStartOffset()) {
            // This is a JSONPair and the flatNode might be and update of
            // the object key value
            this.context.setCurrentNode(node);
            return;
        } else if (node.getEndStructuredDocumentRegion() != null) {
            if (offset == node.getEndStructuredDocumentRegion().getStartOffset()) {
                // This is a JSONPair and the flatNode might be an update of
                // the pair value
                this.context.setCurrentNode(node);
                return;
            }
        }
    }
    if (node instanceof JSONPairImpl && ((JSONPairImpl) node).getValue() != null) {
        if (isJSONValue(startStructuredDocumentRegion.getType())) {
            // This is a JSONPair and the flatNode might be the pair value
            this.context.setCurrentNode(node);
            return;
        }
    }
    // into the children
    for (IJSONNode child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (offset >= ((JSONNodeImpl) child).getEndOffset()) {
            if (child instanceof JSONPairImpl && ((JSONPairImpl) child).getValue() == null) {
                if (isJSONValue(startStructuredDocumentRegion.getType())) {
                    this.context.setCurrentNode(child);
                    return;
                }
            } else {
                continue;
            }
        }
        this.context.setCurrentNode(child);
        return;
    }
    // The node is tha paret node of the flatNode
    this.context.setParentNode(node);
    this.context.setLast();
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 32 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JSONPairFormatter method formatChildren.

@Override
protected void formatChildren(IJSONNode node, IRegion region, StringBuilder source) {
    IJSONNode child = node.getFirstChild();
    int start = region.getOffset();
    int end = region.getOffset() + region.getLength();
    while (child != null) {
        int curEnd = child.getEndOffset();
        StringBuilder childSource = null;
        boolean toFinish = false;
        if (start < curEnd) {
            int curStart = child.getStartOffset();
            if (curStart < end) {
                // append child
                IJSONSourceFormatter formatter = (IJSONSourceFormatter) ((INodeNotifier) child).getAdapterFor(IJSONSourceFormatter.class);
                if (formatter == null) {
                    formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter(child);
                }
                if (includes(region, curStart, curEnd))
                    childSource = ((AbstractJSONSourceFormatter) formatter).formatProc(child);
                else
                    childSource = ((AbstractJSONSourceFormatter) formatter).formatProc(child, overlappedRegion(region, curStart, curEnd));
            } else
                toFinish = true;
        }
        if (childSource != null) {
            source.append(childSource);
        }
        if (toFinish)
            break;
        child = child.getNextSibling();
    }
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 33 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class Validator method validateUniqueItems.

private void validateUniqueItems(IJSONNode node, JsonObject schema, IJSONValue value, JsonValue memberValue, JSONValidationInfo valinfo) {
    if (memberValue != null && memberValue.isBoolean() && memberValue.asBoolean()) {
        if (value instanceof IJSONArray) {
            Set<String> instanceValues = new HashSet<String>();
            IJSONNode child = value.getFirstChild();
            int instanceSize = 0;
            while (child != null) {
                instanceSize = instanceSize + 1;
                instanceValues.add(JSONUtil.getString(child));
                child = child.getNextSibling();
            }
            ;
            if (instanceSize != instanceValues.size()) {
                int offset = node.getStartOffset();
                int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
                valinfo.addMessage("Array has duplicate items", line, 0, offset == 0 ? 1 : offset);
            }
        }
    }
}
Also used : IJSONArray(org.eclipse.wst.json.core.document.IJSONArray) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) HashSet(java.util.HashSet)

Example 34 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JSONHoverProcessor method computeHoverHelp.

/**
 * Retrieves documentation to display in the hover help popup.
 *
 * @return String any documentation information to display <code>null</code>
 *         if there is nothing to display.
 */
protected String computeHoverHelp(ITextViewer textViewer, int documentPosition) {
    String result = null;
    IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition);
    if (treeNode == null) {
        return null;
    }
    IJSONNode node = (IJSONNode) treeNode;
    IJSONNode parentNode = node.getParentNode();
    IStructuredDocumentRegion flatNode = ((IStructuredDocument) textViewer.getDocument()).getRegionAtCharacterOffset(documentPosition);
    if (flatNode != null) {
        ITextRegion region = flatNode.getRegionAtCharacterOffset(documentPosition);
        if (region != null) {
            result = computeRegionHelp(treeNode, parentNode, flatNode, region);
        }
    }
    return result;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 35 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JSONModelParser method changeAttrName.

/**
 * changeAttrName method
 */
private void changeAttrName(IStructuredDocumentRegion flatNode, ITextRegion region) {
    int offset = flatNode.getStart();
    if (offset < 0)
        return;
    JSONNodeImpl root = (JSONNodeImpl) this.context.getRootNode();
    if (root == null)
        return;
    IJSONNode node = root.getNodeAt(offset);
    if (node == null)
        return;
    if (node.getNodeType() != IJSONNode.PAIR_NODE) {
        return;
    }
    JSONPairImpl pair = (JSONPairImpl) node;
    String name = flatNode.getText(region);
    pair.setName(name);
    JSONObjectImpl parentObj = (JSONObjectImpl) node.getParentNode();
    if (parentObj != null && parentObj.getParentOrPairNode() instanceof JSONPairImpl) {
        JSONPairImpl parentPair = (JSONPairImpl) parentObj.getParentOrPairNode();
        if (parentPair.getValue() != parentObj.getParentNode()) {
            parentPair.setValue(parentObj);
        }
    }
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Aggregations

IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)56 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)10 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)9 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)9 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)8 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)4 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)4 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)3 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1