Skip to content
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

Break: rename api->spi, remove JavaFormatterOptions and expose new method #47

Merged
merged 10 commits into from
Oct 22, 2019
Merged
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-47.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: break
break:
description: Expose `formatSourceReflowStringsAndFixImports` from the API, which
formats a whole file returning the formatted file as a String, and remove JavaFormatterOptions altogether from the API.
links:
- https://github.com/palantir/palantir-java-format/pull/47
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import com.intellij.util.IncorrectOperationException;
import com.palantir.javaformat.java.FormatterException;
import com.palantir.javaformat.java.FormatterService;
import com.palantir.javaformat.java.JavaFormatterOptions;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
Expand Down Expand Up @@ -83,10 +82,10 @@ public PalantirCodeStyleManager(@NotNull CodeStyleManager original) {
}

static Map<TextRange, String> getReplacements(
FormatterService formatter, JavaFormatterOptions options, String text, Collection<TextRange> ranges) {
FormatterService formatter, String text, Collection<TextRange> ranges) {
try {
ImmutableMap.Builder<TextRange, String> replacements = ImmutableMap.builder();
formatter.getFormatReplacements(options, text, toRanges(ranges)).forEach(replacement -> {
formatter.getFormatReplacements(text, toRanges(ranges)).forEach(replacement -> {
replacements.put(toTextRange(replacement.getReplaceRange()), replacement.getReplacementString());
});
return replacements.build();
Expand Down Expand Up @@ -201,9 +200,7 @@ private void format(Document document, Collection<TextRange> ranges) {
PalantirJavaFormatSettings settings = PalantirJavaFormatSettings.getInstance(getProject());
FormatterService formatter = implementationCache.get(settings.getImplementationClassPath());

JavaFormatterOptions options = JavaFormatterOptions.builder().style(settings.getStyle()).build();

performReplacements(document, getReplacements(formatter, options, document.getText(), ranges));
performReplacements(document, getReplacements(formatter, document.getText(), ranges));
}

private static FormatterService createFormatter(Optional<List<URI>> implementationClassPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public interface FormatterService {
* @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
*/
ImmutableList<Replacement> getFormatReplacements(
JavaFormatterOptions options, String input, Collection<Range<Integer>> ranges) throws FormatterException;
ImmutableList<Replacement> getFormatReplacements(String input, Collection<Range<Integer>> ranges)
throws FormatterException;

/**
* Formats an input string (a Java compilation unit), reflows strings and fixes imports.
*
* <p>Fixing imports includes ordering, spacing, and removal of unused import statements.
*
* @param input the input string
* @return the output string
* @throws FormatterException if the input string cannot be parsed
* @see <a href="https://google.github.io/styleguide/javaguide.html#s3.3.3-import-ordering-and-spacing">Google Java
* Style Guide - 3.3.3 Import ordering and spacing</a>
*/
String formatSourceReflowStringsAndFixImports(String input) throws FormatterException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
import com.palantir.javaformat.java.JavaFormatterOptions.Style;
import java.util.Collection;

@AutoService(FormatterService.class)
public final class FormatterServiceImpl implements FormatterService {

private final Formatter formatter;

public FormatterServiceImpl() {
JavaFormatterOptions options = JavaFormatterOptions.builder().style(Style.PALANTIR).build();
formatter = Formatter.createFormatter(options);
}

@Override
public ImmutableList<Replacement> getFormatReplacements(
JavaFormatterOptions options, String text, Collection<Range<Integer>> toRanges) throws FormatterException {
public ImmutableList<Replacement> getFormatReplacements(String text, Collection<Range<Integer>> toRanges)
throws FormatterException {
return formatter.getFormatReplacements(text, toRanges);
}

return Formatter.createFormatter(options).getFormatReplacements(text, toRanges);
@Override
public String formatSourceReflowStringsAndFixImports(String input) throws FormatterException {
return formatter.formatSourceAndFixImports(input);
}
}