com.google.googlejavaformat.Newlines Java Examples

The following examples show how to use com.google.googlejavaformat.Newlines. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RemoveUnusedImports.java    From google-java-format with Apache License 2.0 7 votes vote down vote up
/** Construct replacements to fix unused imports. */
private static RangeMap<Integer, String> buildReplacements(
    String contents,
    JCCompilationUnit unit,
    Set<String> usedNames,
    Multimap<String, Range<Integer>> usedInJavadoc) {
  RangeMap<Integer, String> replacements = TreeRangeMap.create();
  for (JCImport importTree : unit.getImports()) {
    String simpleName = getSimpleName(importTree);
    if (!isUnused(unit, usedNames, usedInJavadoc, importTree, simpleName)) {
      continue;
    }
    // delete the import
    int endPosition = importTree.getEndPosition(unit.endPositions);
    endPosition = Math.max(CharMatcher.isNot(' ').indexIn(contents, endPosition), endPosition);
    String sep = Newlines.guessLineSeparator(contents);
    if (endPosition + sep.length() < contents.length()
        && contents.subSequence(endPosition, endPosition + sep.length()).toString().equals(sep)) {
      endPosition += sep.length();
    }
    replacements.put(Range.closedOpen(importTree.getStartPosition(), endPosition), "");
  }
  return replacements;
}
 
Example #2
Source File: Formatter.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
/**
 * Emit a list of {@link Replacement}s to convert from input to output.
 *
 * @param input the input compilation unit
 * @param characterRanges the character ranges to reformat
 * @return a list of {@link Replacement}s, sorted from low index to high index, without overlaps
 * @throws FormatterException if the input string cannot be parsed
 */
public ImmutableList<Replacement> getFormatReplacements(
    String input, Collection<Range<Integer>> characterRanges) throws FormatterException {
  JavaInput javaInput = new JavaInput(input);

  // TODO(cushon): this is only safe because the modifier ordering doesn't affect whitespace,
  // and doesn't change the replacements that are output. This is not true in general for
  // 'de-linting' changes (e.g. import ordering).
  javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges);

  String lineSeparator = Newlines.guessLineSeparator(input);
  JavaOutput javaOutput =
      new JavaOutput(lineSeparator, javaInput, new JavaCommentsHelper(lineSeparator, options));
  try {
    format(javaInput, javaOutput, options);
  } catch (FormattingError e) {
    throw new FormatterException(e.diagnostics());
  }
  RangeSet<Integer> tokenRangeSet = javaInput.characterRangesToTokenRanges(characterRanges);
  return javaOutput.getFormatReplacements(tokenRangeSet);
}
 
Example #3
Source File: Formatter.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges.
 */
public static RangeSet<Integer> lineRangesToCharRanges(
        String input, RangeSet<Integer> lineRanges) {
    List<Integer> lines = new ArrayList<>();
    Iterators.addAll(lines, Newlines.lineOffsetIterator(input));
    lines.add(input.length() + 1);

    final RangeSet<Integer> characterRanges = TreeRangeSet.create();
    for (Range<Integer> lineRange :
            lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) {
        int lineStart = lines.get(lineRange.lowerEndpoint());
        // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines
        // as empty ranges is convenient.
        int lineEnd = lines.get(lineRange.upperEndpoint()) - 1;
        Range<Integer> range = Range.closedOpen(lineStart, lineEnd);
        characterRanges.add(range);
    }
    return characterRanges;
}
 
Example #4
Source File: Formatter.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Emit a list of {@link Replacement}s to convert from input to output.
 *
 * @param input           the input compilation unit
 * @param characterRanges the character ranges to reformat
 * @return a list of {@link Replacement}s, sorted from low index to high index, without overlaps
 * @throws FormatterException if the input string cannot be parsed
 */
public ImmutableList<Replacement> getFormatReplacements(
        String input, Collection<Range<Integer>> characterRanges) throws FormatterException {
    JavaInput javaInput = new JavaInput(input);

    // TODO(cushon): this is only safe because the modifier ordering doesn't affect whitespace,
    // and doesn't change the replacements that are output. This is not true in general for
    // 'de-linting' changes (e.g. import ordering).
    javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges);

    String lineSeparator = Newlines.guessLineSeparator(input);
    JavaOutput javaOutput =
            new JavaOutput(lineSeparator, javaInput, new JavaCommentsHelper(lineSeparator, options));
    try {
        format(javaInput, javaOutput, options);
    } catch (FormattingError e) {
        throw new FormatterException(e.diagnostics());
    }
    RangeSet<Integer> tokenRangeSet = javaInput.characterRangesToTokenRanges(characterRanges);
    return javaOutput.getFormatReplacements(tokenRangeSet);
}
 
Example #5
Source File: Formatter.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
/**
 * Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges.
 */
public static RangeSet<Integer> lineRangesToCharRanges(
    String input, RangeSet<Integer> lineRanges) {
  List<Integer> lines = new ArrayList<>();
  Iterators.addAll(lines, Newlines.lineOffsetIterator(input));
  lines.add(input.length() + 1);

  final RangeSet<Integer> characterRanges = TreeRangeSet.create();
  for (Range<Integer> lineRange :
      lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) {
    int lineStart = lines.get(lineRange.lowerEndpoint());
    // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines
    // as empty ranges is convenient.
    int lineEnd = lines.get(lineRange.upperEndpoint()) - 1;
    Range<Integer> range = Range.closedOpen(lineStart, lineEnd);
    characterRanges.add(range);
  }
  return characterRanges;
}
 
Example #6
Source File: JavaCommentsHelper.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
@Override
public String rewrite(Tok tok, int maxWidth, int column0) {
  if (!tok.isComment()) {
    return tok.getOriginalText();
  }
  String text = tok.getOriginalText();
  if (tok.isJavadocComment() && options.formatJavadoc()) {
    text = JavadocFormatter.formatJavadoc(text, column0);
  }
  List<String> lines = new ArrayList<>();
  Iterator<String> it = Newlines.lineIterator(text);
  while (it.hasNext()) {
    lines.add(CharMatcher.whitespace().trimTrailingFrom(it.next()));
  }
  if (tok.isSlashSlashComment()) {
    return indentLineComments(lines, column0);
  } else if (javadocShaped(lines)) {
    return indentJavadoc(lines, column0);
  } else {
    return preserveIndentation(lines, column0);
  }
}
 
Example #7
Source File: JavaCommentsHelper.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
@Override
public String rewrite(Tok tok, int maxWidth, int column0) {
    if (!tok.isComment()) {
        return tok.getOriginalText();
    }
    String text = tok.getOriginalText();
    if (tok.isJavadocComment()) {
        text = JavadocFormatter.formatJavadoc(text, column0, options);
    }
    List<String> lines = new ArrayList<>();
    Iterator<String> it = Newlines.lineIterator(text);
    while (it.hasNext()) {
        lines.add(CharMatcher.whitespace().trimTrailingFrom(it.next()));
    }
    if (tok.isSlashSlashComment()) {
        return indentLineComments(lines, column0);
    } else if (javadocShaped(lines)) {
        return indentJavadoc(lines, column0);
    } else {
        return preserveIndentation(lines, column0);
    }
}
 
Example #8
Source File: JavaCommentsHelper.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public String rewrite(Tok tok, int maxWidth, int column0) {
    if (!tok.isComment()) {
        return tok.getOriginalText();
    }
    String text = tok.getOriginalText();
    if (tok.isJavadocComment()) {
        text = JavadocFormatter.formatJavadoc(text, column0, options);
    }
    List<String> lines = new ArrayList<>();
    Iterator<String> it = Newlines.lineIterator(text);
    while (it.hasNext()) {
        lines.add(CharMatcher.whitespace().trimTrailingFrom(it.next()));
    }
    if (tok.isSlashSlashComment()) {
        return indentLineComments(lines, column0);
    } else if (javadocShaped(lines)) {
        return indentJavadoc(lines, column0);
    } else {
        return preserveIndentation(lines, column0);
    }
}
 
Example #9
Source File: Formatter.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Converts zero-indexed, [closed, open) line ranges in the given source file to character ranges.
 */
public static RangeSet<Integer> lineRangesToCharRanges(
        String input, RangeSet<Integer> lineRanges) {
    List<Integer> lines = new ArrayList<>();
    Iterators.addAll(lines, Newlines.lineOffsetIterator(input));
    lines.add(input.length() + 1);

    final RangeSet<Integer> characterRanges = TreeRangeSet.create();
    for (Range<Integer> lineRange :
            lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) {
        int lineStart = lines.get(lineRange.lowerEndpoint());
        // Exclude the trailing newline. This isn't strictly necessary, but handling blank lines
        // as empty ranges is convenient.
        int lineEnd = lines.get(lineRange.upperEndpoint()) - 1;
        Range<Integer> range = Range.closedOpen(lineStart, lineEnd);
        characterRanges.add(range);
    }
    return characterRanges;
}
 
Example #10
Source File: Formatter.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Emit a list of {@link Replacement}s to convert from input to output.
 *
 * @param input           the input compilation unit
 * @param characterRanges the character ranges to reformat
 * @return a list of {@link Replacement}s, sorted from low index to high index, without overlaps
 * @throws FormatterException if the input string cannot be parsed
 */
public ImmutableList<Replacement> getFormatReplacements(
        String input, Collection<Range<Integer>> characterRanges) throws FormatterException {
    JavaInput javaInput = new JavaInput(input);

    // TODO(cushon): this is only safe because the modifier ordering doesn't affect whitespace,
    // and doesn't change the replacements that are output. This is not true in general for
    // 'de-linting' changes (e.g. import ordering).
    javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges);

    String lineSeparator = Newlines.guessLineSeparator(input);
    JavaOutput javaOutput =
            new JavaOutput(lineSeparator, javaInput, new JavaCommentsHelper(lineSeparator, options));
    try {
        format(javaInput, javaOutput, options);
    } catch (FormattingError e) {
        throw new FormatterException(e.diagnostics());
    }
    RangeSet<Integer> tokenRangeSet = javaInput.characterRangesToTokenRanges(characterRanges);
    return javaOutput.getFormatReplacements(tokenRangeSet);
}
 
Example #11
Source File: ImportOrderer.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
private ImportOrderer(String text, ImmutableList<Tok> toks, Style style) {
  this.text = text;
  this.toks = toks;
  this.lineSeparator = Newlines.guessLineSeparator(text);
  if (style.equals(Style.GOOGLE)) {
    this.importComparator = GOOGLE_IMPORT_COMPARATOR;
    this.shouldInsertBlankLineFn = ImportOrderer::shouldInsertBlankLineGoogle;
  } else if (style.equals(Style.AOSP)) {
    this.importComparator = AOSP_IMPORT_COMPARATOR;
    this.shouldInsertBlankLineFn = ImportOrderer::shouldInsertBlankLineAosp;
  } else {
    throw new IllegalArgumentException("Unsupported code style: " + style);
  }
}
 
Example #12
Source File: StringWrapper.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/** Returns true if any lines in the given Java source exceed the column limit. */
private static boolean longLines(int columnLimit, String input) {
  // TODO(cushon): consider adding Newlines.lineIterable?
  Iterator<String> it = Newlines.lineIterator(input);
  while (it.hasNext()) {
    String line = it.next();
    if (line.length() > columnLimit) {
      return true;
    }
  }
  return false;
}
 
Example #13
Source File: JavaInput.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static int updateColumn(int columnI, String originalTokText) {
    Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText));
    if (last > 0) {
        columnI = originalTokText.length() - last;
    } else {
        columnI += originalTokText.length();
    }
    return columnI;
}
 
Example #14
Source File: JavaInput.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
private static int updateColumn(int columnI, String originalTokText) {
  Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText));
  if (last > 0) {
    columnI = originalTokText.length() - last;
  } else {
    columnI += originalTokText.length();
  }
  return columnI;
}
 
Example #15
Source File: JavaInput.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private static int updateColumn(int columnI, String originalTokText) {
    Integer last = Iterators.getLast(Newlines.lineOffsetIterator(originalTokText));
    if (last > 0) {
        columnI = originalTokText.length() - last;
    } else {
        columnI += originalTokText.length();
    }
    return columnI;
}
 
Example #16
Source File: JavaInput.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isNewline() {
  return Newlines.isNewline(text);
}
 
Example #17
Source File: FormatterIntegrationTest.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
public FormatterIntegrationTest(String name, String input, String expected) {
  this.name = name;
  this.input = input;
  this.expected = expected;
  this.separator = Newlines.getLineEnding(expected);
}
 
Example #18
Source File: JavaInput.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isNewline() {
    return Newlines.isNewline(text);
}
 
Example #19
Source File: ImportOrderer.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
private String reorderImports() throws FormatterException {
  int firstImportStart;
  Optional<Integer> maybeFirstImport = findIdentifier(0, IMPORT_OR_CLASS_START);
  if (!maybeFirstImport.isPresent() || !tokenAt(maybeFirstImport.get()).equals("import")) {
    // No imports, so nothing to do.
    return text;
  }
  firstImportStart = maybeFirstImport.get();
  int unindentedFirstImportStart = unindent(firstImportStart);

  ImportsAndIndex imports = scanImports(firstImportStart);
  int afterLastImport = imports.index;

  // Make sure there are no more imports before the next class (etc) definition.
  Optional<Integer> maybeLaterImport = findIdentifier(afterLastImport, IMPORT_OR_CLASS_START);
  if (maybeLaterImport.isPresent() && tokenAt(maybeLaterImport.get()).equals("import")) {
    throw new FormatterException("Imports not contiguous (perhaps a comment separates them?)");
  }

  StringBuilder result = new StringBuilder();
  String prefix = tokString(0, unindentedFirstImportStart);
  result.append(prefix);
  if (!prefix.isEmpty() && Newlines.getLineEnding(prefix) == null) {
    result.append(lineSeparator).append(lineSeparator);
  }
  result.append(reorderedImportsString(imports.imports));

  List<String> tail = new ArrayList<>();
  tail.add(CharMatcher.whitespace().trimLeadingFrom(tokString(afterLastImport, toks.size())));
  if (!toks.isEmpty()) {
    Tok lastTok = getLast(toks);
    int tailStart = lastTok.getPosition() + lastTok.length();
    tail.add(text.substring(tailStart));
  }
  if (tail.stream().anyMatch(s -> !s.isEmpty())) {
    result.append(lineSeparator);
    tail.forEach(result::append);
  }

  return result.toString();
}
 
Example #20
Source File: JavaOutput.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
@Override
public void append(String text, Range<Integer> range) {
  if (!range.isEmpty()) {
    boolean sawNewlines = false;
    // Skip over input line we've passed.
    int iN = javaInput.getLineCount();
    while (iLine < iN
        && (javaInput.getRanges(iLine).isEmpty()
            || javaInput.getRanges(iLine).upperEndpoint() <= range.lowerEndpoint())) {
      if (javaInput.getRanges(iLine).isEmpty()) {
        // Skipped over a blank line.
        sawNewlines = true;
      }
      ++iLine;
    }
    /*
     * Output blank line if we've called {@link OpsBuilder#blankLine}{@code (true)} here, or if
     * there's a blank line here and it's a comment.
     */
    BlankLineWanted wanted = blankLines.getOrDefault(lastK, BlankLineWanted.NO);
    if (isComment(text) ? sawNewlines : wanted.wanted().orElse(sawNewlines)) {
      ++newlinesPending;
    }
  }
  if (Newlines.isNewline(text)) {
    /*
     * Don't update range information, and swallow extra newlines. The case below for '\n' is for
     * block comments.
     */
    if (newlinesPending == 0) {
      ++newlinesPending;
    }
    spacesPending = new StringBuilder();
  } else {
    boolean rangesSet = false;
    int textN = text.length();
    for (int i = 0; i < textN; i++) {
      char c = text.charAt(i);
      switch (c) {
        case ' ':
          spacesPending.append(' ');
          break;
        case '\t':
          spacesPending.append('\t');
          break;
        case '\r':
          if (i + 1 < text.length() && text.charAt(i + 1) == '\n') {
            i++;
          }
          // falls through
        case '\n':
          spacesPending = new StringBuilder();
          ++newlinesPending;
          break;
        default:
          while (newlinesPending > 0) {
            // drop leading blank lines
            if (!mutableLines.isEmpty() || lineBuilder.length() > 0) {
              mutableLines.add(lineBuilder.toString());
            }
            lineBuilder = new StringBuilder();
            rangesSet = false;
            --newlinesPending;
          }
          if (spacesPending.length() > 0) {
            lineBuilder.append(spacesPending);
            spacesPending = new StringBuilder();
          }
          lineBuilder.append(c);
          if (!range.isEmpty()) {
            if (!rangesSet) {
              while (ranges.size() <= mutableLines.size()) {
                ranges.add(Formatter.EMPTY_RANGE);
              }
              ranges.set(mutableLines.size(), union(ranges.get(mutableLines.size()), range));
              rangesSet = true;
            }
          }
      }
    }
  }
  if (!range.isEmpty()) {
    lastK = range.upperEndpoint();
  }
}
 
Example #21
Source File: JavaOutput.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
@Override
public void append(String text, Range<Integer> range) {
    if (!range.isEmpty()) {
        boolean sawNewlines = false;
        // Skip over input line we've passed.
        int iN = javaInput.getLineCount();
        while (iLine < iN
                && (javaInput.getRange1s(iLine).isEmpty()
                || javaInput.getRange1s(iLine).upperEndpoint() <= range.lowerEndpoint())) {
            if (javaInput.getRanges(iLine).isEmpty()) {
                // Skipped over a blank line.
                sawNewlines = true;
            }
            ++iLine;
        }
  /*
   * Output blank line if we've called {@link OpsBuilder#blankLine}{@code (true)} here, or if
   * there's a blank line here and it's a comment.
   */
        BlankLineWanted wanted = firstNonNull(blankLines.get(lastK), BlankLineWanted.NO);
        if (isComment(text) ? sawNewlines : wanted.wanted().or(sawNewlines)) {
            ++newlinesPending;
        }
    }
    if (Newlines.isNewline(text)) {
  /*
   * Don't update range information, and swallow extra newlines. The case below for '\n' is for
   * block comments.
   */
        if (newlinesPending == 0) {
            ++newlinesPending;
        }
        spacesPending = 0;
    } else {
        boolean range0sSet = false;
        boolean rangesSet = false;
        int textN = text.length();
        for (int i = 0; i < textN; i++) {
            char c = text.charAt(i);
            switch (c) {
                case ' ':
                    ++spacesPending;
                    break;
                case '\r':
                    if (i + 1 < text.length() && text.charAt(i + 1) == '\n') {
                        i++;
                    }
                    // falls through
                case '\n':
                    spacesPending = 0;
                    ++newlinesPending;
                    break;
                default:
                    while (newlinesPending > 0) {
                        // drop leading blank lines
                        if (!mutableLines.isEmpty() || lineBuilder.length() > 0) {
                            mutableLines.add(lineBuilder.toString());
                        }
                        lineBuilder = new StringBuilder();
                        rangesSet = false;
                        --newlinesPending;
                    }
                    while (spacesPending > 0) {
                        lineBuilder.append(' ');
                        --spacesPending;
                    }
                    lineBuilder.append(c);
                    if (!range.isEmpty()) {
                        if (!range0sSet) {
                            if (!range.isEmpty()) {
                                while (range0s.size() <= mutableLines.size()) {
                                    range0s.add(Formatter.EMPTY_RANGE);
                                }
                                range0s.set(mutableLines.size(), union(range0s.get(mutableLines.size()), range));
                                range0sSet = true;
                            }
                        }
                        if (!rangesSet) {
                            while (ranges.size() <= mutableLines.size()) {
                                ranges.add(Formatter.EMPTY_RANGE);
                            }
                            ranges.set(mutableLines.size(), union(ranges.get(mutableLines.size()), range));
                            rangesSet = true;
                        }
                    }
            }
        }
        // TODO(jdd): Move others down here. Use common method for these.
        if (!range.isEmpty()) {
            while (range1s.size() <= mutableLines.size()) {
                range1s.add(Formatter.EMPTY_RANGE);
            }
            range1s.set(mutableLines.size(), union(range1s.get(mutableLines.size()), range));
        }
    }
    if (!range.isEmpty()) {
        lastK = range.upperEndpoint();
    }
}
 
Example #22
Source File: ImportOrderer.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private ImportOrderer(String text, ImmutableList<Tok> toks) throws FormatterException {
    this.text = text;
    this.toks = toks;
    this.lineSeparator = Newlines.guessLineSeparator(text);
}
 
Example #23
Source File: JavaOutput.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void append(String text, Range<Integer> range) {
    if (!range.isEmpty()) {
        boolean sawNewlines = false;
        // Skip over input line we've passed.
        int iN = javaInput.getLineCount();
        while (iLine < iN
                && (javaInput.getRange1s(iLine).isEmpty()
                || javaInput.getRange1s(iLine).upperEndpoint() <= range.lowerEndpoint())) {
            if (javaInput.getRanges(iLine).isEmpty()) {
                // Skipped over a blank line.
                sawNewlines = true;
            }
            ++iLine;
        }
  /*
   * Output blank line if we've called {@link OpsBuilder#blankLine}{@code (true)} here, or if
   * there's a blank line here and it's a comment.
   */
        BlankLineWanted wanted = firstNonNull(blankLines.get(lastK), BlankLineWanted.NO);
        if (isComment(text) ? sawNewlines : wanted.wanted().or(sawNewlines)) {
            ++newlinesPending;
        }
    }
    if (Newlines.isNewline(text)) {
  /*
   * Don't update range information, and swallow extra newlines. The case below for '\n' is for
   * block comments.
   */
        if (newlinesPending == 0) {
            ++newlinesPending;
        }
        spacesPending = 0;
    } else {
        boolean range0sSet = false;
        boolean rangesSet = false;
        int textN = text.length();
        for (int i = 0; i < textN; i++) {
            char c = text.charAt(i);
            switch (c) {
                case ' ':
                    ++spacesPending;
                    break;
                case '\r':
                    if (i + 1 < text.length() && text.charAt(i + 1) == '\n') {
                        i++;
                    }
                    // falls through
                case '\n':
                    spacesPending = 0;
                    ++newlinesPending;
                    break;
                default:
                    while (newlinesPending > 0) {
                        // drop leading blank lines
                        if (!mutableLines.isEmpty() || lineBuilder.length() > 0) {
                            mutableLines.add(lineBuilder.toString());
                        }
                        lineBuilder = new StringBuilder();
                        rangesSet = false;
                        --newlinesPending;
                    }
                    while (spacesPending > 0) {
                        lineBuilder.append(' ');
                        --spacesPending;
                    }
                    lineBuilder.append(c);
                    if (!range.isEmpty()) {
                        if (!range0sSet) {
                            if (!range.isEmpty()) {
                                while (range0s.size() <= mutableLines.size()) {
                                    range0s.add(Formatter.EMPTY_RANGE);
                                }
                                range0s.set(mutableLines.size(), union(range0s.get(mutableLines.size()), range));
                                range0sSet = true;
                            }
                        }
                        if (!rangesSet) {
                            while (ranges.size() <= mutableLines.size()) {
                                ranges.add(Formatter.EMPTY_RANGE);
                            }
                            ranges.set(mutableLines.size(), union(ranges.get(mutableLines.size()), range));
                            rangesSet = true;
                        }
                    }
            }
        }
        // TODO(jdd): Move others down here. Use common method for these.
        if (!range.isEmpty()) {
            while (range1s.size() <= mutableLines.size()) {
                range1s.add(Formatter.EMPTY_RANGE);
            }
            range1s.set(mutableLines.size(), union(range1s.get(mutableLines.size()), range));
        }
    }
    if (!range.isEmpty()) {
        lastK = range.upperEndpoint();
    }
}
 
Example #24
Source File: JavaInput.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isNewline() {
    return Newlines.isNewline(text);
}
 
Example #25
Source File: ImportOrderer.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private ImportOrderer(String text, ImmutableList<Tok> toks) throws FormatterException {
    this.text = text;
    this.toks = toks;
    this.lineSeparator = Newlines.guessLineSeparator(text);
}