/**
   * Finds a closing parenthesis to the left of <code>position</code> in document, where that
   * parenthesis is only separated by whitespace from <code>position</code>. If no such parenthesis
   * can be found, <code>position</code> is returned.
   *
   * @param scanner the java heuristic scanner set up on the document
   * @param position the first character position in <code>document</code> to be considered
   * @return the position of a closing parenthesis left to <code>position</code> separated only by
   *     whitespace, or <code>position</code> if no parenthesis can be found
   */
  private static int findClosingParenToLeft(JavaHeuristicScanner scanner, int position) {
    if (position < 1) return position;

    if (scanner.previousToken(position - 1, JavaHeuristicScanner.UNBOUND) == Symbols.TokenRPAREN)
      return scanner.getPosition() + 1;
    return position;
  }
  /**
   * Skips the scope opened by <code>token</code>.
   *
   * @param scanner the scanner
   * @param start the start position
   * @param token the token
   * @return the position after the scope or <code>JavaHeuristicScanner.NOT_FOUND</code>
   */
  private static int skipScope(JavaHeuristicScanner scanner, int start, int token) {
    int openToken = token;
    int closeToken;
    switch (token) {
      case Symbols.TokenLPAREN:
        closeToken = Symbols.TokenRPAREN;
        break;
      case Symbols.TokenLBRACKET:
        closeToken = Symbols.TokenRBRACKET;
        break;
      case Symbols.TokenLBRACE:
        closeToken = Symbols.TokenRBRACE;
        break;
      default:
        Assert.isTrue(false);
        return -1; // dummy
    }

    int depth = 1;
    int p = start;

    while (true) {
      int tok = scanner.nextToken(p, JavaHeuristicScanner.UNBOUND);
      p = scanner.getPosition();

      if (tok == openToken) {
        depth++;
      } else if (tok == closeToken) {
        depth--;
        if (depth == 0) return p + 1;
      } else if (tok == Symbols.TokenEOF) {
        return JavaHeuristicScanner.NOT_FOUND;
      }
    }
  }
  private int getPeerPosition(IDocument document, DocumentCommand command) {
    if (document.getLength() == 0) return 0;
    /*
     * Search for scope closers in the pasted text and find their opening peers
     * in the document.
     */
    Document pasted = new Document(command.text);
    installJavaStuff(pasted);
    int firstPeer = command.offset;

    JavaHeuristicScanner pScanner = new JavaHeuristicScanner(pasted);
    JavaHeuristicScanner dScanner = new JavaHeuristicScanner(document);

    // add scope relevant after context to peer search
    int afterToken =
        dScanner.nextToken(command.offset + command.length, JavaHeuristicScanner.UNBOUND);
    try {
      switch (afterToken) {
        case Symbols.TokenRBRACE:
          pasted.replace(pasted.getLength(), 0, "}"); // $NON-NLS-1$
          break;
        case Symbols.TokenRPAREN:
          pasted.replace(pasted.getLength(), 0, ")"); // $NON-NLS-1$
          break;
        case Symbols.TokenRBRACKET:
          pasted.replace(pasted.getLength(), 0, "]"); // $NON-NLS-1$
          break;
      }
    } catch (BadLocationException e) {
      // cannot happen
      Assert.isTrue(false);
    }

    int pPos = 0; // paste text position (increasing from 0)
    int dPos = Math.max(0, command.offset - 1); // document position (decreasing from paste offset)
    while (true) {
      int token = pScanner.nextToken(pPos, JavaHeuristicScanner.UNBOUND);
      pPos = pScanner.getPosition();
      switch (token) {
        case Symbols.TokenLBRACE:
        case Symbols.TokenLBRACKET:
        case Symbols.TokenLPAREN:
          pPos = skipScope(pScanner, pPos, token);
          if (pPos == JavaHeuristicScanner.NOT_FOUND) return firstPeer;
          break; // closed scope -> keep searching
        case Symbols.TokenRBRACE:
          int peer = dScanner.findOpeningPeer(dPos, '{', '}');
          dPos = peer - 1;
          if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer;
          firstPeer = peer;
          break; // keep searching
        case Symbols.TokenRBRACKET:
          peer = dScanner.findOpeningPeer(dPos, '[', ']');
          dPos = peer - 1;
          if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer;
          firstPeer = peer;
          break; // keep searching
        case Symbols.TokenRPAREN:
          peer = dScanner.findOpeningPeer(dPos, '(', ')');
          dPos = peer - 1;
          if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer;
          firstPeer = peer;
          break; // keep searching
        case Symbols.TokenCASE:
        case Symbols.TokenDEFAULT:
          JavaIndenter indenter = new JavaIndenter(document, dScanner, fProject);
          peer = indenter.findReferencePosition(dPos, false, false, false, true);
          if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer;
          firstPeer = peer;
          break; // keep searching

        case Symbols.TokenEOF:
          return firstPeer;
        default:
          // keep searching
      }
    }
  }