Skip to content

Commit fc8706b

Browse files
committed
feature: Allow setting 'skip reflowing long strings' in IntelliJ plugin
1 parent f5de565 commit fc8706b

12 files changed

Lines changed: 181 additions & 34 deletions

File tree

gradle-palantir-java-format/src/main/groovy/com/palantir/javaformat/gradle/PalantirJavaFormatPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.palantir.javaformat.bootstrap.NativeImageFormatterService;
2020
import com.palantir.javaformat.java.FormatterService;
21+
import com.palantir.javaformat.java.JavaFormatterOptions;
2122
import java.io.File;
2223
import java.io.IOException;
2324
import org.gradle.api.DefaultTask;
@@ -84,7 +85,7 @@ public final void formatDiff() throws IOException, InterruptedException {
8485
FormatDiff.formatDiff(
8586
getProject().getProjectDir().toPath(),
8687
new NativeImageFormatterService(
87-
getNativeImage().get().getAsFile().toPath()));
88+
getNativeImage().get().getAsFile().toPath(), JavaFormatterOptions.defaultOptions()));
8889
} else {
8990
log.info("Using the Java-based formatter");
9091
JavaFormatExtension extension =

gradle-palantir-java-format/src/test/java/com/palantir/javaformat/gradle/FormatDiffTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.palantir.javaformat.bootstrap.BootstrappingFormatterService;
2626
import com.palantir.javaformat.bootstrap.NativeImageFormatterService;
2727
import com.palantir.javaformat.java.FormatterService;
28+
import com.palantir.javaformat.java.JavaFormatterOptions;
2829
import java.io.ByteArrayOutputStream;
2930
import java.io.File;
3031
import java.io.IOException;
@@ -104,11 +105,12 @@ private void runCommandInRepo(String... args) throws IOException, InterruptedExc
104105
}
105106

106107
private static Stream<FormatterService> getFormatters() throws IOException {
108+
JavaFormatterOptions options = JavaFormatterOptions.builder().build();
107109
return Stream.of(
108110
new BootstrappingFormatterService(
109-
javaBinPath(), Runtime.version().feature(), getClasspath()),
111+
javaBinPath(), Runtime.version().feature(), getClasspath(), options),
110112
new NativeImageFormatterService(
111-
Path.of(Files.readString(NATIVE_IMAGE_FILE.toPath()).trim())));
113+
Path.of(Files.readString(NATIVE_IMAGE_FILE.toPath()).trim()), options));
112114
}
113115

114116
private static List<Path> getClasspath() throws IOException {

idea-plugin/src/main/java/com/palantir/javaformat/intellij/FormatterProvider.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.palantir.javaformat.bootstrap.BootstrappingFormatterService;
3434
import com.palantir.javaformat.bootstrap.NativeImageFormatterService;
3535
import com.palantir.javaformat.java.FormatterService;
36+
import com.palantir.javaformat.java.JavaFormatterOptions;
3637
import java.io.IOException;
3738
import java.io.UncheckedIOException;
3839
import java.net.MalformedURLException;
@@ -73,14 +74,18 @@ Optional<FormatterService> get(Project project, PalantirJavaFormatSettings setti
7374
getSdkVersion(project),
7475
settings.getImplementationClassPath(),
7576
settings.getNativeImageClassPath(),
76-
settings.injectedVersionIsOutdated()));
77+
settings.injectedVersionIsOutdated(),
78+
settings.isSkipReflowingLongStrings()));
7779
}
7880

7981
@SuppressWarnings("for-rollout:Slf4jLogsafeArgs")
8082
private static Optional<FormatterService> createFormatter(FormatterCacheKey cacheKey) {
83+
JavaFormatterOptions options = JavaFormatterOptions.builder()
84+
.skipReflowingLongStrings(cacheKey.skipReflowingLongStrings)
85+
.build();
8186
if (cacheKey.nativeImageClassPath.isPresent()) {
8287
log.info("Using the native formatter with classpath: {}", cacheKey.nativeImageClassPath.get());
83-
return Optional.of(new NativeImageFormatterService(Path.of(cacheKey.nativeImageClassPath.get())));
88+
return Optional.of(new NativeImageFormatterService(Path.of(cacheKey.nativeImageClassPath.get()), options));
8489
}
8590
if (cacheKey.jdkMajorVersion.isEmpty()) {
8691
return Optional.empty();
@@ -96,7 +101,8 @@ private static Optional<FormatterService> createFormatter(FormatterCacheKey cach
96101
jdkMajorVersion, ApplicationInfo.getInstance().getBuild())) {
97102
Path jdkPath = getJdkPath(cacheKey.project);
98103
log.info("Using bootstrapping formatter with jdk version {} and path: {}", jdkMajorVersion, jdkPath);
99-
return Optional.of(new BootstrappingFormatterService(jdkPath, jdkMajorVersion, implementationClasspath));
104+
return Optional.of(
105+
new BootstrappingFormatterService(jdkPath, jdkMajorVersion, implementationClasspath, options));
100106
}
101107

102108
// Use "in-process" formatter service
@@ -225,18 +231,21 @@ private static final class FormatterCacheKey {
225231
private final Optional<List<URI>> implementationClassPath;
226232
private final Optional<URI> nativeImageClassPath;
227233
private final boolean useBundledImplementation;
234+
private final boolean skipReflowingLongStrings;
228235

229236
FormatterCacheKey(
230237
Project project,
231238
OptionalInt jdkMajorVersion,
232239
Optional<List<URI>> implementationClassPath,
233240
Optional<URI> nativeImageClassPath,
234-
boolean useBundledImplementation) {
241+
boolean useBundledImplementation,
242+
boolean skipReflowingLongStrings) {
235243
this.project = project;
236244
this.jdkMajorVersion = jdkMajorVersion;
237245
this.implementationClassPath = implementationClassPath;
238246
this.nativeImageClassPath = nativeImageClassPath;
239247
this.useBundledImplementation = useBundledImplementation;
248+
this.skipReflowingLongStrings = skipReflowingLongStrings;
240249
}
241250

242251
@Override
@@ -250,6 +259,7 @@ public boolean equals(Object o) {
250259
FormatterCacheKey that = (FormatterCacheKey) o;
251260
return Objects.equals(jdkMajorVersion, that.jdkMajorVersion)
252261
&& useBundledImplementation == that.useBundledImplementation
262+
&& skipReflowingLongStrings == that.skipReflowingLongStrings
253263
&& Objects.equals(project, that.project)
254264
&& Objects.equals(implementationClassPath, that.implementationClassPath)
255265
&& Objects.equals(nativeImageClassPath, that.nativeImageClassPath);
@@ -258,7 +268,12 @@ public boolean equals(Object o) {
258268
@Override
259269
public int hashCode() {
260270
return Objects.hash(
261-
project, jdkMajorVersion, implementationClassPath, nativeImageClassPath, useBundledImplementation);
271+
project,
272+
jdkMajorVersion,
273+
implementationClassPath,
274+
nativeImageClassPath,
275+
useBundledImplementation,
276+
skipReflowingLongStrings);
262277
}
263278
}
264279
}
Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.palantir.javaformat.intellij.PalantirJavaFormatConfigurable">
3-
<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">
3+
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="8" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
66
<xy x="20" y="20" width="500" height="400"/>
@@ -16,73 +16,81 @@
1616
<text value="Enable palantir-java-format"/>
1717
</properties>
1818
</component>
19-
<vspacer id="19e83">
19+
<component id="skipReflow" class="javax.swing.JCheckBox" binding="skipReflowingLongStrings" default-binding="true">
2020
<constraints>
21-
<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"/>
21+
<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"/>
2222
</constraints>
23-
</vspacer>
23+
<properties>
24+
<text value="Skip reflowing long strings"/>
25+
</properties>
26+
</component>
2427
<component id="c93e1" class="javax.swing.JLabel">
2528
<constraints>
26-
<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"/>
29+
<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"/>
2730
</constraints>
2831
<properties>
2932
<text value="Code style"/>
3033
</properties>
3134
</component>
3235
<component id="31761" class="javax.swing.JComboBox" binding="styleComboBox" custom-create="true">
3336
<constraints>
34-
<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"/>
37+
<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"/>
3538
</constraints>
3639
<properties/>
3740
</component>
38-
<component id="d2ce8" class="javax.swing.JLabel">
41+
<component id="6e9b7" class="javax.swing.JLabel">
3942
<constraints>
4043
<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"/>
4144
</constraints>
4245
<properties>
43-
<text value="Implementation version"/>
46+
<text value="Plugin version"/>
4447
</properties>
4548
</component>
46-
<component id="f9300" class="javax.swing.JLabel" binding="formatterVersion">
49+
<component id="ba751" class="javax.swing.JLabel" binding="pluginVersion">
4750
<constraints>
48-
<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"/>
51+
<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"/>
4952
</constraints>
5053
<properties>
51-
<text value="what version are we running with?"/>
54+
<text value="plugin version"/>
5255
</properties>
5356
</component>
54-
<component id="6e9b7" class="javax.swing.JLabel">
57+
<component id="d2ce8" class="javax.swing.JLabel">
5558
<constraints>
56-
<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"/>
59+
<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"/>
5760
</constraints>
5861
<properties>
59-
<text value="Plugin version"/>
62+
<text value="Implementation version"/>
6063
</properties>
6164
</component>
62-
<component id="ba751" class="javax.swing.JLabel" binding="pluginVersion">
65+
<component id="f9300" class="javax.swing.JLabel" binding="formatterVersion">
6366
<constraints>
64-
<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"/>
67+
<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"/>
6568
</constraints>
6669
<properties>
67-
<text value="plugin version"/>
70+
<text value="what version are we running with?"/>
6871
</properties>
6972
</component>
7073
<component id="ce92" class="javax.swing.JLabel">
7174
<constraints>
72-
<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"/>
75+
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
7376
</constraints>
7477
<properties>
7578
<text value="Formatter type"/>
7679
</properties>
7780
</component>
7881
<component id="1ad4" class="javax.swing.JLabel" binding="isUsingNativeImage">
7982
<constraints>
80-
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
83+
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
8184
</constraints>
8285
<properties>
8386
<text value="any notes"/>
8487
</properties>
8588
</component>
89+
<vspacer id="19e83">
90+
<constraints>
91+
<grid row="7" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
92+
</constraints>
93+
</vspacer>
8694
</children>
8795
</grid>
8896
</form>

idea-plugin/src/main/java/com/palantir/javaformat/intellij/PalantirJavaFormatConfigurable.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class PalantirJavaFormatConfigurable extends BaseConfigurable implements Searcha
3636
private final Project project;
3737
private JPanel panel;
3838
private JCheckBox enable;
39+
private JCheckBox skipReflowingLongStrings;
3940

4041
@SuppressWarnings("for-rollout:RawTypes")
4142
private JComboBox styleComboBox;
@@ -84,6 +85,7 @@ public void apply() throws ConfigurationException {
8485
PalantirJavaFormatSettings settings = PalantirJavaFormatSettings.getInstance(project);
8586
settings.setEnabled(enable.isSelected() ? EnabledState.ENABLED : getDisabledState());
8687
settings.setStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert());
88+
settings.setSkipReflowingLongStrings(skipReflowingLongStrings.isSelected());
8789
}
8890

8991
private EnabledState getDisabledState() {
@@ -98,6 +100,7 @@ public void reset() {
98100
PalantirJavaFormatSettings settings = PalantirJavaFormatSettings.getInstance(project);
99101
enable.setSelected(settings.isEnabled());
100102
styleComboBox.setSelectedItem(UiFormatterStyle.convert(settings.getStyle()));
103+
skipReflowingLongStrings.setSelected(settings.isSkipReflowingLongStrings());
101104
pluginVersion.setText(settings.getImplementationVersion().orElse("unknown"));
102105
formatterVersion.setText(getFormatterVersionText(settings));
103106
isUsingNativeImage.setText(isUsingNativeImage(settings));
@@ -107,7 +110,8 @@ public void reset() {
107110
public boolean isModified() {
108111
PalantirJavaFormatSettings settings = PalantirJavaFormatSettings.getInstance(project);
109112
return enable.isSelected() != settings.isEnabled()
110-
|| !styleComboBox.getSelectedItem().equals(UiFormatterStyle.convert(settings.getStyle()));
113+
|| !styleComboBox.getSelectedItem().equals(UiFormatterStyle.convert(settings.getStyle()))
114+
|| skipReflowingLongStrings.isSelected() != settings.isSkipReflowingLongStrings();
111115
}
112116

113117
@Override

idea-plugin/src/main/java/com/palantir/javaformat/intellij/PalantirJavaFormatSettings.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ void setStyle(JavaFormatterOptions.Style style) {
8282
state.style = style;
8383
}
8484

85+
boolean isSkipReflowingLongStrings() {
86+
return state.skipReflowingLongStrings;
87+
}
88+
89+
void setSkipReflowingLongStrings(boolean skipReflowingLongStrings) {
90+
state.skipReflowingLongStrings = skipReflowingLongStrings;
91+
}
92+
8593
/**
8694
* The paths to jars that provide an alternative implementation of the formatter. If set, this implementation will
8795
* be used instead of the bundled version.
@@ -147,6 +155,7 @@ static class State {
147155
private Optional<URI> nativeImageClassPath = Optional.empty();
148156

149157
public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.PALANTIR;
158+
public boolean skipReflowingLongStrings = false;
150159

151160
public void setImplementationClassPath(@Nullable List<String> value) {
152161
implementationClassPath = Optional.ofNullable(value)

idea-plugin/src/test/java/com/palantir/javaformat/intellij/PalantirJavaFormatFormattingServiceTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ public void defaultFormatSettings() throws Exception {
9797
assertThat(delegatingFormatter.wasInvoked()).isTrue();
9898
}
9999

100+
@Test
101+
public void skipReflowingLongStringsSettingPersists() throws Exception {
102+
settings.setSkipReflowingLongStrings(true);
103+
assertThat(settings.isSkipReflowingLongStrings()).isTrue();
104+
105+
settings.setSkipReflowingLongStrings(false);
106+
assertThat(settings.isSkipReflowingLongStrings()).isFalse();
107+
}
108+
100109
protected Project getProject() {
101110
return fixture.getProject();
102111
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.palantir.javaformat.intellij;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import com.palantir.javaformat.intellij.PalantirJavaFormatSettings.State;
22+
import org.junit.jupiter.api.Test;
23+
24+
public final class PalantirJavaFormatSettingsTest {
25+
26+
@Test
27+
void skipReflowingLongStrings_defaultIsFalse() {
28+
State state = new State();
29+
assertThat(state.skipReflowingLongStrings).isFalse();
30+
}
31+
32+
@Test
33+
void skipReflowingLongStrings_canBeSetToTrue() {
34+
State state = new State();
35+
state.skipReflowingLongStrings = true;
36+
assertThat(state.skipReflowingLongStrings).isTrue();
37+
}
38+
39+
@Test
40+
void skipReflowingLongStrings_canBeSetToFalse() {
41+
State state = new State();
42+
state.skipReflowingLongStrings = true;
43+
state.skipReflowingLongStrings = false;
44+
assertThat(state.skipReflowingLongStrings).isFalse();
45+
}
46+
47+
@Test
48+
void skipReflowingLongStrings_persistsAcrossStateInstances() {
49+
State state1 = new State();
50+
state1.skipReflowingLongStrings = true;
51+
52+
State state2 = new State();
53+
state2.skipReflowingLongStrings = state1.skipReflowingLongStrings;
54+
55+
assertThat(state2.skipReflowingLongStrings).isTrue();
56+
}
57+
}

0 commit comments

Comments
 (0)