Example #1
0
  /**
   * Inserts token location (beginLine, beginColumn, endLine, endColumn) information into the
   * NodeToken. Takes into account line-wrap. Does not update curLine and curColumn.
   */
  private void placeToken(NodeToken n, int line, int column) {
    int length = n.tokenImage.length();

    //
    // Find beginning of token.  Only line-wrap for single-line tokens
    //
    if (!lineWrap || n.tokenImage.indexOf('\n') != -1 || column + length <= wrapWidth)
      n.beginColumn = column;
    else {
      ++line;
      column = curIndent + indentAmt + 1;
      n.beginColumn = column;
    }

    n.beginLine = line;

    //
    // Find end of token; don't count \n if it's the last character
    //
    for (int i = 0; i < length; ++i) {
      if (n.tokenImage.charAt(i) == '\n' && i < length - 1) {
        ++line;
        column = 1;
      } else ++column;
    }

    n.endLine = line;
    n.endColumn = column;
  }
Example #2
0
  /**
   * Executes the commands waiting in the command queue, then inserts the proper location
   * information into the current NodeToken.
   *
   * <p>If there are any special tokens preceding this token, they will be given the current
   * location information. The token will follow on the next line, at the proper indentation level.
   * If this is not the behavior you want from special tokens, feel free to modify this method.
   */
  public void visit(NodeToken n) {
    for (Enumeration<FormatCommand> e = cmdQueue.elements(); e.hasMoreElements(); ) {
      FormatCommand cmd = e.nextElement();
      switch (cmd.getCommand()) {
        case FormatCommand.FORCE:
          curLine += cmd.getNumCommands();
          curColumn = curIndent + 1;
          break;
        case FormatCommand.INDENT:
          curIndent += indentAmt * cmd.getNumCommands();
          break;
        case FormatCommand.OUTDENT:
          if (curIndent >= indentAmt) curIndent -= indentAmt * cmd.getNumCommands();
          break;
        case FormatCommand.SPACE:
          curColumn += cmd.getNumCommands();
          break;
        default:
          throw new TreeFormatterException("Invalid value in command queue.");
      }
    }

    cmdQueue.removeAllElements();

    //
    // Handle all special tokens preceding this NodeToken
    //
    if (n.numSpecials() > 0)
      for (Enumeration<NodeToken> e = n.specialTokens.elements(); e.hasMoreElements(); ) {
        NodeToken special = e.nextElement();

        //
        // -Place the token.
        // -Move cursor to next line after the special token.
        // -Don't update curColumn--want to keep current indent level.
        //
        placeToken(special, curLine, curColumn);
        curLine = special.endLine + 1;
      }

    placeToken(n, curLine, curColumn);
    curLine = n.endLine;
    curColumn = n.endColumn;
  }