Package com.puppetlabs.xtext.textflow

Examples of com.puppetlabs.xtext.textflow.TextFlow


    "/*   0123456789 0123456789\n *   0123456789 0123456789\n"//
        + " * abc\n" //
        + " * 0123456789 0123456789\n * 0123456789 0123456789\n"//
        + " */";
    IFormattingContext fmtCtx = get(IFormattingContext.class);
    TextFlow s = cp.formatComment(source, in, in, options, fmtCtx);
    assertEquals("Should produce expected result", expected, s.getText().toString());
  }
View Full Code Here


    CommentProcessor cp = new CommentProcessor();
    CommentFormattingOptions options = new CommentFormattingOptions(
      new ICommentFormatterAdvice.DefaultCommentAdvice(), 80);
    String source = "/* the\n  quick\n       *brown\n   * fox\n     \n  \n  */ ";
    IFormattingContext fmtCtx = get(IFormattingContext.class);
    TextFlow s = cp.formatComment(source, in, in, options, fmtCtx);
    // pad expected and result with 2 spaces to emulate the inserting of the result
    // (makes comparison look nicer if test fails)
    String expected = "  /* the\n   * quick\n   * brown\n   * fox\n   */ ";
    assertEquals("Should produce expected result", expected, "  " + CharSequences.trimLeft(s.getText()).toString());
  }
View Full Code Here

  }

  @Test
  public void test_TextFlow_pendingIndent() {
    TextFlow flow = this.getInjector().getInstance(TextFlow.class);
    flow.changeIndentation(1);
    flow.appendBreaks(1);
    flow.appendText("123");
    assertEquals("\n  123", new StringBuilder(flow.getText()).toString());

  }
View Full Code Here

  }

  @Test
  public void test_TextFlow_PendingMeasures() {
    TextFlow flow = this.getInjector().getInstance(TextFlow.class);
    flow.appendText("1234");
    assertEquals(4, flow.getWidthOfLastLine());
    assertEquals(1, flow.getHeight());
    flow.appendBreak();
    assertEquals(4, flow.getWidthOfLastLine());
    assertEquals(1, flow.getHeight());
    flow.changeIndentation(1);
    flow.appendBreak();
    flow.appendText("3");
    flow.appendText("45");
    assertEquals(5, flow.getWidthOfLastLine());
  }
View Full Code Here

    assertFlowOneLineNoBreak(flow);
  }

  @Test
  public void test_TextIndentFirst() {
    TextFlow flow = this.getInjector().getInstance(TextFlow.class);
    flow.setIndentation(1);
    flow.setIndentFirstLine(true);
    flow.appendText("1234");
    assertEquals("  1234", new StringBuilder(flow.getText()).toString());

  }
View Full Code Here

      CommentFormattingOptions options, IFormattingContext formattingContext) {
    final ICommentFormatterAdvice advice = options.advice;
    final String lineSeparator = formattingContext.getLineSeparatorInformation().getLineSeparator();
    final String endToken = out.getEndToken();
    List<CharSequence> lines = commentText.lines;
    TextFlow flow = new TextFlow(formattingContext);
    // StringBuilder builder = new StringBuilder();

    int indentSize = out.getLeftPosition();
    int leftMarginSize = out.getLeftMargin();
    if(Alignment.right == out.getMarkerColumnAlignment())
      indentSize += out.getMarkerColumnWidth() - out.getRepeatingToken().length();
    CharSequence indent = new CharSequences.Spaces(indentSize);
    CharSequence leftMargin = new CharSequences.Spaces(leftMarginSize);

    ensureTrailingLines(lines, options);
    try {
      // always process first line even if it is also the last
      int limit = Math.max(1, lines.size() - 1);
      boolean singleLine = lines.size() == 1;
      for(int i = 0; i < limit; i++) {
        // comment container
        if(i == 0)
          flow.append(CharSequences.spaces(out.getLeftPosition())).append(out.getStartToken());
        else
          flow.append(indent).append(out.getRepeatingToken());

        CharSequence s = lines.get(i);
        if(s.length() > 0) {
          boolean hasBannerLength = s.length() > 4;
          boolean alignSpecialLeft = advice.getAlignSpecialLinesLeft();

          // Homogeneous lines should not have a leftMargin e.g. '#---' '********' unless advice says so
          // anything starting with letter or digit, or that is not homogeneous has a leftMargin
          if(Character.isLetterOrDigit(s.charAt(0)) //
              ||
              !(CharSequences.isHomogeneous(s) && (hasBannerLength || alignSpecialLeft)))
            flow.append(leftMargin);
          flow.append(s);

          // // Homogeneous lines should not have a leftMargin e.g. '#---' '********' unless advice says so
          // // anything starting with letter or digit, or that is not homogeneous has a leftMargin
          // if(Character.isLetterOrDigit(s.charAt(0)) || !advice.getAlignSpecialLinesLeft() ||
          // !(CharSequences.isHomogeneous(s)))
          // flow.append(leftMargin);
          // flow.append(s);
        }
        if(!singleLine)
          flow.append(lineSeparator);
      }
      // process last line
      if(singleLine) {
        // last line is the same as the first
        if(endToken.length() > 0)
          flow.append(" "); // space before end token (if one will be output)
      }
      else {
        CharSequence s = lines.get(limit);
        flow.append(indent);
        if(s.length() > 0 || out.isSLStyle()) {
          flow.append(out.getRepeatingToken());
          if(s.length() > 0) {
            if(Character.isLetterOrDigit(s.charAt(0)) || !CharSequences.isHomogeneous(s))
              flow.append(leftMargin);
            flow.append(s);
            if(!out.isSLStyle())
              flow.append(" "); // a ML comment may be followed by something
          }
        }
      }
      if(endToken.length() > 0)
        flow.append(out.getEndToken());
      // finally append trailing stuff
      if(commentText.trailingContainerText.length() > 0)
        flow.append(commentText.trailingContainerText);
    }
    catch(IOException e) {
      // can't happen here, since the TextFlow uses a StringBuilder
      // TODO: Actually - this is wrong, the API is open
    }
View Full Code Here

  public TextFlow formatComment(CommentText commentText, ICommentContainerInformation out,
      CommentFormattingOptions options, IFormattingContext context) {
    foldLines(
      commentText.lines, options.advice, options.getMaxWidth() - out.getMarkerColumnWidth() - out.getLeftMargin());
    TextFlow result = emit(commentText, out, options, context);
    return result;

  }
View Full Code Here

    ICommentContainerInformation in = commentContext.create(Math.max(0, pos));
    CommentProcessor cpr = new CommentProcessor();
    CommentText comment = cpr.separateCommentFromContainer(textOfNodes(nodes), in, lineSeparator);

    // format in position 0 to measure it
    TextFlow formatted = cpr.formatComment(comment, pos0Context, new CommentFormattingOptions(
      advice, Integer.MAX_VALUE, trailing, maxTrailing), layoutContext);
    int w = formatted.getWidth();
    if(w <= available) {
      // yay, it will fit as a hanging comment, reformat for this position if the position is not
      // at an even indent multiple.
      int use = current - pos_effectiveIndent;
      if(use > 0) {
        // out = commentContext.create(use);
        formatted = cpr.formatComment(comment, commentContext.create(use), new CommentFormattingOptions(
          advice, Integer.MAX_VALUE, trailing, maxTrailing), layoutContext);
      }
      output.appendText(CharSequences.trimLeft(formatted.getText())); // , true);
    }
    else {
      // Did not fit un-wrapped

      // if output ends with a break, then current is at the leftmost position already, and moving it
      // is not an option. The position is also at an indent multiple, so no need to reposition the comment.
      //
      if(output.endsWithBreak() || output.isEmpty()) {

        // re-format for effective indent
        formatted = cpr.formatComment(
          //
          comment, //
          pos0Context, //
          new CommentFormattingOptions(advice, maxWidth - pos_effectiveIndent, trailing, maxTrailing),
          layoutContext);
      }
      else {
        // re-format for the available space (hanging at current position)
        formatted = cpr.formatComment(//
          comment, //
          commentContext.create(current - pos_effectiveIndent), //
          new CommentFormattingOptions(advice, available, trailing, maxTrailing), layoutContext);

        // if comment formatted for hanging at current does not fit the width (because it
        // has non-breakable content, or starts at an awkward position)
        // then reformat and move the comment to the effective indent.
        if(formatted.getWidth() > maxWidth - pos_effectiveIndent) {

          // ouch, not possible to format it as hanging at current position, must move it to the next line.
          output.appendBreak();

          // re-format for the effective indent position space
          formatted = cpr.formatComment(
            //
            comment, //
            pos0Context, //
            new CommentFormattingOptions(advice, maxWidth - pos_effectiveIndent, trailing, maxTrailing),
            layoutContext);
        }
      }
      output.appendText(CharSequences.trimLeft(formatted.getText())); // , true);
    }

  }
View Full Code Here

        ? new TextRegion(0, text.length())
        : regionToFormat, textString);
  }

  protected ITextFlow.WithText getTextFlow(IFormattingContext context) {
    return new TextFlow(context);
  }
View Full Code Here

              Math.max(0, 1 + CharSequences.lastIndexOf(
                xtextDocument.get(0, xtextDocument.getLength()), "\n", offsetOfNode - 1));

          CommentText commentText = commentProcessor.separateCommentFromContainer(
            issueString, mlContainer.create(posOnLine), "\n");
          TextFlow result = commentProcessor.formatComment(
            commentText, hashContainer.create(posOnLine), new CommentFormattingOptions(
              commentConfigurationProvider.get().getFormatterAdvice(CommentType.SingleLine),
              Integer.MAX_VALUE, 0, 1), formattingContextFactory.create(state, FormattingOption.Format));

          if(!endsWithBreak)
            result.appendBreak();
          String replacement = CharSequences.trimLeft(result.getText()).toString();

          acceptor.accept(
            issue, "Change to # style comment",
            "Changes comment to # style (any trailing logic on last line is moved to separate line", null,
            new ReplacingModification(issue.getOffset(), issue.getLength(), replacement));
View Full Code Here

TOP

Related Classes of com.puppetlabs.xtext.textflow.TextFlow

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.