Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.palantir.javaformat.java;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Optional.ofNullable;

import com.google.common.io.ByteStreams;
import com.palantir.javaformat.java.JavaFormatterOptions.Style;
Expand All @@ -30,7 +31,6 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -96,12 +96,14 @@ public static void main(String[] args) {
*
* @param args the command-line arguments
*/
public int format(String... args) throws UsageException {
int format(String... args) throws UsageException {
CommandLineOptions parameters = processArgs(args);

if (parameters.version()) {
errWriter.println(versionString());
return 0;
}

if (parameters.help()) {
throw new UsageException();
}
Expand Down Expand Up @@ -144,7 +146,7 @@ private int formatFiles(
Path path = Paths.get(fileName);
String input;
try {
input = new String(Files.readAllBytes(path), UTF_8);
input = Files.readString(path);
inputs.put(path, input);
results.put(path, executorService.submit(new FormatFileCallable(parameters, input, options)));
} catch (IOException e) {
Expand All @@ -153,31 +155,35 @@ private int formatFiles(
}
}

for (Map.Entry<Path, Future<String>> result : results.entrySet()) {
for (var result : results.entrySet()) {
Path path = result.getKey();
String formatted;

try {
formatted = result.getValue().get();
} catch (InterruptedException e) {
errWriter.println(e.getMessage());
allOk = false;
continue;
} catch (ExecutionException e) {
if (e.getCause() instanceof FormatterException) {
for (FormatterDiagnostic diagnostic : ((FormatterException) e.getCause()).diagnostics()) {
var cause = e.getCause();

if (cause instanceof FormatterException formatterException) {
for (FormatterDiagnostic diagnostic : formatterException.diagnostics()) {
errWriter.println(path + ":" + diagnostic);
}
} else {
errWriter.println(path + ": error: "
+ Optional.ofNullable(e.getCause())
.map(Throwable::getMessage)
.orElse("null"));
Optional.ofNullable(e.getCause()).ifPresent(cause -> cause.printStackTrace(errWriter));
+ ofNullable(cause).map(Throwable::getMessage).orElse("null"));

ofNullable(cause).ifPresent(c -> c.printStackTrace(errWriter));
}
allOk = false;
continue;
}

boolean changed = !formatted.equals(inputs.get(path));

if (changed && parameters.setExitIfChanged()) {
anyChanged = true;
}
Expand All @@ -186,11 +192,11 @@ private int formatFiles(
continue; // preserve original file
}
try {
Files.write(path, formatted.getBytes(UTF_8));
Files.writeString(path, formatted);
} catch (IOException e) {
errWriter.println(path + ": could not write file: " + e.getMessage());

allOk = false;
continue;
}
} else if (parameters.dryRun()) {
if (changed) {
Expand Down Expand Up @@ -245,7 +251,7 @@ private static int exitCode(boolean allOk, boolean anyChanged) {
}

/** Parses and validates command-line flags. */
public static CommandLineOptions processArgs(String... args) throws UsageException {
static CommandLineOptions processArgs(String... args) throws UsageException {
CommandLineOptions parameters;
try {
parameters = CommandLineOptionsParser.parse(Arrays.asList(args));
Expand All @@ -255,7 +261,9 @@ public static CommandLineOptions processArgs(String... args) throws UsageExcepti
t.printStackTrace();
throw new UsageException(Objects.requireNonNullElse(t.getMessage(), "null"));
}

int filesToFormat = parameters.files().size();

if (parameters.stdin()) {
filesToFormat++;
}
Expand All @@ -281,6 +289,7 @@ public static CommandLineOptions processArgs(String... args) throws UsageExcepti
if (parameters.dryRun() && parameters.inPlace()) {
throw new UsageException("cannot use --dry-run and --replace at the same time");
}

return parameters;
}
}
Loading