private boolean canExecute_internal(
        final KeyEvent keyEvent,
        final EditorContext editorContext,
        final SNode node,
        final List<SNode> selectedNodes) {
      if (ListSequence.fromList(selectedNodes).count() != 1) {
        return false;
      }

      // Selected node should be within TypeVariableDeclaration
      SNode selectedNode = ListSequence.fromList(selectedNodes).first();
      SNode typeVarDeclaration =
          SNodeOperations.getAncestor(
              selectedNode,
              "jetbrains.mps.baseLanguage.structure.TypeVariableDeclaration",
              true,
              false);
      if (typeVarDeclaration == null) {
        return false;
      }

      // Next cell should belong to some other SNode, not our typeVarDeclaration
      jetbrains.mps.nodeEditor.cells.EditorCell selectedCell =
          (jetbrains.mps.nodeEditor.cells.EditorCell) editorContext.getSelectedCell();
      jetbrains.mps.nodeEditor.cells.EditorCell nextLeaf = selectedCell.getNextLeaf();
      if (nextLeaf == null) {
        return false;
      }
      SNode nextCellNode = (SNode) nextLeaf.getSNode();
      return nextCellNode != null && nextCellNode != typeVarDeclaration;
    }
 @Override
 public void execute(final jetbrains.mps.openapi.editor.EditorContext context) {
   EditorComponent editorComponent = ((EditorComponent) context.getEditorComponent());
   SelectionManager selectionManager = editorComponent.getSelectionManager();
   Selection selection = selectionManager.getSelection();
   if (selection instanceof SingularSelection) {
     EditorCell selectedCell = ((SingularSelection) selection).getEditorCell();
     SNode selectedNode = selectedCell.getSNode();
     SNode topMostNodeInSingularContainment =
         findTopMostNodeWithSingularContainment(selectedNode);
     if (topMostNodeInSingularContainment != selectedNode) {
       EditorCell nodeCell = editorComponent.findNodeCell(topMostNodeInSingularContainment);
       if (nodeCell != null) {
         editorComponent.pushSelection(nodeCell);
         editorComponent.scrollToCell(nodeCell);
       }
     } else {
       Selection newSelection =
           selectionManager.createRangeSelection(selectedNode, selectedNode);
       if (newSelection instanceof NodeRangeSelection && selectedCell.isBigCell()) {
         newSelection = ((NodeRangeSelection) newSelection).enlargeSelection(myUp);
       }
       if (newSelection != null) {
         selectionManager.pushSelection(newSelection);
         newSelection.ensureVisible();
       }
     }
   } else if (selection instanceof NodeRangeSelection) {
     Selection newSelection = ((NodeRangeSelection) selection).enlargeSelection(myUp);
     if (newSelection != null) {
       selectionManager.pushSelection(newSelection);
       newSelection.ensureVisible();
     }
   }
 }
Beispiel #3
0
 public static void addVariablePattern(EditorContext context) {
   EditorCell contextCell = context.getSelectedCell();
   SNode node = contextCell.getSNode();
   SNode linkDeclaration =
       SNodeOperations.cast(
           contextCell.getLinkDeclaration(),
           "jetbrains.mps.lang.structure.structure.LinkDeclaration");
   SNode genuineLinkDeclaration = SModelUtil.getGenuineLinkDeclaration(linkDeclaration);
   if (linkDeclaration != null
       && SPropertyOperations.hasValue(
           genuineLinkDeclaration, "metaClass", "reference", "reference")) {
     String role = SPropertyOperations.getString(genuineLinkDeclaration, "role");
     AttributeOperations.createAndSetAttrbiute(
         node,
         new IAttributeDescriptor.LinkAttribute(
             SConceptOperations.findConceptDeclaration(
                 "jetbrains.mps.lang.pattern.structure.LinkPatternVariableDeclaration"),
             role),
         "jetbrains.mps.lang.pattern.structure.LinkPatternVariableDeclaration");
   } else {
     AttributeOperations.createAndSetAttrbiute(
         node,
         new IAttributeDescriptor.NodeAttribute(
             SConceptOperations.findConceptDeclaration(
                 "jetbrains.mps.lang.pattern.structure.Pattern")),
         "jetbrains.mps.lang.pattern.structure.PatternVariableDeclaration");
   }
 }
Beispiel #4
0
 public static boolean isPatternApplicable(EditorContext context) {
   EditorCell contextCell = context.getSelectedCell();
   if (contextCell == null) {
     return false;
   }
   INodeAdapter node = BaseAdapter.fromNode(contextCell.getSNode());
   if (node == null) {
     return false;
   }
   return node.findParent(PatternExpression.class) != null;
 }
Beispiel #5
0
 public boolean canExecute(final KeyEvent keyEvent, final EditorContext editorContext) {
   EditorCell contextCell = editorContext.getContextCell();
   if ((contextCell == null)) {
     return false;
   }
   SNode contextNode = contextCell.getSNode();
   if (contextNode == null) {
     return false;
   }
   if (contextNode.isInstanceOfConcept("jetbrains.mps.build.packaging.structure.Module")) {
     return true;
   }
   return false;
 }
  private boolean canPasteBefore(EditorCell selectedCell, List<SNode> pasteNodes) {
    if (!selectedCell.isFirstPositionInBigCell()) return false;
    SNode anchor = selectedCell.getSNode();
    if (anchor.getParent() == null) return false;

    NodeAndRole nodeAndRole =
        new NodePaster(pasteNodes).getActualAnchorNode(anchor, anchor.getRoleInParent());
    if (nodeAndRole == null) return false;

    EditorCell targetCell = selectedCell.getEditor().findNodeCell(nodeAndRole.myNode);
    return targetCell != null
        && targetCell.getFirstLeaf(CellConditions.SELECTABLE) == selectedCell
        && new NodePaster(pasteNodes).canPasteRelative(nodeAndRole.myNode);
  }
 @Override
 public boolean canExecute(jetbrains.mps.openapi.editor.EditorContext context) {
   // TODO: this check should be reallocated into Selection.canExecuteAction() method once it
   // created
   Selection selection =
       ((EditorComponent) context.getEditorComponent()).getSelectionManager().getSelection();
   if (selection instanceof NodeRangeSelection) {
     return true;
   }
   if (selection instanceof SingularSelection) {
     EditorCell editorCell = ((SingularSelection) selection).getEditorCell();
     return editorCell.getSNode().getParent() != null;
   }
   return false;
 }
 public boolean canExecute(final KeyEvent keyEvent, final EditorContext editorContext) {
   EditorCell contextCell = editorContext.getContextCell();
   if ((contextCell == null)) {
     return false;
   }
   SNode contextNode = contextCell.getSNode();
   if (contextNode == null) {
     return false;
   }
   if (contextNode.isInstanceOfConcept(
       "jetbrains.mps.baseLanguage.structure.ParenthesizedExpression")) {
     return true;
   }
   return false;
 }
  @Override
  public void execute(EditorContext context) {
    LOG.assertInCommand();
    EditorComponent editorComponent = (EditorComponent) context.getEditorComponent();
    EditorCell selectedCell = editorComponent.getSelectedCell();
    SNode anchorNode = selectedCell.getSNode();

    PasteNodeData pasteNodeData =
        CopyPasteUtil.getPasteNodeDataFromClipboard(anchorNode.getModel());
    if (pasteNodeData == null || pasteNodeData.getNodes().isEmpty()) {
      pasteNodeData =
          CopyPasteUtil.getConvertedFromClipboard(
              anchorNode.getModel(), context.getOperationContext().getProject());
      if (pasteNodeData == null) return;
    }
    List<SNode> pasteNodes = pasteNodeData.getNodes();
    Set<SReference> requireResolveReferences = pasteNodeData.getRequireResolveReferences();

    new NodePaster(pasteNodes).pasteRelative(anchorNode, myPastePlaceHint);
    ResolverComponent.getInstance()
        .resolveScopesOnly(requireResolveReferences, context.getOperationContext());

    // set selection
    editorComponent.flushEvents();
    EditorCell nodeCell = editorComponent.findNodeCell(pasteNodes.get(0));
    if (nodeCell == null) return; // after 'set reference'?
    EditorCell_Label labelCell =
        CellFinderUtil.findChildByClass(nodeCell, EditorCell_Label.class, true);

    if (labelCell != null) {
      editorComponent.changeSelection(labelCell);
    }

    if (pasteNodes.size() == 1) {
      editorComponent.pushSelection(nodeCell);
    } else {
      SelectionManager selectionManager = editorComponent.getSelectionManager();
      selectionManager.pushSelection(
          selectionManager.createRangeSelection(
              pasteNodes.get(0), pasteNodes.get(pasteNodes.size() - 1)));
    }
  }
Beispiel #10
0
  public boolean canExecute(EditorContext context) {
    EditorCell selectedCell = getCellToPasteTo((EditorCell) context.getSelectedCell());
    if (selectedCell == null) {
      return false;
    }
    SNode selectedNode = selectedCell.getSNode();
    if (selectedNode == null || jetbrains.mps.util.SNodeOperations.isDisposed(selectedNode)) {
      return false;
    }
    List<SNode> pasteNodes = CopyPasteUtil.getNodesFromClipboard(selectedNode.getModel());
    if (pasteNodes == null || pasteNodes.isEmpty()) {
      return CopyPasteUtil.isConversionAvailable(selectedNode.getModel(), selectedNode);
    }

    if (!new NodePaster(pasteNodes).canPaste(selectedCell)) {
      LOG.debug("Couldn't paste node here");
      return false;
    }
    return true;
  }
  @Override
  public boolean canExecute(EditorContext context) {
    EditorCell selectedCell = (EditorCell) context.getSelectedCell();
    if (selectedCell == null) {
      return false;
    }
    SNode anchorNode = selectedCell.getSNode();
    if (anchorNode == null) {
      return false;
    }
    List<SNode> pasteNodes = CopyPasteUtil.getNodesFromClipboard(anchorNode.getModel());
    if (pasteNodes == null || pasteNodes.isEmpty()) {
      return CopyPasteUtil.isConversionAvailable(anchorNode.getModel(), anchorNode);
    }

    if (!new NodePaster(pasteNodes).canPasteRelative(anchorNode)) {
      LOG.debug("Couldn't paste node relative");
      return false;
    }
    return true;
  }
 public void execute(final KeyEvent keyEvent, final EditorContext editorContext) {
   EditorCell contextCell = editorContext.getContextCell();
   this.execute_internal(
       keyEvent, editorContext, contextCell.getSNode(), this.getSelectedNodes(editorContext));
 }
Beispiel #13
0
  public void execute(final EditorContext context) {
    LOG.assertInCommand();
    final EditorComponent editorComponent = (EditorComponent) context.getEditorComponent();
    EditorCell pasteTargetCell = getCellToPasteTo(editorComponent.getSelectedCell());
    final CellInfo pasteTargetCellInfo = pasteTargetCell.getCellInfo();
    final SNode nodeSelected = pasteTargetCell.getSNode();
    final SNodePointer selectedNodePointer = new SNodePointer(nodeSelected);
    final SModel model = nodeSelected.getModel();
    // sometimes model is not in repository (paste in merge dialog)
    final boolean inRepository = model.getModelDescriptor() == selectedNodePointer.getModel();

    PasteNodeData data = CopyPasteUtil.getPasteNodeDataFromClipboard(model);
    if (data == null || data.getNodes().isEmpty()) {
      data =
          CopyPasteUtil.getConvertedFromClipboard(
              model, context.getOperationContext().getProject());
      if (data == null || data.getNodes().isEmpty()) return;
    }
    final PasteNodeData pasteNodeData = data;

    SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            final Runnable addImportsRunnable =
                CopyPasteUtil.addImportsWithDialog(
                    pasteNodeData, model, context.getOperationContext());
            ModelAccess.instance()
                .runCommandInEDT(
                    new Runnable() {
                      public void run() {
                        if (addImportsRunnable != null) {
                          addImportsRunnable.run();
                        }
                        SNode selectedNode =
                            inRepository ? selectedNodePointer.getNode() : nodeSelected;
                        if (jetbrains.mps.util.SNodeOperations.isDisposed(selectedNode)) {
                          StringBuilder errorText =
                              new StringBuilder(
                                  "Selected node is disposed: " + selectedNode.toString());
                          SModelReference modelReference = selectedNodePointer.getModelReference();
                          if (modelReference != null) {
                            SModelDescriptor modelDescriptor =
                                SModelRepository.getInstance().getModelDescriptor(modelReference);
                            if (modelDescriptor != null) {
                              SModel sModel = modelDescriptor.getSModel();
                              errorText.append(", sModel.isDisposed(): " + sModel.isDisposed());
                              SNode node = sModel.getNodeById(selectedNodePointer.getNodeId());
                              if (node != null) {
                                errorText.append(
                                    ", node != null, node.isDisposed(): "
                                        + jetbrains.mps.util.SNodeOperations.isDisposed(node));
                              } else {
                                errorText.append(", node == null");
                              }
                            }
                          }
                          LOG.error(errorText.toString());
                          return;
                        }
                        EditorCell selectedCell = pasteTargetCellInfo.findCell(editorComponent);
                        assert selectedCell != null;

                        List<SNode> pasteNodes = pasteNodeData.getNodes();

                        if (canPasteBefore(selectedCell, pasteNodes)) {
                          new NodePaster(pasteNodes)
                              .pasteRelative(selectedNode, PastePlaceHint.BEFORE_ANCHOR);
                        } else {
                          new NodePaster(pasteNodes).paste(selectedCell);
                        }

                        Set<SReference> requireResolveReferences = new HashSet<SReference>();
                        for (SReference ref : pasteNodeData.getRequireResolveReferences()) {
                          // ref can be detached from model while using copy/paste handlers
                          if (ref.getSourceNode() == null || ref.getSourceNode().getModel() == null)
                            continue;
                          requireResolveReferences.add(ref);
                        }

                        ResolverComponent.getInstance()
                            .resolveScopesOnly(
                                requireResolveReferences, context.getOperationContext());

                        // set selection
                        editorComponent.flushEvents();
                        EditorCell nodeCell = editorComponent.findNodeCell(pasteNodes.get(0));
                        if (nodeCell == null) return; // after 'set reference'?

                        EditorCell_Label labelCell =
                            nodeCell.findChild(CellFinders.byClass(EditorCell_Label.class, true));
                        if (labelCell != null) {
                          editorComponent.changeSelection(labelCell);
                        }

                        if (pasteNodes.size() == 1) {
                          editorComponent.pushSelection(nodeCell);
                        } else {
                          SNode firstNodeToSelect = pasteNodes.get(0);
                          SNode lastNodeToSelect = null;
                          for (int i = pasteNodes.size() - 1;
                              i > 0 && lastNodeToSelect == null;
                              i--) {
                            if (pasteNodes.get(i).getParent() == firstNodeToSelect.getParent()) {
                              lastNodeToSelect = pasteNodes.get(i);
                            }
                          }
                          if (lastNodeToSelect != null) {
                            SelectionManager selectionManager =
                                editorComponent.getSelectionManager();
                            selectionManager.pushSelection(
                                selectionManager.createRangeSelection(
                                    firstNodeToSelect, lastNodeToSelect));
                          }
                        }
                      }
                    },
                    context.getOperationContext().getProject());
          }
        });
  }