Beispiel #1
0
  /**
   * Handles having an operator
   *
   * @param std the coding standard to be used
   * @param cs the contents of the string
   * @param buf the buffer where the contents should be added
   * @param parsingUtils helper to get the contents
   * @param i current index
   * @param c current char
   * @return the new index after handling the operator
   */
  private int handleOperator(
      FormatStd std, char[] cs, FastStringBuffer buf, ParsingUtils parsingUtils, int i, char c) {
    // let's discover if it's an unary operator (~ + -)
    boolean isUnaryWithContents = true;

    boolean isUnary = false;
    boolean changeWhitespacesBefore = true;
    if (c == '~' || c == '+' || c == '-') {
      // could be an unary operator...
      String trimmedLastWord = buf.getLastWord().trim();
      isUnary =
          trimmedLastWord.length() == 0 || PySelection.ALL_KEYWORD_TOKENS.contains(trimmedLastWord);

      if (!isUnary) {
        for (char itChar : buf.reverseIterator()) {
          if (itChar == ' ' || itChar == '\t') {
            continue;
          }

          switch (itChar) {
            case '[':
            case '{':
            case '=':
              changeWhitespacesBefore = false;

            case '(':
            case ':':
              isUnaryWithContents = false;

            case '>':
            case '<':

            case '-':
            case '+':
            case '~':

            case '*':
            case '/':
            case '%':
            case '!':
            case '&':
            case '^':
            case '|':
            case ',':
              isUnary = true;
          }
          break;
        }
      } else {
        isUnaryWithContents = buf.length() > 0;
      }
    }

    if (!isUnary) {
      // We don't want to change whitespaces before in a binary operator that is in a new line.
      for (char ch : buf.reverseIterator()) {
        if (!Character.isWhitespace(ch)) {
          break;
        }
        if (ch == '\r' || ch == '\n') {
          changeWhitespacesBefore = false;
          break;
        }
      }
    }

    if (changeWhitespacesBefore) {
      while (buf.length() > 0 && (buf.lastChar() == ' ' || buf.lastChar() == ' ')) {
        buf.deleteLast();
      }
    }

    boolean surroundWithSpaces = std.operatorsWithSpace;

    if (changeWhitespacesBefore) {
      // add spaces before
      if (isUnaryWithContents && surroundWithSpaces) {
        buf.append(' ');
      }
    }

    char localC = c;
    char prev = '\0';
    boolean backOne = true;
    while (isOperatorPart(localC, prev)) {
      buf.append(localC);
      prev = localC;
      i++;
      if (i == cs.length) {
        break;
      }
      localC = cs[i];
      if (localC == '=') {
        // when we get to an assign, we have found a full stmt (with assign) -- e.g.: a \\=  a += a
        // ==
        buf.append(localC);
        backOne = false;
        break;
      }
    }
    if (backOne) {
      i--;
    }

    // add space after only if it's not unary
    if (!isUnary && surroundWithSpaces) {
      buf.append(' ');
    }

    i = parsingUtils.eatWhitespaces(null, i + 1);
    return i;
  }