Search in sources :

Example 11 with IJSONValue

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

the class JSONModelParser method removeNode.

/**
 * removeNode method
 *
 * @param node
 *            org.w3c.dom.Node
 */
private void removeNode(IJSONNode node) {
    if (node == null)
        return;
    if (this.context == null)
        return;
    IJSONNode parent = node.getParentNode();
    if (parent == null) {
        if (node instanceof IJSONValue) {
            parent = node.getParentOrPairNode();
            if (parent == null) {
                return;
            }
        }
    }
    IJSONNode next = node.getNextSibling();
    IJSONNode prev = node.getPreviousSibling();
    // update context
    IJSONNode oldParent = this.context.getParentNode();
    if (node == oldParent) {
        if (next != null)
            this.context.setCurrentNode(next);
        else
            this.context.setParentNode(parent);
    } else {
        IJSONNode oldNext = this.context.getNextNode();
        if (node == oldNext) {
            this.context.setCurrentNode(next);
        }
    }
    parent.removeChild(node);
    // demote sibling
    if (prev != null && prev.getNodeType() == IJSONNode.OBJECT_NODE) {
        JSONObjectImpl newElement = (JSONObjectImpl) prev;
        if (!newElement.hasEndTag() && !newElement.isEmptyTag() && newElement.isContainer()) {
            // find new parent
            for (IJSONNode last = newElement.getLastChild(); last != null; last = last.getLastChild()) {
                if (last.getNodeType() != IJSONNode.OBJECT_NODE)
                    break;
                JSONObjectImpl lastElement = (JSONObjectImpl) last;
                if (lastElement.hasEndTag() || lastElement.isEmptyTag() || !lastElement.isContainer())
                    break;
                newElement = lastElement;
            }
            IJSONNode lastChild = newElement.getLastChild();
            demoteNodes(prev, newElement, parent, next);
            // update context
            IJSONNode newNext = null;
            if (lastChild != null)
                newNext = lastChild.getNextSibling();
            else
                newNext = newElement.getFirstChild();
            if (newNext != null)
                this.context.setCurrentNode(newNext);
            else
                this.context.setParentNode(newElement);
        }
    }
}
Also used : IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 12 with IJSONValue

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

the class JSONNodeImpl method getNodeAt.

/**
 * getNodeAt method
 *
 * @return org.w3c.dom.Node
 * @param offset
 *            int
 */
IJSONNode getNodeAt(int offset) {
    IJSONNode parent = this;
    IJSONNode child = getFirstChild();
    while (child != null) {
        if (child.getEndOffset() == offset) {
            return child;
        }
        if (child.getEndOffset() <= offset) {
            child = child.getNextSibling();
            continue;
        }
        if (child.getStartOffset() > offset) {
            break;
        }
        IStructuredDocumentRegion startStructuredDocumentRegion = child.getStartStructuredDocumentRegion();
        if (startStructuredDocumentRegion != null) {
            if (startStructuredDocumentRegion.getEnd() > offset)
                return child;
        }
        // dig more
        parent = child;
        // JSONNode like JSONObject or JSONArray
        if (parent instanceof JSONPairImpl) {
            IJSONValue value = ((JSONPairImpl) parent).getValue();
            if (value instanceof JSONObjectImpl || value instanceof JSONArrayImpl) {
                parent = value;
            }
        }
        child = parent.getFirstChild();
    }
    return parent;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 13 with IJSONValue

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

the class JSONPairImpl method updateValue.

public void updateValue(IJSONValue value) {
    IJSONValue oldValue = this.value;
    ((JSONValueImpl) value).setParentNode(ownerObject);
    ((JSONValueImpl) value).setOwnerPairNode(this);
    this.value = value;
    notify(CHANGE, null, oldValue, this.value, getStartOffset());
}
Also used : IJSONValue(org.eclipse.wst.json.core.document.IJSONValue)

Example 14 with IJSONValue

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

the class JFaceNodeAdapter method getStyledLabelText.

@Override
public StyledString getStyledLabelText(Object element) {
    StyledString styledString = new StyledString();
    if (element instanceof IJSONNode) {
        IJSONNode node = (IJSONNode) element;
        switch(node.getNodeType()) {
            case IJSONNode.PAIR_NODE:
                IJSONPair pair = ((IJSONPair) node);
                String name = pair.getName();
                if (name != null) {
                    styledString.append(name);
                    String value = pair.getSimpleValue();
                    if (value != null) {
                        styledString.append(" : ");
                        Styler styler = fAdapterFactory.getStyler(pair.getValueRegionType());
                        styledString.append(value, styler);
                    }
                }
                break;
            case IJSONNode.VALUE_BOOLEAN_NODE:
            case IJSONNode.VALUE_NULL_NODE:
            case IJSONNode.VALUE_NUMBER_NODE:
            case IJSONNode.VALUE_STRING_NODE:
                String value = ((IJSONValue) node).getSimpleValue();
                if (value != null) {
                    styledString.append(" : ");
                    Styler styler = fAdapterFactory.getStyler(((IJSONValue) node).getValueRegionType());
                    styledString.append(value, styler);
                }
                break;
        }
    }
    return styledString;
}
Also used : IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) Styler(org.eclipse.jface.viewers.StyledString.Styler) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 15 with IJSONValue

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

the class AbstractJSONCompletionProposalComputer method computeObjectKeyProposals.

private ContentAssistRequest computeObjectKeyProposals(String matchString, ITextRegion completionRegion, IJSONNode nodeAtOffset, IJSONNode node, CompletionProposalInvocationContext context) {
    int documentPosition = context.getInvocationOffset();
    ContentAssistRequest contentAssistRequest = null;
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    int replaceLength = 0;
    int begin = documentPosition;
    if (completionRegion.getType() == JSONRegionContexts.JSON_OBJECT_KEY || completionRegion.getType() == JSONRegionContexts.JSON_UNKNOWN) {
        replaceLength = completionRegion.getTextLength();
        // value region not the entire container
        if (completionRegion instanceof ITextRegionContainer) {
            ITextRegion openRegion = ((ITextRegionContainer) completionRegion).getFirstRegion();
            ITextRegion closeRegion = ((ITextRegionContainer) completionRegion).getLastRegion();
            if (openRegion.getType() != closeRegion.getType()) {
                replaceLength = openRegion.getTextLength();
            }
        }
        begin = sdRegion.getStartOffset(completionRegion);
    }
    if (isPairValue(context, nodeAtOffset)) {
        IJSONPair pair = (IJSONPair) nodeAtOffset;
        IJSONValue value = pair.getValue();
        if (value != null) {
            try {
                begin = value.getStartOffset();
                String valueText = getNodeText(value);
                valueText = valueText.trim();
                replaceLength = valueText.length();
                if (valueText.startsWith(QUOTE)) {
                    begin = begin + 1;
                    replaceLength = replaceLength - 1;
                }
                if (valueText.endsWith(QUOTE)) {
                    replaceLength = replaceLength - 1;
                }
            } catch (BadLocationException e) {
            // ignore
            }
        }
    } else if (nodeAtOffset instanceof IJSONPair) {
        IJSONPair pair = (IJSONPair) nodeAtOffset;
        try {
            begin = pair.getStartOffset();
            String text = getNodeText(pair);
            text = text.trim();
            replaceLength = pair.getName().length();
            if (text.startsWith(QUOTE)) {
                begin = begin + 1;
            }
        } catch (BadLocationException e) {
        // ignore
        }
    }
    contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node.getParentNode(), sdRegion, completionRegion, begin, replaceLength, matchString);
    addObjectKeyProposals(contentAssistRequest, context);
    return contentAssistRequest;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)15 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)10 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)8 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)3 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)3 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)3 JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)2 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)2 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)2 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 StyledString (org.eclipse.jface.viewers.StyledString)1 Styler (org.eclipse.jface.viewers.StyledString.Styler)1 IJSONPath (org.eclipse.json.jsonpath.IJSONPath)1 IJSONSchemaDocument (org.eclipse.json.schema.IJSONSchemaDocument)1