Skip to content

API for dry-run mode #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alexkleiman opened this issue Oct 12, 2017 · 3 comments
Closed

API for dry-run mode #210

alexkleiman opened this issue Oct 12, 2017 · 3 comments

Comments

@alexkleiman
Copy link

The dry-mode mode introduced in #105 is a much-appreciated addition to google-java-format. Thank you for taking the time to add this option!

An API to accompany the CLI option for a dry-run mode would be useful as well. This would have advantages, as an API method would:

  • Eliminate the need to read the formatted and unformatted versions of a file into memory
  • Eliminate the need to perform string comparisons on these two versions
  • Potentially eliminate the need to format an entire file, as a file with a formatted error on line 1 doesn't need to be examined any further.
  • Allow Gradle code and plugins to run google-java-format in validation mode without spinning up a new JVM. When running google-java-format once per Gradle module, this can make a very large difference in efficiency.

Now, this would only be a benefit if the API call didn't do all of this under the hood. Save for obviating the need to spin up a new JVM, I wouldn't expect the API to display any additional efficiencies on it's first implementation, but one can always dream for the future, right?

One additional consideration: from an implementation perspective, it's easier for Gradle plugins to place the google-java-format library on the runtime classpath and call its API than it is to make a CLI call. Using the API also enables users to easily specify the version of google-java-format they want the Gradle plugin to use, as is done with the gradle-errorprone-plugin.

@alexkleiman
Copy link
Author

One option for an API would be to add public boolean tryFormatSource() methods to the com.google.googlejavaformat.java.Formatter class.

@cushon
Copy link
Collaborator

cushon commented Oct 13, 2017

While it's true that a built-in implementation could theoretically be made faster than input.equals(formatSource(input)), it's very unlikely that's going to happen. The interesting parts of the formatter (parsing, the actual formatting) operate on the entire file and do not have a streaming mode. Changing that would add significant implementation complexity, and the current performance seems to be fine in practice.

Can you provide more detail about the issue with gradle? If you're using the API, does the input.equals(formatSource(input)) approach not work?

@alexkleiman
Copy link
Author

input.equals(formatSource(input)) works well, and is not really any more inconvenient than a dedicated API method. Given what you've said about the interesting parts of the formatter not having a streaming mode, I'll go ahead and close this request.

Thanks for the info on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants