Ejemplo n.º 1
0
 public FastStringBuffer insertN(int pos, char c, int repetitions) {
   FastStringBuffer other = new FastStringBuffer(repetitions);
   other.appendN(c, repetitions);
   insert(pos, other);
   return this;
 }
Ejemplo n.º 2
0
  /**
   * Formats the contents for when a parenthesis is found (so, go until the closing parens and
   * format it accordingly)
   *
   * @param throwSyntaxError
   * @throws SyntaxErrorException
   */
  private int formatForPar(
      final ParsingUtils parsingUtils,
      final char[] cs,
      final int i,
      final FormatStd std,
      final FastStringBuffer buf,
      final int parensLevel,
      final String delimiter,
      boolean throwSyntaxError)
      throws SyntaxErrorException {
    char c = ' ';
    FastStringBuffer locBuf = new FastStringBuffer();

    int j = i + 1;
    int start = j;
    int end = start;
    while (j < cs.length && (c = cs[j]) != ')') {

      j++;

      if (c == '\'' || c == '"') { // ignore comments or multiline comments...
        j = parsingUtils.eatLiterals(null, j - 1, std.trimMultilineLiterals) + 1;
        end = j;

      } else if (c == '#') {
        j = parsingUtils.eatComments(null, j - 1) + 1;
        end = j;

      } else if (c == '(') { // open another par.
        if (end > start) {
          locBuf.append(cs, start, end - start);
          start = end;
        }
        j =
            formatForPar(
                    parsingUtils,
                    cs,
                    j - 1,
                    std,
                    locBuf,
                    parensLevel + 1,
                    delimiter,
                    throwSyntaxError)
                + 1;
        start = j;

      } else {
        end = j;
      }
    }
    if (end > start) {
      locBuf.append(cs, start, end - start);
      start = end;
    }

    if (c == ')') {
      // Now, when a closing parens is found, let's see the contents of the line where that parens
      // was found
      // and if it's only whitespaces, add all those whitespaces (to handle the following case:
      // a(a,
      //  b
      //   ) <-- we don't want to change this one.
      char c1;
      FastStringBuffer buf1 = new FastStringBuffer();

      if (locBuf.indexOf('\n') != -1 || locBuf.indexOf('\r') != -1) {
        for (int k = locBuf.length();
            k > 0 && (c1 = locBuf.charAt(k - 1)) != '\n' && c1 != '\r';
            k--) {
          buf1.insert(0, c1);
        }
      }

      String formatStr =
          formatStr(trim(locBuf).toString(), std, parensLevel, delimiter, throwSyntaxError);
      FastStringBuffer formatStrBuf = trim(new FastStringBuffer(formatStr, 10));

      String closing = ")";
      if (buf1.length() > 0 && PySelection.containsOnlyWhitespaces(buf1.toString())) {
        formatStrBuf.append(buf1);

      } else if (std.parametersWithSpace) {
        closing = " )";
      }

      if (std.parametersWithSpace) {
        if (formatStrBuf.length() == 0) {
          buf.append("()");

        } else {
          buf.append("( ");
          buf.append(formatStrBuf);
          buf.append(closing);
        }
      } else {
        buf.append('(');
        buf.append(formatStrBuf);
        buf.append(closing);
      }
      return j;
    } else {
      if (throwSyntaxError) {
        throw new SyntaxErrorException("No closing ')' found.");
      }
      // we found no closing parens but we finished looking already, so, let's just add anything
      // without
      // more formatting...
      buf.append('(');
      buf.append(locBuf);
      return j;
    }
  }