示例#1
0
  @NotNull
  private String[] getInitialEditorHints(EditorContext editorContext) {
    if (myInitialEditorHints != null) {
      return myInitialEditorHints;
    }

    Project project =
        ProjectHelper.toIdeaProject(ProjectHelper.getProject(editorContext.getRepository()));
    if (project == null) {
      return EMPTY_HINTS_ARRAY;
    }
    HintsState state = ConceptEditorHintSettingsComponent.getInstance(project).getState();
    return state.getEnabledHints().toArray(EMPTY_HINTS_ARRAY);
  }
示例#2
0
  @Override
  public void execute(final EditorContext context) {
    LOG.assertLog(
        context.getRepository().getModelAccess().isCommandAction(),
        "This action must be performed in command");
    final EditorComponent editorComponent = (EditorComponent) context.getEditorComponent();
    final Selection selection = editorComponent.getSelectionManager().getSelection();
    final List<SNode> selectedNodes = selection.getSelectedNodes();

    // this is used in case node is in repo to pass it into invokeLater
    final List<SNodeReference> selectedReferences = new ArrayList<SNodeReference>();
    for (SNode node : selectedNodes) {
      selectedReferences.add(node.getReference());
    }

    final CellInfo pasteTargetCellInfo;
    final SNode cellNodeSelected;
    final SNodeReference selectedCellReference;
    if (selection instanceof SingularSelection) {
      EditorCell pasteTargetCell = getCellToPasteTo(context.getSelectedCell());
      if (pasteTargetCell == null) {
        return;
      }
      pasteTargetCellInfo = APICellAdapter.getCellInfo(pasteTargetCell);
      cellNodeSelected = pasteTargetCell.getSNode();
      selectedCellReference = cellNodeSelected.getReference();
    } else {
      pasteTargetCellInfo = null;
      cellNodeSelected = null;
      selectedCellReference = null;
    }

    final SModel modelToPaste = selectedNodes.get(0).getModel();

    // sometimes model is not in repository (paste in merge dialog)
    final boolean inRepository =
        modelToPaste.getReference().resolve(context.getRepository()) != null;

    // FIXME relationship between Project and Editor needs attention, it's bad to extract
    final Project mpsProject = ProjectHelper.getProject(context.getRepository());
    if (mpsProject == null) {
      LOG.warning("Paste needs a project to show a dialog for additional imports");
      return;
    }

    final PasteNodeData pasteNodeData = CopyPasteUtil.getPasteNodeDataFromClipboard(modelToPaste);

    // FIXME why SwingUtlities? Is it necessary to execute in EDT thread? If yes, use ThreadUtils.
    // If not, use any other thread than UI, e.g. app's pooled one.
    SwingUtilities.invokeLater(
        new Runnable() {
          @Override
          public void run() {
            final Runnable addImportsRunnable =
                CopyPasteUtil.addImportsWithDialog(pasteNodeData, modelToPaste, mpsProject);
            context
                .getRepository()
                .getModelAccess()
                .executeCommandInEDT(
                    new Runnable() {
                      @Override
                      public void run() {
                        if (addImportsRunnable != null) {
                          addImportsRunnable.run();
                        }

                        List<SNode> pasteNodes = pasteNodeData.getNodes();
                        List<SNode> currentSelectedNodes;
                        if (!inRepository) {
                          currentSelectedNodes = selectedNodes;
                        } else {
                          currentSelectedNodes = new ArrayList<SNode>();
                          for (SNodeReference ref : selectedReferences) {
                            currentSelectedNodes.add(
                                ref.resolve(MPSModuleRepository.getInstance()));
                          }
                        }

                        NodePaster nodePaster = new NodePaster(pasteNodes);
                        boolean disposed =
                            checkDisposedSelectedNodes(currentSelectedNodes, selectedReferences);
                        boolean canPasteWithRemove =
                            !disposed && nodePaster.canPasteWithRemove(currentSelectedNodes);
                        if (selection instanceof SingularSelection
                            && (selection instanceof EditorCellLabelSelection
                                    && !isCompletelySelected((EditorCellLabelSelection) selection)
                                || (selection instanceof EditorCellSelection
                                    && !canPasteWithRemove))) {
                          EditorCell selectedCell = pasteTargetCellInfo.findCell(editorComponent);
                          assert selectedCell != null;

                          if (canPasteBefore(selectedCell, pasteNodes)) {
                            SNode selectedNode =
                                inRepository
                                    ? selectedCellReference.resolve(
                                        MPSModuleRepository.getInstance())
                                    : cellNodeSelected;
                            if (checkDisposed(selectedCellReference, cellNodeSelected)) {
                              return;
                            }
                            new NodePaster(pasteNodes)
                                .pasteRelative(selectedNode, PastePlaceHint.BEFORE_ANCHOR);
                          } else {
                            new NodePaster(pasteNodes).paste(selectedCell);
                          }
                        } else if ((selection instanceof MultipleSelection
                                || selection instanceof EditorCellSelection)
                            && canPasteWithRemove) {
                          nodePaster.pasteWithRemove(currentSelectedNodes);
                        } else {
                          return;
                        }

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

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

                        // set selection
                        editorComponent.getUpdater().flushModelEvents();
                        SNode lastNode = pasteNodes.get(pasteNodes.size() - 1);
                        editorComponent
                            .getSelectionManager()
                            .setSelection(lastNode, SelectionManager.LAST_CELL, -1);
                      }
                    });
          }
        });
  }