Skip to content

Allow selecting code style in Intellij plugin #1256

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1256.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: Allow selecting code style in Intellij plugin
links:
- https://github.com/palantir/palantir-java-format/pull/1256

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.palantir.javaformat.bootstrap.BootstrappingFormatterService;
import com.palantir.javaformat.bootstrap.NativeImageFormatterService;
import com.palantir.javaformat.java.FormatterService;
import com.palantir.javaformat.java.JavaFormatterOptions.Style;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
Expand Down Expand Up @@ -69,6 +70,7 @@ Optional<FormatterService> get(Project project, PalantirJavaFormatSettings setti
return implementationCache.get(new FormatterCacheKey(
project,
getSdkVersion(project),
settings.getStyle(),
settings.getImplementationClassPath(),
settings.getNativeImageClassPath(),
settings.injectedVersionIsOutdated()));
Expand All @@ -93,7 +95,8 @@ private static Optional<FormatterService> createFormatter(FormatterCacheKey cach
jdkMajorVersion, ApplicationInfo.getInstance().getBuild())) {
Path jdkPath = getJdkPath(cacheKey.project);
log.info("Using bootstrapping formatter with jdk version {} and path: {}", jdkMajorVersion, jdkPath);
return Optional.of(new BootstrappingFormatterService(jdkPath, jdkMajorVersion, implementationClasspath));
return Optional.of(new BootstrappingFormatterService(
jdkPath, jdkMajorVersion, implementationClasspath, cacheKey.style));
}

// Use "in-process" formatter service
Expand Down Expand Up @@ -214,18 +217,21 @@ private static List<Path> listDirAsUrlsUnchecked(Path dir) {
private static final class FormatterCacheKey {
private final Project project;
private final OptionalInt jdkMajorVersion;
private final Style style;
private final Optional<List<URI>> implementationClassPath;
private final Optional<URI> nativeImageClassPath;
private final boolean useBundledImplementation;

FormatterCacheKey(
Project project,
OptionalInt jdkMajorVersion,
Style style,
Optional<List<URI>> implementationClassPath,
Optional<URI> nativeImageClassPath,
boolean useBundledImplementation) {
this.project = project;
this.jdkMajorVersion = jdkMajorVersion;
this.style = style;
this.implementationClassPath = implementationClassPath;
this.nativeImageClassPath = nativeImageClassPath;
this.useBundledImplementation = useBundledImplementation;
Expand All @@ -243,14 +249,20 @@ public boolean equals(Object o) {
return Objects.equals(jdkMajorVersion, that.jdkMajorVersion)
&& useBundledImplementation == that.useBundledImplementation
&& Objects.equals(project, that.project)
&& Objects.equals(style, that.style)
&& Objects.equals(implementationClassPath, that.implementationClassPath)
&& Objects.equals(nativeImageClassPath, that.nativeImageClassPath);
}

@Override
public int hashCode() {
return Objects.hash(
project, jdkMajorVersion, implementationClassPath, nativeImageClassPath, useBundledImplementation);
project,
jdkMajorVersion,
style,
implementationClassPath,
nativeImageClassPath,
useBundledImplementation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
/** Configuration options for the formatting style. */
enum UiFormatterStyle {
PALANTIR("Default Palantir Java style", Style.PALANTIR),
GOOGLE("Default Google Java Style", Style.GOOGLE),
AOSP("AOSP-compliant Style", Style.AOSP),
;

private final String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.collect.Range;
import com.palantir.javaformat.java.FormatterException;
import com.palantir.javaformat.java.FormatterService;
import com.palantir.javaformat.java.JavaFormatterOptions.Style;
import com.palantir.javaformat.java.Replacement;
import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -43,11 +44,18 @@ public final class BootstrappingFormatterService implements FormatterService {
private final Path jdkPath;
private final Integer jdkMajorVersion;
private final List<Path> implementationClassPath;
private final Style style;

public BootstrappingFormatterService(Path jdkPath, Integer jdkMajorVersion, List<Path> implementationClassPath) {
this(jdkPath, jdkMajorVersion, implementationClassPath, Style.PALANTIR);
}

public BootstrappingFormatterService(
Path jdkPath, Integer jdkMajorVersion, List<Path> implementationClassPath, Style style) {
this.jdkPath = jdkPath;
this.jdkMajorVersion = jdkMajorVersion;
this.implementationClassPath = implementationClassPath;
this.style = style;
}

@Override
Expand Down Expand Up @@ -85,6 +93,7 @@ private ImmutableList<Replacement> getFormatReplacementsInternal(String input, C
.implementationClasspath(implementationClassPath)
.outputReplacements(true)
.characterRanges(ranges.stream().map(RangeUtils::toStringRange).collect(Collectors.toList()))
.style(style)
.build();

Optional<String> output = FormatterCommandRunner.runWithStdin(command.toArgs(), input);
Expand All @@ -100,6 +109,7 @@ private String runFormatterCommand(String input) throws IOException {
.withJvmArgsForVersion(jdkMajorVersion)
.implementationClasspath(implementationClassPath)
.outputReplacements(false)
.style(style)
.build();
return FormatterCommandRunner.runWithStdin(command.toArgs(), input).orElse(input);
}
Expand All @@ -116,6 +126,8 @@ interface FormatterCliArgs {

List<String> jvmArgs();

Style style();

default List<String> toArgs() {
ImmutableList.Builder<String> args = ImmutableList.<String>builder()
.add(jdkPath().toAbsolutePath().toString())
Expand All @@ -134,9 +146,16 @@ default List<String> toArgs() {
args.add("--output-replacements");
}

switch (style()) {
case GOOGLE -> {}
case PALANTIR -> {
args.add("--palantir");
}
case AOSP -> {
args.add("--aosp");
}
}
return args
// Use palantir style
.add("--palantir")
// Trailing "-" enables formatting stdin -> stdout
.add("-")
.build();
Expand Down