Java Code Examples for com.google.googlejavaformat.Newlines#guessLineSeparator()

The following examples show how to use com.google.googlejavaformat.Newlines#guessLineSeparator() . 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 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 3
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 4
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 5
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 6
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);
}
 
Example 7
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);
}