package jetbrains.mps.ide.editor.actions;
/*Generated by MPS */
import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import jetbrains.mps.internal.collections.runtime.Sequence;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.openapi.editor.EditorContext;
import jetbrains.mps.ide.editor.MPSEditorDataKeys;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import jetbrains.mps.project.MPSProject;
import jetbrains.mps.ide.actions.MPSCommonDataKeys;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.openapi.editor.cells.EditorCell;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.awt.RelativePoint;
import jetbrains.mps.nodeEditor.EditorComponent;
import java.awt.Point;
import jetbrains.mps.openapi.editor.selection.Selection;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import jetbrains.mps.util.Pair;
import jetbrains.mps.openapi.intentions.IntentionExecutable;
import java.util.Comparator;
import com.intellij.openapi.actionSystem.AnAction;
import jetbrains.mps.intentions.IntentionsManager;
import jetbrains.mps.typesystem.inference.TypeContextManager;
import jetbrains.mps.typesystem.inference.ITypechecking;
import jetbrains.mps.typesystem.inference.TypeCheckingContext;
public class ShowSurroundWithIntentions_Action extends BaseAction {
private static final Icon ICON = null;
public ShowSurroundWithIntentions_Action() {
super("Surround with...", "", ICON);
this.setIsAlwaysVisible(true);
this.setExecuteOutsideCommand(true);
}
@Override
public boolean isDumbAware() {
return true;
}
@Override
public boolean isApplicable(AnActionEvent event, final Map<String, Object> _params) {
if (ShowSurroundWithIntentions_Action.this.getAnchorCell(_params) == null) {
return false;
}
return Sequence.fromIterable(ShowSurroundWithIntentions_Action.this.getAvailableIntentions(_params)).isNotEmpty();
}
@Override
public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
this.setEnabledState(event.getPresentation(), this.isApplicable(event, _params));
}
@Override
protected boolean collectActionData(AnActionEvent event, final Map<String, Object> _params) {
if (!(super.collectActionData(event, _params))) {
return false;
}
{
EditorContext p = event.getData(MPSEditorDataKeys.EDITOR_CONTEXT);
MapSequence.fromMap(_params).put("editorContext", p);
if (p == null) {
return false;
}
}
{
MPSProject p = event.getData(MPSCommonDataKeys.MPS_PROJECT);
MapSequence.fromMap(_params).put("project", p);
if (p == null) {
return false;
}
}
{
SNode p = event.getData(MPSCommonDataKeys.NODE);
MapSequence.fromMap(_params).put("selectedNode", p);
if (p == null) {
return false;
}
}
return true;
}
@Override
public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
final EditorCell selectedCell = ShowSurroundWithIntentions_Action.this.getAnchorCell(_params);
int x = selectedCell.getX();
int y = selectedCell.getY();
y += selectedCell.getHeight();
final Wrappers._T<ListPopup> popup = new Wrappers._T<ListPopup>(null);
((EditorContext) MapSequence.fromMap(_params).get("editorContext")).getRepository().getModelAccess().runReadAction(new Runnable() {
public void run() {
ActionGroup group = ShowSurroundWithIntentions_Action.this.getActionGroup(_params);
if (group.getChildren(event).length == 0) {
return;
}
popup.value = JBPopupFactory.getInstance().createActionGroupPopup("Surround with", group, event.getDataContext(), JBPopupFactory.ActionSelectionAid.ALPHA_NUMBERING, false);
}
});
if (popup.value == null) {
return;
}
RelativePoint relativePoint = new RelativePoint((EditorComponent) ((EditorContext) MapSequence.fromMap(_params).get("editorContext")).getEditorComponent(), new Point(x, y));
popup.value.show(relativePoint);
}
private EditorCell getAnchorCell(final Map<String, Object> _params) {
Selection selection = ((EditorComponent) ((EditorContext) MapSequence.fromMap(_params).get("editorContext")).getEditorComponent()).getSelectionManager().getSelection();
if (selection == null) {
return null;
}
List<EditorCell> selectedCells = ((List<EditorCell>) ((List) selection.getSelectedCells()));
if (ListSequence.fromList(selectedCells).isNotEmpty()) {
return ListSequence.fromList(selectedCells).first();
}
return null;
}
private ActionGroup getActionGroup(final Map<String, Object> _params) {
DefaultActionGroup actionGroup = new DefaultActionGroup();
Iterable<Pair<IntentionExecutable, SNode>> availableIntentions = Sequence.fromIterable(ShowSurroundWithIntentions_Action.this.getAvailableIntentions(_params)).sort(new Comparator<Pair<IntentionExecutable, SNode>>() {
public int compare(Pair<IntentionExecutable, SNode> a, Pair<IntentionExecutable, SNode> b) {
return ShowSurroundWithIntentions_Action.this.getDescriptior(a, _params).compareTo(ShowSurroundWithIntentions_Action.this.getDescriptior(b, _params));
}
}, true);
for (Pair<IntentionExecutable, SNode> pair : Sequence.fromIterable(availableIntentions)) {
final Pair<IntentionExecutable, SNode> finalPair = pair;
actionGroup.add(new AnAction(ShowSurroundWithIntentions_Action.this.getDescriptior(pair, _params)) {
@Override
public void actionPerformed(AnActionEvent event) {
((MPSProject) MapSequence.fromMap(_params).get("project")).getModelAccess().executeCommandInEDT(new Runnable() {
@Override
public void run() {
finalPair.o1.execute(finalPair.o2, ((EditorContext) MapSequence.fromMap(_params).get("editorContext")));
}
});
}
});
}
return actionGroup;
}
private Iterable<Pair<IntentionExecutable, SNode>> getAvailableIntentions(final Map<String, Object> _params) {
final IntentionsManager.QueryDescriptor query = new IntentionsManager.QueryDescriptor();
query.setSurroundWith(true);
query.setCurrentNodeOnly(true);
return TypeContextManager.getInstance().runTypeCheckingComputation(((EditorComponent) ((EditorContext) MapSequence.fromMap(_params).get("editorContext")).getEditorComponent()).getTypecheckingContextOwner(), ((EditorContext) MapSequence.fromMap(_params).get("editorContext")).getEditorComponent().getEditedNode(), new ITypechecking.Computation<Iterable<Pair<IntentionExecutable, SNode>>>() {
@Override
public Iterable<Pair<IntentionExecutable, SNode>> compute(TypeCheckingContext context) {
return IntentionsManager.getInstance().getAvailableIntentions(query, ((SNode) MapSequence.fromMap(_params).get("selectedNode")), ((EditorContext) MapSequence.fromMap(_params).get("editorContext")));
}
});
}
private String getDescriptior(Pair<IntentionExecutable, SNode> pair, final Map<String, Object> _params) {
return pair.o1.getDescription(pair.o2, ((EditorContext) MapSequence.fromMap(_params).get("editorContext")));
}
}