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 org.jetbrains.mps.openapi.language.SAbstractConcept;
import org.jetbrains.annotations.Nullable;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.internal.collections.runtime.ITranslator2;
import org.jetbrains.mps.openapi.model.SNodeUtil;
import org.jetbrains.mps.util.Condition;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
public abstract class DescendantsScope extends Scope {
private SNode node;
private SContainmentLink link;
private SAbstractConcept concept;
public DescendantsScope(SNode node, SContainmentLink link, SAbstractConcept concept) {
this.node = node;
this.link = link;
this.concept = concept;
}
public abstract String getName(SNode child);
@Override
public Iterable<SNode> getAvailableElements(@Nullable final String prefix) {
Iterable<SNode> seq = ListSequence.fromList(SNodeOperations.getChildren(node, link)).translate(new ITranslator2<SNode, SNode>() {
public Iterable<SNode> translate(SNode it) {
return SNodeUtil.getDescendants(it, new Condition<SNode>() {
public boolean met(SNode n) {
return SNodeOperations.isInstanceOf(n, SNodeOperations.asSConcept(concept));
}
}, true);
}
});
if (prefix == null || prefix.isEmpty()) {
return seq;
}
return Sequence.fromIterable(seq).where(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
String name = getName(it);
return name != null && name.startsWith(prefix);
}
});
}
@Nullable
@Override
public SNode resolve(SNode contextNode, @NotNull String refText) {
SNode result = null;
for (SNode n : Sequence.fromIterable(getAvailableElements(null))) {
String name = getName(n);
if (refText.equals(name)) {
if (result == null) {
result = n;
} else {
return null;
}
}
}
return result;
}
@Nullable
@Override
public String getReferenceText(SNode contextNode, @NotNull SNode node) {
if (!(SNodeOperations.isInstanceOf(node, SNodeOperations.asSConcept(concept)))) {
return null;
}
String result = getName(node);
for (SNode n : Sequence.fromIterable(getAvailableElements(null))) {
if (n == node) {
continue;
}
String name = getName(n);
if (name.equals(result)) {
// ambiguity
return null;
}
}
return result;
}
public static DescendantsScope forNamedElements(SNode node, SContainmentLink link, SAbstractConcept concept) {
return new DescendantsScope(node, link, concept) {
@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"));
}
};
}
}