private boolean formatSourceFile()

in src/main/java/com/spotify/fmt/Formatter.java [140:177]


  private boolean formatSourceFile(
      File file, com.google.googlejavaformat.java.Formatter formatter, Style style) {
    if (file.isDirectory()) {
      if (cfg.verbose()) {
        log.debug("File '" + file + "' is a directory. Skipping.");
      }
      return true;
    }

    if (cfg.verbose()) {
      log.debug("Formatting '" + file + "'.");
    }

    CharSource source = com.google.common.io.Files.asCharSource(file, Charsets.UTF_8);
    try {
      String input = source.read();
      String formatted = formatter.formatSource(input);
      formatted = RemoveUnusedImports.removeUnusedImports(formatted);
      if (!cfg.skipSortingImports()) {
        formatted = ImportOrderer.reorderImports(formatted, style);
      }
      if (!input.equals(formatted)) {
        if (cfg.writeReformattedFiles()) {
          CharSink sink = com.google.common.io.Files.asCharSink(file, Charsets.UTF_8);
          sink.write(formatted);
        }
        nonComplyingFiles.add(file.getAbsolutePath());
      }
      processedFiles.add(file.getAbsolutePath());
      if (processedFiles.size() % 100 == 0) {
        logNumberOfFilesProcessed();
      }
    } catch (com.google.googlejavaformat.java.FormatterException | IOException e) {
      log.error("Failed to format file '" + file + "'.", e);
      return false;
    }
    return true;
  }