package jetbrains.mps.scope;
/*Generated by MPS */
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.annotations.Nullable;
import jetbrains.mps.internal.collections.runtime.Sequence;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.smodel.behaviour.BHReflection;
import jetbrains.mps.core.aspects.behaviour.SMethodTrimmedId;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
public abstract class Scope {
public Scope() {
}
/**
* Returns all available elements in the scope.
*
* @param prefix (if not null) filters out elements whose reference text doesn't start with prefix
* @return list of nodes in the scope
*/
public abstract Iterable<SNode> getAvailableElements(@Nullable String prefix);
/**
* Returns <tt>true</tt> if this scope contains the specified element.
*
* Invariant: contains(node) == getAvailableElements(null).contains(node)
*
* @param node element to check presence for
* @return <tt>true</tt> if this scope contains the specified element
*/
public boolean contains(SNode node) {
return Sequence.fromIterable(getAvailableElements(null)).contains(node);
}
/**
* Resolves element by reference text.
*
* Invariant: getReferenceText(contextNode, resolve(contextNode, refText)) == refText
*
* @param contextNode source node for the reference, or its nearest parent node (if source node is unavailable)
* @param refText reference text
* @return resolved element when reference text unambiguously identifies element, null otherwise
*/
@Nullable
public abstract SNode resolve(SNode contextNode, @NotNull String refText);
/**
* Creates textual reference for scope element. If element has no textual representation
* for the reference, returns null.
*
* Invariant: resolve(contextNode, getReferenceText(contextNode, node)) == node
*
* @param contextNode source node for the reference, or its nearest parent node (if source node is unavailable)
* @param node element from the current scope (contains(node) == true)
* @return reference text for the node element in the current scope
*/
@Nullable
public abstract String getReferenceText(SNode contextNode, @NotNull SNode node);
/**
* Get scope for existing node.
*/
public static Scope getScope(SNode node, SNode fromChild, SAbstractConcept kind) {
SNode curr = node;
SNode prev = fromChild;
while (curr != null) {
if (SNodeOperations.isInstanceOf(curr, MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x33d23ee961a0cbf3L, "jetbrains.mps.lang.core.structure.ScopeProvider"))) {
Scope scope = ((Scope) BHReflection.invoke(SNodeOperations.cast(curr, MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x33d23ee961a0cbf3L, "jetbrains.mps.lang.core.structure.ScopeProvider")), SMethodTrimmedId.create("getScope", null, "52_Geb4QDV$"), kind, prev));
if (scope != null) {
return scope;
}
}
prev = curr;
curr = parent(curr);
}
return null;
}
/**
* Get scope for smart reference, when node doesn't exist yet
*/
public static Scope getScope(SNode node, SContainmentLink link, int index, SAbstractConcept kind) {
if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x33d23ee961a0cbf3L, "jetbrains.mps.lang.core.structure.ScopeProvider"))) {
Scope scope = ((Scope) BHReflection.invoke(SNodeOperations.cast(node, MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x33d23ee961a0cbf3L, "jetbrains.mps.lang.core.structure.ScopeProvider")), SMethodTrimmedId.create("getScope", null, "52_Geb4QFgX"), kind, link, ((int) index)));
if (scope != null) {
return scope;
}
}
return getScope(parent(node), node, kind);
}
public static SNode parent(SNode n) {
if (SNodeOperations.isAttribute(n)) {
if (SNodeOperations.isInstanceOf(n, MetaAdapterFactory.getConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x2eb1ad060897da54L, "jetbrains.mps.lang.core.structure.NodeAttribute"))) {
SNode next = SNodeOperations.getPrevSibling(n);
while (next != null) {
if (SNodeOperations.isInstanceOf(next, MetaAdapterFactory.getConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x2eb1ad060897da54L, "jetbrains.mps.lang.core.structure.NodeAttribute"))) {
return next;
}
next = SNodeOperations.getPrevSibling(next);
}
n = SNodeOperations.getParent(n);
return (n == null ? null : SNodeOperations.getParent(n));
}
return SNodeOperations.getParent(n);
}
SNode lastAttr = ListSequence.fromList(SLinkOperations.getChildren(SNodeOperations.cast(n, MetaAdapterFactory.getConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x10802efe25aL, "jetbrains.mps.lang.core.structure.BaseConcept")), MetaAdapterFactory.getContainmentLink(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x10802efe25aL, 0x47bf8397520e5942L, "smodelAttribute"))).where(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
return SNodeOperations.isInstanceOf(it, MetaAdapterFactory.getConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x2eb1ad060897da54L, "jetbrains.mps.lang.core.structure.NodeAttribute"));
}
}).last();
if ((lastAttr != null)) {
return lastAttr;
}
return SNodeOperations.getParent(n);
}
}