예제 #1
0
  /**
   * Generates content for the Roxygen comment for the given function definition
   *
   * @param rMethod function element
   * @param lineDelimiter the line delimiter to be used
   * @return
   * @throws CoreException thrown when the evaluation of the code template fails
   */
  public static EvaluatedTemplate getCommonFunctionRoxygenComment(
      final IRMethod rMethod, final String lineDelimiter) throws CoreException {
    final Template template =
        RUIPlugin.getDefault()
            .getRCodeGenerationTemplateStore()
            .findTemplate(RCodeTemplatesContextType.ROXYGEN_COMMONFUNCTION_TEMPLATE);
    if (template == null) {
      return null;
    }

    final ISourceUnit su = rMethod.getSourceUnit();
    final RCodeTemplatesContext context =
        new RCodeTemplatesContext(
            RCodeTemplatesContextType.ROXYGEN_COMMONFUNCTION_CONTEXTTYPE, su, lineDelimiter);
    context.setRElement(rMethod);

    try {
      final TemplateBuffer buffer = context.evaluate(template);
      if (buffer == null) {
        return null;
      }
      final EvaluatedTemplate data = new EvaluatedTemplate(buffer, lineDelimiter);

      final AbstractDocument content = data.startPostEdit();
      final StringBuilder tagBuffer = new StringBuilder(64);
      final TemplateVariable paramVariable =
          TemplatesUtil.findVariable(buffer, RCodeTemplatesContextType.ROXYGEN_PARAM_TAGS_VARIABLE);
      final Position[] paramPositions =
          new Position[(paramVariable != null) ? paramVariable.getOffsets().length : 0];
      for (int i = 0; i < paramPositions.length; i++) {
        paramPositions[i] = new Position(paramVariable.getOffsets()[i], paramVariable.getLength());
        content.addPosition(paramPositions[i]);
      }

      if (paramPositions.length > 0) {
        String[] tags = null;
        final ArgsDefinition args = rMethod.getArgsDefinition();
        if (args != null) {
          final int count = args.size();
          tags = new String[count];
          for (int i = 0; i < count; i++) {
            tagBuffer.append("@param "); // $NON-NLS-1$
            tagBuffer.append(args.get(i).name);
            tagBuffer.append(" "); // $NON-NLS-1$
            tags[i] = tagBuffer.toString();
            tagBuffer.setLength(0);
          }
        }
        for (final Position pos : paramPositions) {
          insertRoxygen(content, pos, tags);
        }
      }

      data.finishPostEdit();
      return data;
    } catch (final Exception e) {
      throw new CoreException(
          new Status(
              IStatus.ERROR,
              RUI.PLUGIN_ID,
              NLS.bind(
                  TemplateMessages.TemplateEvaluation_error_description, template.getDescription()),
              e));
    }
  }
예제 #2
0
  /**
   * Generates content for the Roxygen comment for the given method definition
   *
   * @param rMethod function element
   * @param lineDelimiter the line delimiter to be used
   * @return
   * @throws CoreException thrown when the evaluation of the code template fails
   */
  public static EvaluatedTemplate getMethodRoxygenComment(
      final IRMethod rMethod, final String lineDelimiter) throws CoreException {
    final Template template =
        RUIPlugin.getDefault()
            .getRCodeGenerationTemplateStore()
            .findTemplate(RCodeTemplatesContextType.ROXYGEN_S4METHOD_TEMPLATE);
    if (template == null) {
      return null;
    }

    final ISourceUnit su = rMethod.getSourceUnit();
    final RCodeTemplatesContext context =
        new RCodeTemplatesContext(
            RCodeTemplatesContextType.ROXYGEN_METHOD_CONTEXTTYPE, su, lineDelimiter);
    context.setRElement(rMethod);

    try {
      final TemplateBuffer buffer = context.evaluate(template);
      if (buffer == null) {
        return null;
      }
      final EvaluatedTemplate data = new EvaluatedTemplate(buffer, lineDelimiter);

      final AbstractDocument content = data.startPostEdit();
      final StringBuilder sb = new StringBuilder(64);

      final Position[] sigPositions;
      String sigText = null;
      {
        final TemplateVariable variable =
            TemplatesUtil.findVariable(buffer, RCodeTemplatesContextType.ROXYGEN_SIG_LIST_VARIABLE);
        sigPositions = new Position[(variable != null) ? variable.getOffsets().length : 0];
        for (int i = 0; i < sigPositions.length; i++) {
          sigPositions[i] = new Position(variable.getOffsets()[i], variable.getLength());
          content.addPosition(sigPositions[i]);
        }

        if (sigPositions.length > 0) {
          final ArgsDefinition args = rMethod.getArgsDefinition();
          if (args != null) {
            final int count = args.size();
            for (int i = 0; i < count; i++) {
              final Arg arg = args.get(i);
              if (arg.className == null || arg.className.equals("ANY")) { // $NON-NLS-1$
                break;
              }
              sb.append(arg.className);
              sb.append(","); // $NON-NLS-1$
            }
            if (sb.length() > 0) {
              sigText = sb.substring(0, sb.length() - 1);
              sb.setLength(0);
            }
          }
        }
      }
      final Position[] paramPositions;
      String[] paramTags = null;
      {
        final TemplateVariable variable =
            TemplatesUtil.findVariable(
                buffer, RCodeTemplatesContextType.ROXYGEN_PARAM_TAGS_VARIABLE);
        paramPositions = new Position[(variable != null) ? variable.getOffsets().length : 0];
        for (int i = 0; i < paramPositions.length; i++) {
          paramPositions[i] = new Position(variable.getOffsets()[i], variable.getLength());
          content.addPosition(paramPositions[i]);
        }

        if (paramPositions.length > 0) {
          String list = null;
          final ArgsDefinition args = rMethod.getArgsDefinition();
          if (args != null) {
            final int count = args.size();
            paramTags = new String[count];
            for (int i = 0; i < count; i++) {
              sb.append("@param "); // $NON-NLS-1$
              sb.append(args.get(i).name);
              sb.append(" "); // $NON-NLS-1$
              paramTags[i] = sb.toString();
              sb.setLength(0);
            }
          }
        }
      }

      if (sigPositions != null) {
        for (final Position pos : sigPositions) {
          insertRoxygen(content, pos, sigText);
        }
      }
      if (paramPositions != null) {
        for (final Position pos : paramPositions) {
          insertRoxygen(content, pos, paramTags);
        }
      }

      data.finishPostEdit();
      return data;
    } catch (final Exception e) {
      throw new CoreException(
          new Status(
              IStatus.ERROR,
              RUI.PLUGIN_ID,
              NLS.bind(
                  TemplateMessages.TemplateEvaluation_error_description, template.getDescription()),
              e));
    }
  }