Search in sources :

Example 11 with IJSONObject

use of org.eclipse.wst.json.core.document.IJSONObject 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 12 with IJSONObject

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

the class JSONModelNotifierImpl method notifyDeferred.

/**
 */
private void notifyDeferred() {
    if (this.fEvents == null)
        return;
    if (this.flushing)
        return;
    // force notification
    this.flushing = true;
    int count = this.fEvents.size();
    if (!doingNewModel && fOptimizeDeferred) {
        Map notifyEvents = new HashMap();
        for (int i = 0; i < count; i++) {
            NotifyEvent event = (NotifyEvent) this.fEvents.get(i);
            if (event == null)
                // error
                continue;
            event.index = i;
            if (event.type == INodeNotifier.REMOVE) {
                addToMap(event.oldValue, event, notifyEvents);
            }
            if (event.type == INodeNotifier.ADD) {
                addToMap(event.newValue, event, notifyEvents);
            }
        }
        Iterator it = notifyEvents.values().iterator();
        while (it.hasNext()) {
            NotifyEvent[] es = (NotifyEvent[]) it.next();
            for (int i = 0; i < es.length - 1; i++) {
                NotifyEvent event = es[i];
                if (es[i].discarded)
                    continue;
                NotifyEvent next = es[i + 1];
                if (es[i].type == INodeNotifier.ADD && next.type == INodeNotifier.REMOVE) {
                    // Added then removed later, discard both
                    event.discarded = true;
                    next.discarded = true;
                    if (Debug.debugNotifyDeferred) {
                        event.reason = event.reason + ADDED_THEN_REMOVED + "(see " + next.index + // $NON-NLS-1$ //$NON-NLS-2$
                        ")";
                        next.reason = next.reason + ADDED_THEN_REMOVED + "(see " + event.index + // $NON-NLS-1$ //$NON-NLS-2$
                        ")";
                    }
                }
            }
        }
        for (int i = 0; i < count; i++) {
            NotifyEvent event = (NotifyEvent) this.fEvents.get(i);
            if (event == null)
                // error
                continue;
            if (event.discarded)
                continue;
            if (event.notifier != null && fOptimizeDeferredAccordingToParentAdded) {
                if (event.type == INodeNotifier.ADD) {
                    NotifyEvent[] es = (NotifyEvent[]) notifyEvents.get(event.notifier);
                    if (es != null)
                        for (int p = 0; p < es.length && es[p].index < event.index; p++) {
                            NotifyEvent prev = es[p];
                            if (prev.type == INodeNotifier.REMOVE && prev.oldValue == event.notifier) {
                                // parent is reparented, do not discard
                                if (Debug.debugNotifyDeferred) {
                                    event.reason = event.reason + PARENT_IS_REPARENTED + "(see " + prev.index + // $NON-NLS-1$ //$NON-NLS-2$
                                    ")";
                                }
                                break;
                            } else if (prev.type == INodeNotifier.ADD && prev.newValue == event.notifier) {
                                // parent has been added, discard this
                                event.discarded = true;
                                if (Debug.debugNotifyDeferred) {
                                    event.reason = event.reason + PARENT_IS_ADDED + "(see " + prev.index + // $NON-NLS-1$ //$NON-NLS-2$
                                    ")";
                                }
                                break;
                            }
                        }
                }
            }
            if (event.discarded)
                continue;
            if (event.notifier != null && fOptimizeDeferredAccordingToParentRemoved) {
                if (event.type == INodeNotifier.REMOVE) {
                    NotifyEvent[] es = (NotifyEvent[]) notifyEvents.get(event.notifier);
                    if (es != null)
                        for (int n = 0; n < es.length; n++) {
                            NotifyEvent next = es[n];
                            if (next.index > event.index && next.type == INodeNotifier.REMOVE) {
                                if (next.oldValue == event.notifier) {
                                    // parent will be removed, discard this
                                    event.discarded = true;
                                    if (Debug.debugNotifyDeferred) {
                                        event.reason = event.reason + PARENT_IS_REMOVED_TOO + "(see " + next.index + // $NON-NLS-1$ //$NON-NLS-2$
                                        ")";
                                    }
                                    break;
                                }
                            }
                        }
                }
            }
            if (event.discarded)
                continue;
        }
    }
    for (int i = 0; i < count; i++) {
        NotifyEvent event = (NotifyEvent) this.fEvents.get(i);
        if (event == null)
            // error
            continue;
        if (event.discarded)
            continue;
        notify(event.notifier, event.type, event.changedFeature, event.oldValue, event.newValue, event.pos);
    }
    if (Debug.debugNotifyDeferred) {
        for (int l = 0; l < count; l++) {
            NotifyEvent event = (NotifyEvent) this.fEvents.get(l);
            Object o = null;
            String t = null;
            if (event.type == INodeNotifier.ADD) {
                o = event.newValue;
                // $NON-NLS-1$
                t = " + ";
            } else if (event.type == INodeNotifier.REMOVE) {
                o = event.oldValue;
                // $NON-NLS-1$
                t = " - ";
            }
            if (o instanceof IJSONObject) {
                String p = ((IJSONNode) event.notifier).getNodeName();
                String c = ((IJSONNode) o).getNodeName();
                // $NON-NLS-1$ //$NON-NLS-2$
                String d = (event.discarded ? "! " : "  ");
                System.out.println(d + p + t + c);
            }
        }
    }
    this.flushing = false;
    this.fEvents = null;
}
Also used : HashMap(java.util.HashMap) Iterator(java.util.Iterator) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) Map(java.util.Map) HashMap(java.util.HashMap) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Aggregations

IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)12 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)8 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)4 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)4 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)3 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)3 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 IJSONPath (org.eclipse.json.jsonpath.IJSONPath)1 JSONPath (org.eclipse.json.jsonpath.JSONPath)1 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)1 IJSONSchemaDocument (org.eclipse.json.schema.IJSONSchemaDocument)1 Image (org.eclipse.swt.graphics.Image)1 IJSONCleanupStrategy (org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy)1