Search in sources :

Example 1 with JSONKeyCompletionProposal

use of org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal in project webtools.sourceediting by eclipse.

the class BowerDependencyCompletionProposalCollector method addProposals.

protected void addProposals(JsonValue json, ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context, TargetType target) {
    if (json.isArray()) {
        // Ex :
        // [{"name":"angular-mocks","url":"git://github.com/angular/bower-angular-mocks.git"},
        // {"name":"angular-moment","url":"git://github.com/urish/angular-moment.git"}]
        String dependency = null;
        String replacementString = null;
        JsonArray values = (JsonArray) json;
        for (JsonValue value : values) {
            if (value.isObject()) {
                dependency = ((JsonObject) value).get("name").asString();
                replacementString = ContentAssistHelper.getRequiredName(dependency, JSONSchemaType.String);
                Image icon = BowerEditorPluginImageHelper.getInstance().getImage(BowerEditorPluginImages.IMG_OBJ_BOWER);
                JSONKeyCompletionProposal proposal = new JSONKeyCompletionProposal(replacementString, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), replacementString.length() - 2, icon, dependency, null, null, JSONRelevanceConstants.R_OBJECT_KEY);
                contentAssistRequest.addProposal(proposal);
            }
        }
    }
// System.err.println(json);
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) JSONKeyCompletionProposal(org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject) Image(org.eclipse.swt.graphics.Image)

Example 2 with JSONKeyCompletionProposal

use of org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal in project webtools.sourceediting by eclipse.

the class JSONCompletionProposalComputer method addStringProposal.

private void addStringProposal(ContentAssistRequest contentAssistRequest, String replacementString, boolean addQuote) {
    String additionalProposalInfo = null;
    Image icon = null;
    String displayString = replacementString;
    if (addQuote) {
        replacementString = QUOTE + replacementString + QUOTE;
    }
    String matchString = contentAssistRequest.getMatchString();
    if (matchString != null) {
        // $NON-NLS-1$
        matchString = matchString.replaceAll(QUOTE, "");
    }
    JSONKeyCompletionProposal proposal = new JSONKeyCompletionProposal(replacementString, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), replacementString.length() - 2, icon, displayString, null, additionalProposalInfo, JSONRelevanceConstants.R_OBJECT_KEY);
    contentAssistRequest.addProposal(proposal);
}
Also used : JSONKeyCompletionProposal(org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal) Image(org.eclipse.swt.graphics.Image)

Example 3 with JSONKeyCompletionProposal

use of org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal 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 4 with JSONKeyCompletionProposal

use of org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal in project webtools.sourceediting by eclipse.

the class NPMDependencyCompletionProposalCollector method addProposals.

protected void addProposals(JsonValue json, ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context, TargetType target) {
    if (json.isArray()) {
        // Ex :
        // [{"value":"express"},{"value":"lodash"},{"value":"gulp"}]
        String dependency = null;
        String replacementString = null;
        JsonArray values = (JsonArray) json;
        for (JsonValue value : values) {
            if (value.isObject()) {
                dependency = ((JsonObject) value).get("value").asString();
                replacementString = ContentAssistHelper.getRequiredName(dependency, JSONSchemaType.String);
                Image icon = NPMEditorPluginImageHelper.getInstance().getImage(NPMEditorPluginImages.IMG_OBJ_NPM);
                JSONKeyCompletionProposal proposal = new JSONKeyCompletionProposal(replacementString, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), replacementString.length() - 2, icon, dependency, null, null, JSONRelevanceConstants.R_OBJECT_KEY);
                contentAssistRequest.addProposal(proposal);
            }
        }
    }
// System.err.println(json);
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) JSONKeyCompletionProposal(org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject) Image(org.eclipse.swt.graphics.Image)

Aggregations

Image (org.eclipse.swt.graphics.Image)4 JSONKeyCompletionProposal (org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal)4 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 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 IJSONPath (org.eclipse.json.jsonpath.IJSONPath)1 JSONPath (org.eclipse.json.jsonpath.JSONPath)1 IJSONSchemaDocument (org.eclipse.json.schema.IJSONSchemaDocument)1 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)1 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)1 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)1 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)1 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)1