Search in sources :

Example 1 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class CompletionUtils method getJsonPathNames.

/**
 * Return the property names on the path towards the current model.
 */
public static List<String> getJsonPathNames(EObject model) {
    List<String> namePath = new LinkedList<>();
    EObject eobj = model;
    while (!(eobj instanceof JSONDocument)) {
        if (eobj instanceof NameValuePair) {
            NameValuePair nvp = (NameValuePair) eobj;
            namePath.add(0, nvp.getName());
        }
        eobj = eobj.eContainer();
    }
    return namePath;
}
Also used : NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) EObject(org.eclipse.emf.ecore.EObject) JSONDocument(org.eclipse.n4js.json.JSON.JSONDocument) LinkedList(java.util.LinkedList)

Example 2 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class JSONIdeContentProposalProvider method _createProposals.

@Override
protected void _createProposals(Assignment assignment, ContentAssistContext context, IIdeContentProposalAcceptor acceptor) {
    AbstractElement terminal = assignment.getTerminal();
    if (terminal instanceof CrossReference) {
        createProposals(terminal, context, acceptor);
    }
    if (assignment == grammarAccess.getNameValuePairAccess().getNameAssignment_0()) {
        List<String> namePath = CompletionUtils.getJsonPathNames(context.getCurrentModel());
        proposeLocalPackages(context, acceptor, namePath);
    }
    if (assignment == grammarAccess.getNameValuePairAccess().getValueAssignment_2() && context.getCurrentModel() instanceof NameValuePair) {
        List<String> namePath = CompletionUtils.getJsonPathNames(context.getCurrentModel());
        proposeVersions(context, acceptor, namePath);
        proposeProjectTypes(context, acceptor, namePath);
    }
}
Also used : NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) AbstractElement(org.eclipse.xtext.AbstractElement) CrossReference(org.eclipse.xtext.CrossReference)

Example 3 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class JSONIdeContentProposalProvider method proposeVersions.

private void proposeVersions(ContentAssistContext context, IIdeContentProposalAcceptor acceptor, List<String> namePath) {
    if (namePath.size() >= 2) {
        // somewhat poor heuristic: propose all projects that are known in the current workspace
        String devOrDep = namePath.get(namePath.size() - 2);
        if (PackageJsonProperties.DEPENDENCIES.name.equals(devOrDep) || PackageJsonProperties.DEV_DEPENDENCIES.name.equals(devOrDep)) {
            NameValuePair pair = (NameValuePair) context.getCurrentModel();
            N4JSProjectConfigSnapshot project = workspaceAccess.findProjectByName(context.getResource(), pair.getName());
            if (project != null) {
                VersionNumber version = project.getVersion();
                ContentAssistEntry versionEntry = getProposalCreator().createProposal('"' + version.toString() + '"', context, ContentAssistEntry.KIND_VALUE, null);
                acceptor.accept(versionEntry, getProposalPriorities().getDefaultPriority(versionEntry));
            }
            ContentAssistEntry wildcard = getProposalCreator().createProposal("\"*\"", context, ContentAssistEntry.KIND_VALUE, null);
            acceptor.accept(wildcard, getProposalPriorities().getDefaultPriority(wildcard));
        }
    }
}
Also used : NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) ContentAssistEntry(org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) VersionNumber(org.eclipse.n4js.semver.Semver.VersionNumber)

Example 4 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class JSONHierarchicalSymbolService method getSymbols.

private void getSymbols(EObject value, Consumer<? super DocumentSymbol> acceptor, CancelIndicator ci) {
    operationCanceledManager.checkCanceled(ci);
    if (value instanceof NameValuePair) {
        NameValuePair nameValuePair = (NameValuePair) value;
        DocumentSymbol symbol = symbolMapper.toDocumentSymbol(nameValuePair);
        if (isValid(symbol)) {
            acceptor.accept(symbol);
            JSONValue pairValue = ((NameValuePair) value).getValue();
            if (pairValue != null && pairValue.isContainer()) {
                List<DocumentSymbol> children = symbol.getChildren();
                getSymbols(pairValue, children::add, ci);
            }
        }
    } else if (value instanceof JSONValue) {
        JSONValue casted = (JSONValue) value;
        if (casted.isContainer()) {
            for (EObject child : ((JSONValue) value).getChildren()) {
                getSymbols(child, acceptor, ci);
            }
        } else {
            DocumentSymbol symbol = symbolMapper.toDocumentSymbol(casted);
            if (isValid(symbol)) {
                acceptor.accept(symbol);
            }
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) EObject(org.eclipse.emf.ecore.EObject) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol)

Example 5 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class JSONModelUtils method merge.

/**
 * Moves or copies all {@link NameValuePair}s from object 'source' to object 'target', replacing any
 * {@code NameValuePair}s of same name present in 'target'. The order of properties is preserved.
 *
 * @param target
 *            target object; will be changed in place.
 * @param source
 *            source object; won't be changed iff 'copy' is set to <code>true</code>.
 * @param copy
 *            tells if {@link NameValuePair}s should be copied over, instead of being moved.
 * @param recursive
 *            tells if a recursive merge is to be performed in case an object value in 'target' is overwritten by an
 *            object value in 'source' (i.e. in case of nested objects on both sides).
 */
public static void merge(JSONObject target, JSONObject source, boolean copy, boolean recursive) {
    final Map<String, NameValuePair> targetPairsPerName = JSONModelUtils.getPropertiesAsMap(target, true);
    for (NameValuePair sourcePair : source.getNameValuePairs()) {
        final String sourcePairName = sourcePair.getName();
        final JSONValue sourcePairValue = sourcePair.getValue();
        if (recursive && sourcePairValue instanceof JSONObject) {
            final NameValuePair targetPair = targetPairsPerName.get(sourcePairName);
            final JSONValue targetPairValue = targetPair != null ? targetPair.getValue() : null;
            if (targetPairValue instanceof JSONObject) {
                merge((JSONObject) targetPairValue, (JSONObject) sourcePairValue, copy, recursive);
                continue;
            }
        }
        targetPairsPerName.put(sourcePairName, copy ? EcoreUtil.copy(sourcePair) : sourcePair);
    }
    final List<NameValuePair> targetList = target.getNameValuePairs();
    targetList.clear();
    targetList.addAll(targetPairsPerName.values());
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Aggregations

NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)28 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)17 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)17 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)7 EObject (org.eclipse.emf.ecore.EObject)6 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)4 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 URI (org.eclipse.emf.common.util.URI)2 JSONBooleanLiteral (org.eclipse.n4js.json.JSON.JSONBooleanLiteral)2 JSONNumericLiteral (org.eclipse.n4js.json.JSON.JSONNumericLiteral)2 SourceContainerType (org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)2 NPMVersionRequirement (org.eclipse.n4js.semver.Semver.NPMVersionRequirement)2 INode (org.eclipse.xtext.nodemodel.INode)2 Check (org.eclipse.xtext.validation.Check)2 Optional (com.google.common.base.Optional)1