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;
}
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);
}
}
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));
}
}
}
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);
}
}
}
}
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());
}
Aggregations