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
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions changelog/@unreleased/pr-47.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
changes:
- type: break
break:
description: Rename `palantir-java-format-api` to `palantir-java-format-spi`.
links:
- https://github.com/palantir/palantir-java-format/pull/47
- type: break
break:
description: Remove `JavaFormatterOptions` altogether from the API, the style now being baked in to be PALANTIR.
links:
- https://github.com/palantir/palantir-java-format/pull/47
- type: improvement
improvement:
description: Expose `formatSourceReflowStringsAndFixImports` from the SPI, which
formats a whole file returning the formatted file as a String.
links:
- https://github.com/palantir/palantir-java-format/pull/47
2 changes: 1 addition & 1 deletion idea-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ configurations {
}

dependencies {
implementation project(':palantir-java-format-api')
implementation project(':palantir-java-format-spi')
implementation 'com.github.ben-manes.caffeine:caffeine'

formatter project(':palantir-java-format')
Expand Down
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.palantir.javaformat.java;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
import java.util.Collection;

/**
* A stable facade for palantir-java-format. The implementation must be ServiceLoaded, to ensure its classpath remains
* isolated.
*/
public interface FormatterService {

/**
* Emit a list of {@link Replacement}s to convert from input to formatted output.
*
* @param input the input compilation unit
* @param ranges 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
*/
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;
}
2 changes: 1 addition & 1 deletion palantir-java-format/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mainClassName = 'com.palantir.javaformat.java.Main'
description = 'Google Java Format'

dependencies {
api project(':palantir-java-format-api')
api project(':palantir-java-format-spi')
api 'com.google.guava:guava'
implementation 'com.google.errorprone:javac-shaded'

Expand Down
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);
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
rootProject.name = 'palantir-java-format-parent'

include ':palantir-java-format'
include ':palantir-java-format-api'
include ':palantir-java-format-spi'
include ':idea-plugin'
include ':gradle-palantir-java-format'

Expand Down