package jetbrains.mps.build.util;
/*Generated by MPS */
import jetbrains.mps.scope.Scope;
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import jetbrains.mps.scope.SimpleRoleScope;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import jetbrains.mps.scope.FilteringScope;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.internal.collections.runtime.ISelector;
import jetbrains.mps.lang.core.behavior.ScopeProvider__BehaviorDescriptor;
import jetbrains.mps.scope.DelegatingScope;
import org.jetbrains.annotations.Nullable;
import jetbrains.mps.generator.TransientModelsModule;
import org.jetbrains.mps.openapi.module.SModule;
import jetbrains.mps.scope.ModelPlusImportedScope;
import jetbrains.mps.build.behavior.BuildLayout_Node__BehaviorDescriptor;
import java.util.List;
import java.util.ArrayList;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.build.behavior.BuildLayout_PathElement__BehaviorDescriptor;
public class ScopeUtil {
public ScopeUtil() {
}
public static Scope simpleRoleScope(SNode node, SContainmentLink link) {
return new SimpleRoleScope(node, link) {
@Override
public String getName(SNode child) {
if (!(SNodeOperations.isInstanceOf(child, MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, "jetbrains.mps.lang.core.structure.INamedConcept")))) {
return child.getPresentation();
}
return SPropertyOperations.getString(SNodeOperations.cast(child, MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, "jetbrains.mps.lang.core.structure.INamedConcept")), MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name"));
}
};
}
public static Scope where(Scope scope, final _FunctionTypes._return_P1_E0<? extends Boolean, ? super SNode> filter) {
return new FilteringScope(scope) {
@Override
public boolean isExcluded(SNode node) {
return !(filter.invoke(node));
}
};
}
public static Iterable<Scope> imported(Iterable<SNode> importDeclarations, final SAbstractConcept concept, final SNode child) {
return Sequence.fromIterable(importDeclarations).where(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
// searching for smart references
return ListSequence.fromList(SNodeOperations.getReferences(it)).count() == 1 && SNodeOperations.isInstanceOf(SLinkOperations.getTargetNode(ListSequence.fromList(SNodeOperations.getReferences(it)).first()), MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x33d23ee961a0cbf3L, "jetbrains.mps.lang.core.structure.ScopeProvider"));
}
}).select(new ISelector<SNode, SNode>() {
public SNode select(SNode it) {
// that references providers
return SNodeOperations.cast(SLinkOperations.getTargetNode(ListSequence.fromList(SNodeOperations.getReferences(it)).first()), MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x33d23ee961a0cbf3L, "jetbrains.mps.lang.core.structure.ScopeProvider"));
}
}).where(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
// check for cycles
return !(ListSequence.fromList(SNodeOperations.getNodeDescendants(it, null, false, new SAbstractConcept[]{})).contains(child));
}
}).select(new ISelector<SNode, Scope>() {
public Scope select(SNode it) {
return (Scope) ScopeProvider__BehaviorDescriptor.getScope_id52_Geb4QDV$.invoke(it, concept, child);
}
});
}
public static Scope unique(Scope scope) {
return new DelegatingScope(scope) {
@Override
public Iterable<SNode> getAvailableElements(@Nullable String prefix) {
return Sequence.fromIterable(super.getAvailableElements(prefix)).distinct();
}
};
}
public static Scope getVisibleArtifactsScope(SNode project, boolean includeLayoutRoots) {
if (SNodeOperations.getModel(project).getModule() instanceof TransientModelsModule) {
SModule transientModule = SNodeOperations.getModel(project).getModule();
return new ModelPlusImportedScope(SNodeOperations.getModel(project), false, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x668c6cfbafac4c85L, "jetbrains.mps.build.structure.BuildLayout_Node"));
}
return new ScopeUtil.VisibleArtifactsScope(VisibleArtifacts.createFor(project), includeLayoutRoots);
}
public static Scope getVisibleJarsScope(SNode project) {
if (SNodeOperations.getModel(project).getModule() instanceof TransientModelsModule) {
SModule transientModule = SNodeOperations.getModel(project).getModule();
return new ModelPlusImportedScope(SNodeOperations.getModel(project), false, MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x4ddcec86afa57ad2L, "jetbrains.mps.build.structure.BuildSource_SingleFile"));
}
return new ScopeUtil.VisibleJarsScope(VisibleArtifacts.createFor(project));
}
public static Scope getVisibleJarFoldersScope(SNode project) {
if (SNodeOperations.getModel(project).getModule() instanceof TransientModelsModule) {
SModule transientModule = SNodeOperations.getModel(project).getModule();
return new ModelPlusImportedScope(SNodeOperations.getModel(project), false, MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x4ddcec86afb659d7L, "jetbrains.mps.build.structure.BuildSource_SingleFolder"));
}
return new ScopeUtil.VisibleJarFoldersScope(VisibleArtifacts.createFor(project));
}
private static class VisibleArtifactsScope extends Scope {
private VisibleArtifacts artifacts;
private boolean includeLayoutRoots;
public VisibleArtifactsScope(VisibleArtifacts artifacts, boolean includeLayoutRoots) {
this.artifacts = artifacts;
this.includeLayoutRoots = includeLayoutRoots;
}
private Iterable<SNode> getAllNodes() {
Iterable<SNode> seq = Sequence.fromIterable(artifacts.getArtifacts()).where(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
return (boolean) BuildLayout_Node__BehaviorDescriptor.isFile_id1bWeed$oPZ2.invoke(it) || (boolean) BuildLayout_Node__BehaviorDescriptor.isFolder_id1bWeed$oPYW.invoke(it);
}
});
if (includeLayoutRoots) {
seq = Sequence.fromIterable(seq).concat(Sequence.fromIterable(artifacts.getLayouts()));
}
return seq;
}
@Override
public Iterable<SNode> getAvailableElements(@Nullable String prefix) {
if ((prefix == null || prefix.length() == 0)) {
return Sequence.fromIterable(getAllNodes()).toListSequence();
}
List<SNode> result = new ArrayList<SNode>();
for (SNode n : getAllNodes()) {
String name = getName(n);
if (prefix == null || name.startsWith(prefix)) {
ListSequence.fromList(result).addElement(n);
}
}
return result;
}
@Override
public SNode resolve(SNode contextNode, @NotNull String refText) {
SNode result = null;
for (SNode n : getAllNodes()) {
String name = getName(n);
if (refText.equals(name)) {
if (result == null) {
result = n;
} else {
return null;
}
}
}
return result;
}
@Override
public String getReferenceText(SNode contextNode, SNode node) {
if (node == null || !(SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x668c6cfbafac4c85L, "jetbrains.mps.build.structure.BuildLayout_Node")))) {
return null;
}
return getName(SNodeOperations.cast(node, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x668c6cfbafac4c85L, "jetbrains.mps.build.structure.BuildLayout_Node")));
}
private String getName(SNode node) {
StringBuilder sb = new StringBuilder();
appendName(node, sb);
return sb.toString();
}
private void appendName(SNode node, StringBuilder sb) {
SNode parent = artifacts.parent(node);
if ((parent != null)) {
appendName(parent, sb);
}
BuildLayout_PathElement__BehaviorDescriptor.appendName_id1bWeed$ownT.invoke(node, parent, sb);
}
}
public static abstract class TransformingScope extends Scope {
protected Scope wrapped;
public TransformingScope(Scope wrapped) {
this.wrapped = wrapped;
}
@Override
public SNode resolve(SNode contextNode, String refText) {
SNode resolve = wrapped.resolve(contextNode, refText);
return (resolve == null ? null : unwrap(resolve));
}
@Override
public Iterable<SNode> getAvailableElements(@Nullable String prefix) {
return Sequence.fromIterable(wrapped.getAvailableElements(prefix)).select(new ISelector<SNode, SNode>() {
public SNode select(SNode it) {
return unwrap(it);
}
}).toListSequence();
}
@Override
public String getReferenceText(SNode contextNode, SNode node) {
node = wrap(node);
if (node == null) {
return null;
}
return wrapped.getReferenceText(contextNode, node);
}
@Override
public boolean contains(SNode node) {
node = wrap(node);
if (node == null) {
return false;
}
return wrapped.contains(node);
}
public abstract SNode wrap(SNode node);
public abstract SNode unwrap(SNode node);
}
public static class VisibleJarFoldersScope extends ScopeUtil.TransformingScope {
public VisibleJarFoldersScope(VisibleArtifacts artifacts) {
super(new ScopeUtil.VisibleArtifactsScope(artifacts, false));
}
@Override
public SNode wrap(SNode node) {
if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x668c6cfbafac4c85L, "jetbrains.mps.build.structure.BuildLayout_Node"))) {
return SNodeOperations.cast(node, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x668c6cfbafac4c85L, "jetbrains.mps.build.structure.BuildLayout_Node"));
} else if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x1ff930b22643b0ffL, "jetbrains.mps.build.structure.BuildInputSingleFolder")) && SNodeOperations.isInstanceOf(SNodeOperations.getParent(node), MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x7f76698a394d9b91L, "jetbrains.mps.build.structure.BuildLayout_AbstractCopy"))) {
return SNodeOperations.cast(SNodeOperations.getParent(node), MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x668c6cfbafac4c85L, "jetbrains.mps.build.structure.BuildLayout_Node"));
}
return null;
}
@Override
public SNode unwrap(SNode node) {
if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x4ddcec86afb659d7L, "jetbrains.mps.build.structure.BuildSource_SingleFolder"))) {
return SNodeOperations.cast(node, MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x4ddcec86afb659d7L, "jetbrains.mps.build.structure.BuildSource_SingleFolder"));
} else {
SNode copyNode = SNodeOperations.as(node, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x7f76698a394d9b91L, "jetbrains.mps.build.structure.BuildLayout_AbstractCopy"));
if ((copyNode != null) && SNodeOperations.isInstanceOf(SLinkOperations.getTarget(copyNode, MetaAdapterFactory.getContainmentLink(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x7f76698a394d9b91L, 0x48d5d03db92339baL, "fileset")), MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x4ddcec86afb659d7L, "jetbrains.mps.build.structure.BuildSource_SingleFolder"))) {
return SNodeOperations.cast(SLinkOperations.getTarget(copyNode, MetaAdapterFactory.getContainmentLink(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x7f76698a394d9b91L, 0x48d5d03db92339baL, "fileset")), MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x4ddcec86afb659d7L, "jetbrains.mps.build.structure.BuildSource_SingleFolder"));
}
}
return null;
}
}
public static class VisibleJarsScope extends ScopeUtil.TransformingScope {
public VisibleJarsScope(VisibleArtifacts artifacts) {
super(new ScopeUtil.VisibleArtifactsScope(artifacts, false));
}
@Override
public SNode wrap(SNode node) {
if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x668c6cfbafac4c85L, "jetbrains.mps.build.structure.BuildLayout_Node"))) {
return SNodeOperations.cast(node, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x668c6cfbafac4c85L, "jetbrains.mps.build.structure.BuildLayout_Node"));
} else if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x48d5d03db9224596L, "jetbrains.mps.build.structure.BuildInputSingleFile")) && SNodeOperations.isInstanceOf(SNodeOperations.getParent(node), MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x7f76698a394d9b91L, "jetbrains.mps.build.structure.BuildLayout_AbstractCopy"))) {
return SNodeOperations.cast(SNodeOperations.getParent(node), MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x668c6cfbafac4c85L, "jetbrains.mps.build.structure.BuildLayout_Node"));
}
return null;
}
@Override
public SNode unwrap(SNode node) {
if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x4ddcec86afa57ad2L, "jetbrains.mps.build.structure.BuildSource_SingleFile"))) {
return SNodeOperations.cast(node, MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x4ddcec86afa57ad2L, "jetbrains.mps.build.structure.BuildSource_SingleFile"));
} else {
SNode copyNode = SNodeOperations.as(node, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x7f76698a394d9b91L, "jetbrains.mps.build.structure.BuildLayout_AbstractCopy"));
if ((copyNode != null) && SNodeOperations.isInstanceOf(SLinkOperations.getTarget(copyNode, MetaAdapterFactory.getContainmentLink(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x7f76698a394d9b91L, 0x48d5d03db92339baL, "fileset")), MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x4ddcec86afa57ad2L, "jetbrains.mps.build.structure.BuildSource_SingleFile"))) {
return SNodeOperations.cast(SLinkOperations.getTarget(copyNode, MetaAdapterFactory.getContainmentLink(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x7f76698a394d9b91L, 0x48d5d03db92339baL, "fileset")), MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x4ddcec86afa57ad2L, "jetbrains.mps.build.structure.BuildSource_SingleFile"));
}
}
return null;
}
}
}