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

Add option to IDEA plugin (and CLI) to format JavaDoc #724 #986

Open
wants to merge 6 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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-986.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: feature
fix:
description: Add option to IDEA plugin (and CLI) to format JavaDoc
links:
- https://github.com/palantir/palantir-java-format/pull/986
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
Expand All @@ -32,6 +33,7 @@
import com.intellij.openapi.util.SystemInfo;
import com.palantir.javaformat.bootstrap.BootstrappingFormatterService;
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 @@ -64,7 +66,9 @@ Optional<FormatterService> get(Project project, PalantirJavaFormatSettings setti
project,
getSdkVersion(project),
settings.getImplementationClassPath(),
settings.injectedVersionIsOutdated()));
settings.injectedVersionIsOutdated(),
settings.getStyle(),
settings.isFormatJavadoc()));
}

private static Optional<FormatterService> createFormatter(FormatterCacheKey cacheKey) {
Expand All @@ -76,20 +80,28 @@ private static Optional<FormatterService> createFormatter(FormatterCacheKey cach
List<Path> implementationClasspath =
getImplementationUrls(cacheKey.implementationClassPath, cacheKey.useBundledImplementation);

JavaFormatterOptions options = JavaFormatterOptions.builder()
.formatJavadoc(cacheKey.formatJavadoc)
.style(cacheKey.style != null ? cacheKey.style : JavaFormatterOptions.Style.PALANTIR)
.build();

// When running with JDK 15+ or using newer language features, we use the bootstrapping formatter which injects
// required "--add-exports" args.
if (useBootstrappingFormatter(
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, options));
}

// Use "in-process" formatter service
log.info("Using in-process formatter for jdk version {}", jdkMajorVersion);
URL[] implementationUrls = toUrlsUnchecked(implementationClasspath);
ClassLoader classLoader = new URLClassLoader(implementationUrls, FormatterService.class.getClassLoader());
return ServiceLoader.load(FormatterService.class, classLoader).findFirst();
return ServiceLoader.load(FormatterService.class, classLoader)
.findFirst()
.map(f -> f.withOptions(Functions.constant(options)));
}

/**
Expand Down Expand Up @@ -199,16 +211,22 @@ private static final class FormatterCacheKey {
private final OptionalInt jdkMajorVersion;
private final Optional<List<URI>> implementationClassPath;
private final boolean useBundledImplementation;
private final boolean formatJavadoc;
private final JavaFormatterOptions.Style style;

FormatterCacheKey(
Project project,
OptionalInt jdkMajorVersion,
Optional<List<URI>> implementationClassPath,
boolean useBundledImplementation) {
boolean useBundledImplementation,
JavaFormatterOptions.Style style,
boolean formatJavadoc) {
this.project = project;
this.jdkMajorVersion = jdkMajorVersion;
this.implementationClassPath = implementationClassPath;
this.useBundledImplementation = useBundledImplementation;
this.style = style;
this.formatJavadoc = formatJavadoc;
}

@Override
Expand All @@ -223,12 +241,15 @@ public boolean equals(Object o) {
return Objects.equals(jdkMajorVersion, that.jdkMajorVersion)
&& useBundledImplementation == that.useBundledImplementation
&& Objects.equals(project, that.project)
&& Objects.equals(implementationClassPath, that.implementationClassPath);
&& Objects.equals(implementationClassPath, that.implementationClassPath)
&& Objects.equals(style, that.style)
&& Objects.equals(formatJavadoc, that.formatJavadoc);
}

@Override
public int hashCode() {
return Objects.hash(project, jdkMajorVersion, implementationClassPath, useBundledImplementation);
return Objects.hash(
project, jdkMajorVersion, implementationClassPath, useBundledImplementation, style, formatJavadoc);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void runActivity(@NotNull Project project) {
PalantirJavaFormatSettings settings = PalantirJavaFormatSettings.getInstance(project);
if (settings.isUninitialized()) {
settings.setEnabled(false);
settings.setFormatJavadoc(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.palantir.javaformat.intellij.PalantirJavaFormatConfigurable">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="5" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="4a87f" class="javax.swing.JCheckBox" binding="enable" default-binding="true">
<component id="f9d77" class="javax.swing.JCheckBox" binding="formatJavadoc" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Enable palantir-java-format"/>
<hideActionText value="false"/>
<text value="Format JavaDoc"/>
</properties>
</component>
<vspacer id="19e83">
<constraints>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="c93e1" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Code style"/>
</properties>
</component>
<component id="31761" class="javax.swing.JComboBox" binding="styleComboBox" custom-create="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="1" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="1" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="d2ce8" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Implementation version"/>
</properties>
</component>
<component id="f9300" class="javax.swing.JLabel" binding="formatterVersion">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="0" fill="1" indent="1" use-parent-layout="false"/>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="0" fill="1" indent="1" use-parent-layout="false"/>
</constraints>
<properties>
<text value="what version are we running with?"/>
</properties>
</component>
<component id="6e9b7" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Plugin version"/>
</properties>
</component>
<component id="ba751" class="javax.swing.JLabel" binding="pluginVersion">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
</constraints>
<properties>
<text value="plugin version"/>
</properties>
</component>
<component id="4a87f" class="javax.swing.JCheckBox" binding="enable" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Enable palantir-java-format"/>
</properties>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class PalantirJavaFormatConfigurable extends BaseConfigurable implements Searcha
private final Project project;
private JPanel panel;
private JCheckBox enable;
private JCheckBox formatJavadoc;
private JComboBox styleComboBox;
private JLabel formatterVersion;
private JLabel pluginVersion;
Expand Down Expand Up @@ -82,6 +83,7 @@ public JComponent createComponent() {
public void apply() throws ConfigurationException {
PalantirJavaFormatSettings settings = PalantirJavaFormatSettings.getInstance(project);
settings.setEnabled(enable.isSelected() ? EnabledState.ENABLED : getDisabledState());
settings.setFormatJavadoc(formatJavadoc.isSelected());
settings.setStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert());
}

Expand All @@ -96,6 +98,7 @@ private EnabledState getDisabledState() {
public void reset() {
PalantirJavaFormatSettings settings = PalantirJavaFormatSettings.getInstance(project);
enable.setSelected(settings.isEnabled());
formatJavadoc.setSelected(settings.isFormatJavadoc());
styleComboBox.setSelectedItem(UiFormatterStyle.convert(settings.getStyle()));
pluginVersion.setText(settings.getImplementationVersion().orElse("unknown"));
formatterVersion.setText(getFormatterVersionText(settings));
Expand All @@ -105,6 +108,7 @@ public void reset() {
public boolean isModified() {
PalantirJavaFormatSettings settings = PalantirJavaFormatSettings.getInstance(project);
return enable.isSelected() != settings.isEnabled()
|| formatJavadoc.isSelected() != settings.isFormatJavadoc()
|| !styleComboBox.getSelectedItem().equals(UiFormatterStyle.convert(settings.getStyle()));
}

Expand Down Expand Up @@ -155,11 +159,29 @@ private void createUIComponents() {
null,
0,
false));
formatJavadoc = new JCheckBox();
formatJavadoc.setText("Format JavaDoc");
panel.add(
formatJavadoc,
new GridConstraints(
1,
0,
1,
2,
GridConstraints.ANCHOR_WEST,
GridConstraints.FILL_NONE,
GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
GridConstraints.SIZEPOLICY_FIXED,
null,
null,
null,
0,
false));
final Spacer spacer1 = new Spacer();
panel.add(
spacer1,
new GridConstraints(
4,
5,
0,
1,
2,
Expand All @@ -177,7 +199,7 @@ private void createUIComponents() {
panel.add(
label1,
new GridConstraints(
1,
2,
0,
1,
1,
Expand All @@ -193,7 +215,7 @@ private void createUIComponents() {
panel.add(
styleComboBox,
new GridConstraints(
1,
2,
1,
1,
1,
Expand All @@ -211,7 +233,7 @@ private void createUIComponents() {
panel.add(
label2,
new GridConstraints(
3,
4,
0,
1,
1,
Expand All @@ -229,7 +251,7 @@ private void createUIComponents() {
panel.add(
formatterVersion,
new GridConstraints(
3,
4,
1,
1,
1,
Expand All @@ -247,7 +269,7 @@ private void createUIComponents() {
panel.add(
label3,
new GridConstraints(
2,
3,
0,
1,
1,
Expand All @@ -265,7 +287,7 @@ private void createUIComponents() {
panel.add(
pluginVersion,
new GridConstraints(
2,
3,
1,
1,
1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ void setEnabled(EnabledState enabled) {
state.enabled = enabled;
}

boolean isFormatJavadoc() {
return state.formatJavadoc;
}

void setFormatJavadoc(boolean formatJavadoc) {
state.formatJavadoc = formatJavadoc;
}

boolean isUninitialized() {
return state.enabled.equals(EnabledState.UNKNOWN);
}
Expand Down Expand Up @@ -137,6 +145,7 @@ static class State {
private EnabledState enabled = EnabledState.UNKNOWN;
private Optional<List<URI>> implementationClassPath = Optional.empty();

private boolean formatJavadoc = false;
public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.PALANTIR;

public void setImplementationClassPath(@Nullable List<String> value) {
Expand Down Expand Up @@ -172,11 +181,21 @@ public String getEnabled() {
}
}

public boolean isFormatJavadoc() {
return formatJavadoc;
}

public void setFormatJavadoc(boolean formatJavadoc) {
this.formatJavadoc = formatJavadoc;
}

@Override
public String toString() {
return "PalantirJavaFormatSettings{"
+ "enabled="
+ enabled
+ ", formatJavadoc="
+ formatJavadoc
+ ", formatterPath="
+ implementationClassPath
+ ", style="
Expand Down
Loading