package typesystemIntegration.languageChecker;
/*Generated by MPS */
import jetbrains.mps.nodeEditor.EditorComponent;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.openapi.editor.cells.EditorCell;
import jetbrains.mps.openapi.editor.EditorContext;
import jetbrains.mps.openapi.editor.cells.SubstituteInfo;
import jetbrains.mps.openapi.editor.cells.SubstituteAction;
import jetbrains.mps.internal.collections.runtime.ListSequence;
public class EditorBasedReferenceResolverUtils {
public static boolean resolveInEditor(EditorComponent editorComponent, SNode sourceNode, String resolveInfo, String referenceRole) {
EditorCell cellWithRole = editorComponent.findNodeCellWithRole(sourceNode, referenceRole);
if (cellWithRole == null) {
return false;
}
return substituteCell(cellWithRole, resolveInfo, editorComponent.getEditorContext());
}
public static boolean substituteCell(EditorCell editorCell, String pattern, EditorContext editorContext) {
SubstituteInfo substituteInfo = editorCell.getSubstituteInfo();
if (substituteInfo == null) {
return false;
}
final SubstituteAction applicableSubstituteAction = EditorBasedReferenceResolverUtils.getApplicableSubstituteAction(substituteInfo, pattern);
if (applicableSubstituteAction == null) {
return false;
}
applicableSubstituteAction.substitute(editorContext, pattern);
return true;
}
private static SubstituteAction getApplicableSubstituteAction(SubstituteInfo substituteInfo, String resolveInfo) {
SubstituteAction result = null;
substituteInfo.invalidateActions();
for (SubstituteAction nextAction : ListSequence.fromList(substituteInfo.getMatchingActions(resolveInfo, false))) {
if (nextAction.canSubstitute(resolveInfo)) {
if (result != null) {
return null;
}
result = nextAction;
}
}
return (result != null && result.canSubstituteStrictly(resolveInfo) ? result : null);
}
}