package jetbrains.mps.scope;
/*Generated by MPS */
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.ArrayList;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
public abstract class SimpleRoleScope extends Scope {
private final SNode myNode;
private final SContainmentLink myLink;
private final SAbstractConcept concept;
public SimpleRoleScope(SNode node, SContainmentLink link, SAbstractConcept concept) {
this.myNode = node;
this.myLink = link;
this.concept = concept;
}
public SimpleRoleScope(SNode node, SContainmentLink link) {
this.myNode = node;
this.myLink = link;
this.concept = null;
}
@Override
public SNode resolve(SNode contextNode, @NotNull String refText) {
SNode result = null;
for (SNode n : SNodeOperations.getChildren(myNode, myLink)) {
if (this.concept != null && !(n.getConcept().isSubConceptOf(concept))) {
continue;
}
if (refText.equals(getName(n))) {
if (result != null) {
return null;
}
result = n;
}
}
return result;
}
@Override
public Iterable<SNode> getAvailableElements(@Nullable String prefix) {
List<SNode> result = new ArrayList<SNode>();
for (SNode n : SNodeOperations.getChildren(myNode, myLink)) {
if (this.concept != null && !(n.getConcept().isSubConceptOf(concept))) {
continue;
}
String name = getName(n);
if (prefix == null || name.startsWith(prefix)) {
ListSequence.fromList(result).addElement(n);
}
}
return result;
}
@Override
public String getReferenceText(SNode contextNode, SNode node) {
if (node == null || SNodeOperations.getParent(node) != myNode) {
return null;
}
if (this.concept != null && !(node.getConcept().isSubConceptOf(concept))) {
return null;
}
String result = getName(node);
for (SNode n : SNodeOperations.getChildren(myNode, myLink)) {
if (n == node) {
continue;
}
if (this.concept != null && !(n.getConcept().isSubConceptOf(concept))) {
continue;
}
String name = getName(n);
if (name.equals(result)) {
// ambiguity
return null;
}
}
return result;
}
public abstract String getName(SNode child);
public static SimpleRoleScope forNamedElements(SNode node, SContainmentLink linkDeclaration) {
return new SimpleRoleScope(node, linkDeclaration) {
@Override
public String getName(SNode child) {
return SPropertyOperations.getString(SNodeOperations.cast(child, MetaAdapterFactory.getInterfaceConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, "jetbrains.mps.lang.core.structure.INamedConcept")), MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name"));
}
};
}
}