Ejemplo n.º 1
0
 @Nullable
 private String[] getExplicitHintsForNode(SNode node) {
   if (myHintsForNodeMap == null || !myHintsForNodeMap.containsKey(node.getReference())) {
     return null;
   }
   Collection<String> hints = myHintsForNodeMap.get(node.getReference());
   return hints.toArray(new String[hints.size()]);
 }
Ejemplo n.º 2
0
 public static SNode getAttribute(SNode node, IAttributeDescriptor descriptor) {
   Iterable<SNode> list = getAttributes(node, descriptor);
   if (Sequence.fromIterable(list).isEmpty()) {
     return null;
   }
   if (Sequence.fromIterable(list).count() > 1) {
     if (LOG.isEnabledFor(Level.ERROR)) {
       LOG.error(
           Sequence.fromIterable(list).count()
               + " nodes match single value attribute. The first found node returned as the value.");
     }
     if (LOG.isEnabledFor(Level.ERROR)) {
       LOG.error(
           "  node="
               + node.getReference()
               + "; attribute="
               + ((String)
                   BHReflection.invoke(
                       SNodeOperations.asSConcept(
                           SNodeOperations.getConcept(Sequence.fromIterable(list).first())),
                       SMethodTrimmedId.create("getRole", null, "1653mnvAgoG")))
               + " ("
               + Sequence.fromIterable(list).first().getNodeId()
               + ")");
     }
   }
   return Sequence.fromIterable(list).first();
 }
Ejemplo n.º 3
0
 public static SNode setAttribute(SNode node, IAttributeDescriptor descriptor, SNode value) {
   Iterable<SNode> oldlist = getAttributes(node, descriptor);
   if (Sequence.fromIterable(oldlist).isEmpty() && (value != null)) {
     addAttribute(node, descriptor, value);
   } else if ((value == null)) {
     deleteAttribute(node, descriptor, value);
   } else {
     if (Sequence.fromIterable(oldlist).count() > 1) {
       if (LOG.isEnabledFor(Level.ERROR)) {
         LOG.error(
             Sequence.fromIterable(oldlist).count()
                 + " nodes match signle value attribute during attribute replacing. Only the first found node replaced.");
       }
       if (LOG.isEnabledFor(Level.ERROR)) {
         LOG.error(
             "  node="
                 + node.getReference()
                 + "; attribute="
                 + ((String)
                     BHReflection.invoke(
                         SNodeOperations.asSConcept(
                             SNodeOperations.getConcept(Sequence.fromIterable(oldlist).first())),
                         SMethodTrimmedId.create("getRole", null, "1653mnvAgoG")))
                 + " ("
                 + Sequence.fromIterable(oldlist).first().getNodeId()
                 + ")");
       }
     }
     SNodeOperations.replaceWithAnother(Sequence.fromIterable(oldlist).first(), value);
     descriptor.update(value);
   }
   return value;
 }
Ejemplo n.º 4
0
 /**
  * @deprecated Use alternative with SNodeReference, without explicit input node and
  *     ITemplateGenerator Kept in 3.3 as there might be generated code in MC.isApplicable methods
  *     that call it.
  */
 @Deprecated
 @ToRemove(version = 3.1)
 public TemplateQueryContext(
     SNode inputNode, SNode templateNode, TemplateContext context, ITemplateGenerator generator) {
   myTemplateNode = templateNode == null ? null : templateNode.getReference();
   myContext = context == null ? null : context.subContext(inputNode);
   myGenerator = generator;
 }
Ejemplo n.º 5
0
  @Override
  public SNodeReference getModifiesSwitch() {
    SReference ref = mySwitch.getReference(RuleUtil.link_TemplateSwitch_modifiedSwitch);
    if (ref == null) {
      return null;
    }

    // proceed without resolving (at least for StaticReferences)
    return ref.toNodePointer();
  }
Ejemplo n.º 6
0
  private static void assertReferenceEquals(SNode expectedNode, SNode actualNode) {
    Set<String> roles = new HashSet<String>();
    roles.addAll(jetbrains.mps.util.SNodeOperations.getReferenceRoles(expectedNode));
    roles.addAll(jetbrains.mps.util.SNodeOperations.getReferenceRoles(actualNode));
    Map<String, Set<SReference>> expRoleToReferenceMap = createRoleToReferenceMap(expectedNode);
    Map<String, Set<SReference>> actRoleToReferenceMap = createRoleToReferenceMap(actualNode);

    for (String role : roles) {
      Assert.assertEquals(
          getErrorString("different number of referents in role " + role, expectedNode, actualNode),
          expRoleToReferenceMap.get(role).size(),
          actRoleToReferenceMap.get(role).size());

      SReference expectedReference = expectedNode.getReference(role);
      SReference actualReference = actualNode.getReference(role);

      assertReferenceEquals(
          getErrorString("reference in role " + role, expectedNode, actualNode),
          expectedReference,
          actualReference);
    }
  }
 @Override
 public List<SubstituteAction> createActions() {
   SConcept concept = mySourceNode.getConcept();
   SLanguage primaryLanguage = concept.getLanguage();
   if (primaryLanguage == null) {
     LOG.error(
         "Couldn't build actions : couldn't get declaring language for concept "
             + concept.getName());
     return Collections.emptyList();
   }
   SReference reference = mySourceNode.getReference(myLink);
   return createActions(reference == null ? null : reference.getTargetNode());
 }
Ejemplo n.º 8
0
  public void execute(SNode node) {
    SReference ref =
        node.getReference(((String) makeReferenceStatic_QuickFix.this.getField("role")[0]));
    SNode target = ref.getTargetNode();
    if (target == null) {
      return;
    }

    SReference staticRef =
        StaticReference.create(
            ((String) makeReferenceStatic_QuickFix.this.getField("role")[0]), node, target);
    node.setReference(((String) makeReferenceStatic_QuickFix.this.getField("role")[0]), staticRef);

    // add model import
    ((SModelBase) node.getModel()).addModelImport(target.getModel().getReference(), true);
  }
Ejemplo n.º 9
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);
                      }
                    });
          }
        });
  }
Ejemplo n.º 10
0
 public void showWarningMessage(SNode node, String message) {
   myGenerator
       .getLogger()
       .warning(node == null ? getTemplateNodeRef() : node.getReference(), message);
 }
Ejemplo n.º 11
0
 public void showInformationMessage(SNode node, String message) {
   myGenerator
       .getLogger()
       .info(node == null ? getTemplateNodeRef() : node.getReference(), message);
 }