Ejemplo n.º 1
0
  @Override
  protected void doSubmitCommandL(
      final String[] lines,
      final SrcfileData srcfile,
      final IRSrcref srcref,
      final IProgressMonitor monitor)
      throws CoreException {
    if ((fCurrentPrompt.meta
            & (IRBasicAdapter.META_PROMPT_DEFAULT | IRBasicAdapter.META_PROMPT_SUSPENDED))
        == 0) {
      super.doSubmitCommandL(lines, srcfile, srcref, monitor);
      return;
    }

    final FunctionCall prepare = createFunctionCall("rj:::.statet.prepareCommand");
    prepare.add("lines", fRObjectFactory.createVector(fRObjectFactory.createCharData(lines)));

    if (srcfile != null && srcref != null) {
      final List<String> attributeNames = new ArrayList<String>();
      final List<RObject> attributeValues = new ArrayList<RObject>();

      if (srcfile.getName() != null) {
        prepare.addChar("filename", srcfile.getName());
      }
      //			if (srcfile.workspacePath != null) {
      //				attributeNames.add("statet.Path");
      //				attributeValues.add(fRObjectFactory.createVector(fRObjectFactory.createCharData(
      //						new String[] { srcfile.workspacePath } )));
      //			}
      if (srcfile.getTimestamp() != 0) {
        attributeNames.add("timestamp");
        attributeValues.add(
            fRObjectFactory.createVector(
                fRObjectFactory.createNumData(new double[] {srcfile.getTimestamp()})));
      }
      final int[] rjSrcref = RDbg.createRJSrcref(srcref);
      if (rjSrcref != null) {
        attributeNames.add("linesSrcref");
        attributeValues.add(fRObjectFactory.createVector(fRObjectFactory.createIntData(rjSrcref)));
      }

      if (attributeNames.size() > 0) {
        prepare.add(
            "srcfileAttributes",
            fRObjectFactory.createList(
                attributeValues.toArray(new RObject[attributeValues.size()]),
                attributeNames.toArray(new String[attributeNames.size()])));
      }

      if (srcref instanceof IRModelSrcref) {
        // Move to abstract controller or breakpoint adapter?
        final IRModelSrcref modelSrcref = (IRModelSrcref) srcref;
        final List<IRLangSourceElement> elements = modelSrcref.getElements();
        if (elements.size() > 0) {
          final List<String> elementIds = new ArrayList<String>(elements.size());
          final List<RObject> elementIndexes = new ArrayList<RObject>(elements.size());
          for (final IRLangSourceElement element : elements) {
            if (TAG_ELEMENT_FILTER.include(element)) {
              final FDef fdef = (FDef) element.getAdapter(FDef.class);
              if (fdef != null) {
                final String elementId = RDbg.getElementId(element);
                final RAstNode cont = fdef.getContChild();
                final int[] path =
                    RAst.computeRExpressionIndex(cont, RAst.getRRootNode(cont, modelSrcref));
                if (elementId != null && path != null) {
                  final int[] fullPath = new int[path.length + 1];
                  fullPath[0] = 1;
                  System.arraycopy(path, 0, fullPath, 1, path.length);
                  elementIds.add(elementId);
                  elementIndexes.add(
                      fRObjectFactory.createVector(fRObjectFactory.createIntData(fullPath)));
                }
              }
            }
          }
          if (elementIds.size() > 0) {
            prepare.add(
                "elementIds",
                fRObjectFactory.createList(
                    elementIndexes.toArray(new RObject[elementIndexes.size()]),
                    elementIds.toArray(new String[elementIds.size()])));
          }
        }
      }
    }

    prepare.evalVoid(monitor);

    final boolean addToHistory = (fCurrentPrompt.meta & IRBasicAdapter.META_HISTORY_DONTADD) == 0;
    fCurrentInput = lines[0];
    doBeforeSubmitL();
    for (int i = 1; i < lines.length; i++) {
      setCurrentPromptL(fContinuePromptText, addToHistory);
      fCurrentInput = lines[i];
      doBeforeSubmitL();
    }
    fCurrentInput = "rj:::.statet.evalCommand()";
    doSubmitL(monitor);
  }
  private void createChanges(final TextFileChange change, final SubMonitor progress)
      throws BadLocationException, CoreException {
    fSourceUnit.connect(progress.newChild(1));
    try {
      final AbstractDocument document = fSourceUnit.getDocument(progress.newChild(1));
      final RCodeStyleSettings codeStyle = RRefactoringAdapter.getCodeStyle(fSourceUnit);

      RAstNode firstParentChild = (RAstNode) fFunction.getAdapter(IAstNode.class);
      while (true) {
        final RAstNode parent = firstParentChild.getRParent();
        if (parent == null
            || parent.getNodeType() == NodeType.SOURCELINES
            || parent.getNodeType() == NodeType.BLOCK) {
          break;
        }
        firstParentChild = parent;
      }

      final IRegion region = fAdapter.expandWhitespaceBlock(document, fOperationRegion);
      final int insertOffset =
          fAdapter
              .expandWhitespaceBlock(
                  document,
                  fAdapter.expandSelectionRegion(
                      document, new Region(firstParentChild.getOffset(), 0), fOperationRegion))
              .getOffset();
      final FDef fdefNode = (FDef) fFunction.getAdapter(FDef.class);
      final IRegion fbodyRegion =
          fAdapter.expandWhitespaceBlock(
              document,
              fAdapter.expandSelectionRegion(document, fdefNode.getContChild(), fOperationRegion));

      TextChangeCompatibility.addTextEdit(
          change,
          Messages.FunctionToS4Method_Changes_DeleteOld_name,
          new DeleteEdit(region.getOffset(), region.getLength()));

      final String nl = document.getDefaultLineDelimiter();
      final String argAssign = codeStyle.getArgAssignString();

      final StringBuilder sb = new StringBuilder();
      sb.append("setGeneric(\""); // $NON-NLS-1$
      sb.append(fFunctionName);
      sb.append("\","); // $NON-NLS-1$
      sb.append(nl);
      sb.append("function("); // $NON-NLS-1$
      boolean dots = false;
      for (final Variable variable : fVariablesList) {
        if (variable.getName().equals(RTerminal.S_ELLIPSIS)) {
          dots = true;
        }
        if (variable.getUseAsGenericArgument()) {
          sb.append(
              RElementName.create(RElementName.MAIN_DEFAULT, variable.getName()).getDisplayName());
          sb.append(", "); // $NON-NLS-1$
        }
      }
      if (!dots) {
        sb.append("..., "); // $NON-NLS-1$
      }
      sb.delete(sb.length() - 2, sb.length());
      sb.append(')');
      if (codeStyle.getNewlineFDefBodyBlockBefore()) {
        sb.append(nl);
      } else {
        sb.append(' ');
      }
      sb.append('{');
      sb.append(nl);
      sb.append("standardGeneric(\""); // $NON-NLS-1$
      sb.append(fFunctionName);
      sb.append("\")"); // $NON-NLS-1$
      sb.append(nl);
      sb.append("})"); // $NON-NLS-1$
      sb.append(nl);
      sb.append(nl);
      final String genericDef =
          RRefactoringAdapter.indent(sb, document, firstParentChild.getOffset(), fSourceUnit);
      TextChangeCompatibility.addTextEdit(
          change,
          Messages.FunctionToS4Method_Changes_AddGenericDef_name,
          new InsertEdit(insertOffset, genericDef));

      sb.setLength(0);
      sb.append("setMethod(\""); // $NON-NLS-1$
      sb.append(fFunctionName);
      sb.append("\","); // $NON-NLS-1$
      sb.append(nl);
      sb.append("signature("); // $NON-NLS-1$
      boolean hasType = false;
      for (final Variable variable : fVariablesList) {
        if (variable.getUseAsGenericArgument() && variable.getArgumentType() != null) {
          hasType = true;
          sb.append(
              RElementName.create(RElementName.MAIN_DEFAULT, variable.getName()).getDisplayName());
          sb.append(argAssign);
          sb.append("\""); // $NON-NLS-1$
          sb.append(variable.getArgumentType());
          sb.append("\", "); // $NON-NLS-1$
        }
      }
      if (hasType) {
        sb.delete(sb.length() - 2, sb.length());
      }
      sb.append("),"); // $NON-NLS-1$
      sb.append(nl);
      sb.append("function("); // $NON-NLS-1$
      final FDef.Args argsNode = fdefNode.getArgsChild();
      for (final Variable variable : fVariablesList) {
        sb.append(
            RElementName.create(RElementName.MAIN_DEFAULT, variable.getName()).getDisplayName());
        final FDef.Arg argNode = argsNode.getChild(variable.fArg.index);
        if (argNode.hasDefault()) {
          sb.append(argAssign);
          sb.append(
              document.get(
                  argNode.getDefaultChild().getOffset(), argNode.getDefaultChild().getLength()));
        }
        sb.append(", "); // $NON-NLS-1$
      }
      if (!fVariablesList.isEmpty()) {
        sb.delete(sb.length() - 2, sb.length());
      }
      sb.append(')');
      if (codeStyle.getNewlineFDefBodyBlockBefore()
          || fdefNode.getContChild().getNodeType() != NodeType.BLOCK) {
        sb.append(nl);
      } else {
        sb.append(' ');
      }
      sb.append(document.get(fbodyRegion.getOffset(), fbodyRegion.getLength()).trim());
      sb.append(")"); // $NON-NLS-1$
      sb.append(nl);
      final String methodDef =
          RRefactoringAdapter.indent(sb, document, firstParentChild.getOffset(), fSourceUnit);
      TextChangeCompatibility.addTextEdit(
          change,
          Messages.FunctionToS4Method_Changes_AddMethodDef_name,
          new InsertEdit(insertOffset, methodDef));
    } finally {
      fSourceUnit.disconnect(progress.newChild(1));
    }
  }
Ejemplo n.º 3
0
  @Override
  public void doSubmitFileCommandToConsole(
      final String[] lines,
      final SrcfileData srcfile,
      final ISourceUnit su,
      final IProgressMonitor monitor)
      throws CoreException {
    if (srcfile != null
        && su instanceof IRWorkspaceSourceUnit
        && su.getModelTypeId() == RModel.TYPE_ID) {
      try {
        final IRModelInfo modelInfo =
            (IRModelInfo) su.getModelInfo(RModel.TYPE_ID, IRModelManager.MODEL_FILE, monitor);
        if (modelInfo != null) {
          final IRLangSourceElement fileElement = modelInfo.getSourceElement();
          final RAstNode rootNode = (RAstNode) fileElement.getAdapter(IAstNode.class);
          final List<? extends IRLangSourceElement> elements =
              modelInfo.getSourceElement().getSourceChildren(TAG_ELEMENT_FILTER);

          final List<String> elementIds = new ArrayList<String>(elements.size());
          final List<RObject> elementIndexes = new ArrayList<RObject>(elements.size());

          for (final IRLangSourceElement element : elements) {
            final FDef fdef = (FDef) element.getAdapter(FDef.class);
            if (fdef != null) {
              final String elementId = RDbg.getElementId(element);
              final RAstNode cont = fdef.getContChild();
              final int[] path = RAst.computeRExpressionIndex(cont, rootNode);
              if (elementId != null && path != null) {
                elementIds.add(elementId);
                elementIndexes.add(
                    fRObjectFactory.createVector(fRObjectFactory.createIntData(path)));
              }
            }
          }

          final FunctionCall prepare =
              createFunctionCall("rj:::.statet.prepareSource"); // $NON-NLS-1$
          prepare.add(
              fRObjectFactory.createList(
                  new RObject[] {
                    fRObjectFactory.createVector(
                        fRObjectFactory.createCharData(new String[] {srcfile.getPath()})),
                    fRObjectFactory.createVector(
                        fRObjectFactory.createNumData(new double[] {srcfile.getTimestamp()})),
                    fRObjectFactory.createVector(
                        fRObjectFactory.createIntData(new int[] {rootNode.getChildCount()})),
                    fRObjectFactory.createList(
                        elementIndexes.toArray(new RObject[elementIndexes.size()]),
                        elementIds.toArray(new String[elementIds.size()])),
                  },
                  new String[] {
                    "path", "timestamp", "exprsLength", "elementIds"
                  })); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
          prepare.evalVoid(monitor);
        }
      } catch (final CoreException e) {
        RConsoleCorePlugin.log(
            new Status(
                IStatus.ERROR,
                RConsoleCorePlugin.PLUGIN_ID,
                -1,
                NLS.bind(
                    "An error occurred when preparing element tagging for file ''{0}''.",
                    srcfile.getPath()),
                e));
      }
    }
    super.doSubmitFileCommandToConsole(lines, srcfile, su, monitor);
  }