Search in sources :

Example 46 with IJSONNode

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

the class JSONModelContext method setLast.

/**
 * setLast method
 */
void setLast() {
    if (this.parentNode == null)
        return;
    if (this.nextNode != null) {
        IJSONNode prev = this.nextNode.getPreviousSibling();
        if (prev == null || prev.getNodeType() != IJSONNode.OBJECT_NODE)
            return;
        JSONObjectImpl element = (JSONObjectImpl) prev;
        if (element.hasEndTag() || !element.isContainer() || element.isEmptyTag())
            return;
        setParentNode(prev);
    }
    // find last open parent
    IJSONNode parent = this.parentNode;
    IJSONNode last = parent.getLastChild();
    while (last != null) {
        if (last.getNodeType() != IJSONNode.OBJECT_NODE)
            break;
        JSONObjectImpl element = (JSONObjectImpl) last;
        if (element.hasEndTag() || !element.isContainer() || element.isEmptyTag())
            break;
        parent = element;
        last = parent.getLastChild();
    }
    if (parent != this.parentNode)
        setParentNode(parent);
}
Also used : JSONObjectImpl(org.eclipse.wst.json.core.internal.document.JSONObjectImpl) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 47 with IJSONNode

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

the class JSONModelParser method removeStartObject.

/**
 * removeStartTag method
 *
 * @param element
 *            org.w3c.dom.Element
 */
private void removeStartObject(IJSONObject element) {
    if (element == null)
        return;
    if (this.context == null)
        return;
    // for implicit tag
    JSONObjectImpl oldElement = (JSONObjectImpl) element;
    // if (canBeImplicitTag(oldElement)) {
    // Node newParent = null;
    // Node prev = oldElement.getPreviousSibling();
    // if (prev != null && prev.getNodeType() == IJSONNode.OBJECT_NODE) {
    // JSONObjectImpl prevElement = (JSONObjectImpl) prev;
    // if (!prevElement.hasEndTag()) {
    // if (prevElement.hasStartTag()
    // || prevElement
    // .matchTagName(oldElement.getTagName())) {
    // newParent = prevElement;
    // }
    // }
    // }
    // if (newParent == null) {
    // // this element should stay as implicit tag
    // // just remove all attributes
    // oldElement.removeStartTag();
    // 
    // // update context
    // Node child = oldElement.getFirstChild();
    // if (child != null) {
    // this.context.setCurrentNode(child);
    // } else if (oldElement.hasEndTag()) {
    // this.context.setParentNode(oldElement);
    // }
    // return;
    // }
    // }
    // // for comment tag
    // if (oldElement.isCommentTag())
    // oldElement.removeStartTag();
    // promote children
    IJSONNode elementParent = element.getParentNode();
    IJSONNode parent = elementParent;
    if (parent == null)
        return;
    IJSONNode first = element.getFirstChild();
    // for the case first is removed as end
    IJSONNode firstElement = null;
    // tag
    if (first != null) {
        // find new parent for children
        JSONObjectImpl newElement = null;
        for (IJSONNode last = element.getPreviousSibling(); 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 next = first;
        if (newElement != null) {
            while (next != null) {
                if (!newElement.hasEndTag() && newElement.hasStartTag() && next.getNodeType() == IJSONNode.OBJECT_NODE) {
                    JSONObjectImpl nextElement = (JSONObjectImpl) next;
                    if (!nextElement.hasStartTag() && nextElement.hasEndTag()) /* && nextElement.matchEndTag(newElement) */
                    {
                        // stop at the matched invalid end tag
                        IJSONNode elementChild = nextElement.getFirstChild();
                        while (elementChild != null) {
                            IJSONNode nextChild = elementChild.getNextSibling();
                            nextElement.removeChild(elementChild);
                            newElement.appendChild(elementChild);
                            elementChild = nextChild;
                        }
                        next = nextElement.getNextSibling();
                        element.removeChild(nextElement);
                        // newElement.addEndTag(nextElement);
                        if (nextElement == first)
                            firstElement = newElement;
                        IJSONNode newParent = newElement.getParentNode();
                        if (newParent == parent)
                            break;
                        if (newParent == null || newParent.getNodeType() != IJSONNode.OBJECT_NODE)
                            // error
                            break;
                        newElement = (JSONObjectImpl) newParent;
                        continue;
                    }
                }
                // if (!canContain(newElement, next)) {
                // Node newParent = newElement.getParentNode();
                // if (newParent == parent)
                // break;
                // if (newParent == null
                // || newParent.getNodeType() != IJSONNode.OBJECT_NODE)
                // break; // error
                // newElement = (JSONObjectImpl) newParent;
                // continue;
                // }
                IJSONNode child = next;
                next = next.getNextSibling();
                element.removeChild(child);
                newElement.appendChild(child);
            }
            newElement = null;
        }
        if (parent.getNodeType() == IJSONNode.OBJECT_NODE) {
            newElement = (JSONObjectImpl) parent;
        }
        while (next != null) {
            if (newElement == null) /* || canContain(newElement, next) */
            {
                IJSONNode child = next;
                next = next.getNextSibling();
                element.removeChild(child);
                parent.insertBefore(child, element);
                continue;
            }
            parent = newElement.getParentNode();
            if (parent == null)
                return;
            // promote siblings
            IJSONNode newNext = newElement.getNextSibling();
            IJSONNode child = element;
            while (child != null) {
                IJSONNode nextChild = child.getNextSibling();
                newElement.removeChild(child);
                parent.insertBefore(child, newNext);
                child = nextChild;
            }
            // leave the old end tag where it is
            if (newElement.hasEndTag()) {
            // IJSONObject end = newElement.removeEndTag();
            // if (end != null) {
            // parent.insertBefore(end, newNext);
            // }
            }
            if (!newElement.hasStartTag()) {
                // implicit element
                if (!newElement.hasChildNodes()) {
                    parent.removeChild(newElement);
                }
            }
            if (parent.getNodeType() == IJSONNode.OBJECT_NODE) {
                newElement = (JSONObjectImpl) parent;
            } else {
                newElement = null;
            }
        }
    }
    IJSONNode newNext = element;
    // for the case element is removed as end
    IJSONNode startElement = null;
    // tag
    if (oldElement.hasEndTag()) {
        // find new parent for invalid end tag and siblings
        JSONObjectImpl newElement = null;
        for (IJSONNode last = element.getPreviousSibling(); 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;
        }
        if (newElement != null) {
            // demote invalid end tag and sibling
            IJSONNode next = element;
            while (next != null) {
                if (!newElement.hasEndTag() && newElement.hasStartTag() && next.getNodeType() == IJSONNode.OBJECT_NODE) {
                    JSONObjectImpl nextElement = (JSONObjectImpl) next;
                    if (!nextElement.hasStartTag() && nextElement.hasEndTag()) /* && nextElement.matchEndTag(newElement) */
                    {
                        // stop at the matched invalid end tag
                        IJSONNode elementChild = nextElement.getFirstChild();
                        while (elementChild != null) {
                            IJSONNode nextChild = elementChild.getNextSibling();
                            nextElement.removeChild(elementChild);
                            newElement.appendChild(elementChild);
                            elementChild = nextChild;
                        }
                        next = nextElement.getNextSibling();
                        parent.removeChild(nextElement);
                        // newElement.addEndTag(nextElement);
                        if (nextElement == newNext)
                            startElement = newElement;
                        IJSONNode newParent = newElement.getParentNode();
                        if (newParent == parent)
                            break;
                        if (newParent == null || newParent.getNodeType() != IJSONNode.OBJECT_NODE)
                            // error
                            break;
                        newElement = (JSONObjectImpl) newParent;
                        continue;
                    }
                }
                // if (!canContain(newElement, next)) {
                // Node newParent = newElement.getParentNode();
                // if (newParent == parent)
                // break;
                // if (newParent == null
                // || newParent.getNodeType() != IJSONNode.OBJECT_NODE)
                // break; // error
                // newElement = (JSONObjectImpl) newParent;
                // continue;
                // }
                IJSONNode child = next;
                next = next.getNextSibling();
                parent.removeChild(child);
                if (child == oldElement) {
                // if (!oldElement.isCommentTag()) {
                // clone (re-create) end tag
                // IJSONObject end = oldElement.removeEndTag();
                // if (end != null) {
                // child = end;
                // newNext = end;
                // }
                // }
                }
                newElement.appendChild(child);
            }
        } else {
        // if (!oldElement.isCommentTag()) {
        // clone (re-create) end tag
        // IJSONObject end = oldElement.removeEndTag();
        // if (end != null) {
        // parent.insertBefore(end, oldElement);
        // parent.removeChild(oldElement);
        // newNext = end;
        // }
        // }
        }
    } else {
        newNext = oldElement.getNextSibling();
        parent.removeChild(oldElement);
    }
    // update context
    IJSONNode oldParent = this.context.getParentNode();
    IJSONNode oldNext = this.context.getNextNode();
    if (element == oldParent) {
        if (oldNext != null) {
            // reset for new parent
            this.context.setCurrentNode(oldNext);
        } else if (newNext != null) {
            this.context.setCurrentNode(newNext);
        } else {
            this.context.setParentNode(parent);
        }
    } else if (element == oldNext) {
        if (firstElement != null) {
            this.context.setParentNode(firstElement);
        } else if (first != null) {
            this.context.setCurrentNode(first);
        } else if (startElement != null) {
            this.context.setParentNode(startElement);
        } else {
            this.context.setCurrentNode(newNext);
        }
    }
// removeImplicitElement(elementParent);
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 48 with IJSONNode

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

the class JSONCompletionProposalComputer method collectProposalsFromSchema.

/**
 * Collect completion proposals from JSON Schema.
 *
 * @param contentAssistRequest
 * @param context
 */
private void collectProposalsFromSchema(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
    try {
        IJSONNode node = contentAssistRequest.getNode();
        IJSONSchemaDocument schemaDocument = JSONCorePlugin.getDefault().getSchemaDocument(node);
        if (schemaDocument != null) {
            String matchString = contentAssistRequest.getMatchString();
            if (matchString == null) {
                // $NON-NLS-1$
                matchString = "";
            }
            if ((matchString.length() > 0) && (matchString.startsWith(QUOTE))) {
                matchString = matchString.substring(1);
            }
            // Loop for each properties of the JSON Schema.
            IJSONPath path = node.getPath();
            if (node instanceof IJSONPair) {
                IJSONSchemaProperty thisProperty = schemaDocument.getProperty(path);
                ITextRegion region = contentAssistRequest.getRegion();
                boolean isValue = isPairValue(context, node);
                if (thisProperty != null && isValue) {
                    if (thisProperty.getFirstType() == JSONSchemaType.Boolean) {
                        if (beginsWith(FALSE, matchString.trim())) {
                            addStringProposal(contentAssistRequest, FALSE, false);
                        }
                        if (beginsWith(TRUE, matchString.trim())) {
                            addStringProposal(contentAssistRequest, TRUE, false);
                        }
                        return;
                    }
                    if (thisProperty.getFirstType() == JSONSchemaType.String) {
                        if (thisProperty.getEnumList() != null && thisProperty.getEnumList().size() > 0) {
                            for (String prop : thisProperty.getEnumList()) {
                                boolean showProperty = beginsWith(prop, matchString.trim());
                                if (showProperty) {
                                    addStringProposal(contentAssistRequest, prop, !(region.getType() == JSONRegionContexts.JSON_VALUE_STRING));
                                }
                            }
                        } else {
                            if (thisProperty.getDefaultValue() != null) {
                                boolean showProperty = beginsWith(thisProperty.getDefaultValue(), matchString.trim());
                                if (showProperty) {
                                    addStringProposal(contentAssistRequest, thisProperty.getDefaultValue(), !(region.getType() == JSONRegionContexts.JSON_VALUE_STRING));
                                }
                            }
                        }
                        return;
                    }
                }
            }
            if (!(node instanceof IJSONObject && node.getOwnerPairNode() != null)) {
                if (path.getSegments().length > 0) {
                    String[] segments = new String[path.getSegments().length - 1];
                    System.arraycopy(path.getSegments(), 0, segments, 0, path.getSegments().length - 1);
                    path = new JSONPath(segments);
                }
            }
            IJSONSchemaProperty parentProperty = schemaDocument.getProperty(path);
            Set<String> existing = new HashSet<String>();
            boolean addComma = false;
            if (node instanceof IJSONObject) {
                addExisting(existing, node);
                addComma = addComma(context, node);
            } else if (node instanceof IJSONPair && node.getParentNode() instanceof IJSONObject) {
                addExisting(existing, node.getParentNode());
            }
            if (parentProperty != null) {
                for (IJSONSchemaProperty property : parentProperty.getPropertyValues()) {
                    boolean showProperty = !existing.contains(property.getName()) && beginsWith(property.getName(), matchString.trim());
                    if (showProperty) {
                        String replacementString;
                        if (node instanceof IJSONPair) {
                            replacementString = property.getName();
                        } else {
                            replacementString = ContentAssistHelper.getRequiredName(node, property);
                            if (addComma) {
                                replacementString = replacementString + ",";
                            }
                        }
                        String additionalProposalInfo = property.getDescription();
                        Image icon = JSONEditorPluginImageHelper.getInstance().getImage(property.getFirstType());
                        String displayString = property.getName();
                        JSONKeyCompletionProposal proposal = new JSONKeyCompletionProposal(replacementString, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), replacementString.length() - 2, icon, displayString, null, additionalProposalInfo, JSONRelevanceConstants.R_OBJECT_KEY);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            }
        }
    } catch (IOException e) {
        Logger.logException(e);
    }
}
Also used : IJSONSchemaProperty(org.eclipse.json.schema.IJSONSchemaProperty) JSONKeyCompletionProposal(org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal) IJSONPath(org.eclipse.json.jsonpath.IJSONPath) IOException(java.io.IOException) Image(org.eclipse.swt.graphics.Image) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) IJSONPath(org.eclipse.json.jsonpath.IJSONPath) JSONPath(org.eclipse.json.jsonpath.JSONPath) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONSchemaDocument(org.eclipse.json.schema.IJSONSchemaDocument) HashSet(java.util.HashSet)

Example 49 with IJSONNode

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

the class JSONCompletionProposalComputer method addComma.

private boolean addComma(CompletionProposalInvocationContext context, IJSONNode node) {
    IJSONNode child = node.getFirstChild();
    int documentPosition = context.getInvocationOffset();
    while (child != null) {
        if (documentPosition > child.getStartOffset()) {
            child = child.getNextSibling();
        } else {
            break;
        }
    }
    return child != null;
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 50 with IJSONNode

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

the class JFaceNodeAdapter method getChildren.

public Object[] getChildren(Object object) {
    // (pa) 20021217
    // cmvc defect 235554
    // performance enhancement: using child.getNextSibling() rather than
    // nodeList(item) for O(n) vs. O(n*n)
    // 
    ArrayList v = new ArrayList();
    if (object instanceof IJSONNode) {
        IJSONNode node = (IJSONNode) object;
        if (node.getNodeType() == IJSONNode.PAIR_NODE) {
            node = ((IJSONPair) node).getValue();
        }
        if (node != null) {
            // } else {
            for (IJSONNode child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
                IJSONNode n = child;
                // if (n.getNodeType() != Node.TEXT_NODE) {
                v.add(n);
            // }
            }
        }
    // }
    }
    return v.toArray();
}
Also used : ArrayList(java.util.ArrayList) 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