/**
   * Returns the block balance, i.e. zero if the blocks are balanced at <code>offset</code>, a
   * negative number if there are more closing than opening braces, and a positive number if there
   * are more opening than closing braces.
   *
   * @param document the document
   * @param offset the offset
   * @param partitioning the partitioning
   * @return the block balance
   */
  private static int getBlockBalance(IDocument document, int offset, String partitioning) {
    if (offset < 1) return -1;
    if (offset >= document.getLength()) return 1;

    int begin = offset;
    int end = offset - 1;

    JavaHeuristicScanner scanner = new JavaHeuristicScanner(document);

    while (true) {
      begin = scanner.findOpeningPeer(begin - 1, '{', '}');
      end = scanner.findClosingPeer(end + 1, '{', '}');
      if (begin == -1 && end == -1) return 0;
      if (begin == -1) return -1;
      if (end == -1) return 1;
    }
  }