Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.palantir.javaformat.bootstrap.NativeImageFormatterService;
import com.palantir.javaformat.java.FormatterService;
import com.palantir.javaformat.java.JavaFormatterOptions;
import java.io.File;
import java.io.IOException;
import org.gradle.api.DefaultTask;
Expand Down Expand Up @@ -84,7 +85,7 @@ public final void formatDiff() throws IOException, InterruptedException {
FormatDiff.formatDiff(
getProject().getProjectDir().toPath(),
new NativeImageFormatterService(
getNativeImage().get().getAsFile().toPath()));
getNativeImage().get().getAsFile().toPath(), JavaFormatterOptions.defaultOptions()));
} else {
log.info("Using the Java-based formatter");
JavaFormatExtension extension =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.palantir.javaformat.gradle;
import com.google.common.base.Splitter
import com.palantir.javaformat.bootstrap.BootstrappingFormatterService
import com.palantir.javaformat.java.FormatterService
import com.palantir.javaformat.java.JavaFormatterOptions
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource

Expand Down Expand Up @@ -107,11 +108,12 @@ class FormatDiffTest {
}

private static Stream<FormatterService> getFormatters() {
JavaFormatterOptions options = JavaFormatterOptions.builder().build();
return Stream.of(
new BootstrappingFormatterService(
javaBinPath(), Runtime.version().feature(), getClasspath()),
javaBinPath(), Runtime.version().feature(), getClasspath(), options),
new NativeImageFormatterService(
Path.of(NATIVE_IMAGE_FILE.text)));
Path.of(NATIVE_IMAGE_FILE.text), options));
}

private static getClasspath() {
Expand Down
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;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -73,14 +74,18 @@ Optional<FormatterService> get(Project project, PalantirJavaFormatSettings setti
getSdkVersion(project),
settings.getImplementationClassPath(),
settings.getNativeImageClassPath(),
settings.injectedVersionIsOutdated()));
settings.injectedVersionIsOutdated(),
settings.isSkipReflowingLongStrings()));
}

@SuppressWarnings("for-rollout:Slf4jLogsafeArgs")
private static Optional<FormatterService> createFormatter(FormatterCacheKey cacheKey) {
JavaFormatterOptions options = JavaFormatterOptions.builder()
.skipReflowingLongStrings(cacheKey.skipReflowingLongStrings)
.build();
if (cacheKey.nativeImageClassPath.isPresent()) {
log.info("Using the native formatter with classpath: {}", cacheKey.nativeImageClassPath.get());
return Optional.of(new NativeImageFormatterService(Path.of(cacheKey.nativeImageClassPath.get())));
return Optional.of(new NativeImageFormatterService(Path.of(cacheKey.nativeImageClassPath.get()), options));
}
if (cacheKey.jdkMajorVersion.isEmpty()) {
return Optional.empty();
Expand All @@ -96,7 +101,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, options));
}

// Use "in-process" formatter service
Expand Down Expand Up @@ -225,18 +231,21 @@ private static final class FormatterCacheKey {
private final Optional<List<URI>> implementationClassPath;
private final Optional<URI> nativeImageClassPath;
private final boolean useBundledImplementation;
private final boolean skipReflowingLongStrings;

FormatterCacheKey(
Project project,
OptionalInt jdkMajorVersion,
Optional<List<URI>> implementationClassPath,
Optional<URI> nativeImageClassPath,
boolean useBundledImplementation) {
boolean useBundledImplementation,
boolean skipReflowingLongStrings) {
this.project = project;
this.jdkMajorVersion = jdkMajorVersion;
this.implementationClassPath = implementationClassPath;
this.nativeImageClassPath = nativeImageClassPath;
this.useBundledImplementation = useBundledImplementation;
this.skipReflowingLongStrings = skipReflowingLongStrings;
}

@Override
Expand All @@ -250,6 +259,7 @@ public boolean equals(Object o) {
FormatterCacheKey that = (FormatterCacheKey) o;
return Objects.equals(jdkMajorVersion, that.jdkMajorVersion)
&& useBundledImplementation == that.useBundledImplementation
&& skipReflowingLongStrings == that.skipReflowingLongStrings
&& Objects.equals(project, that.project)
&& Objects.equals(implementationClassPath, that.implementationClassPath)
&& Objects.equals(nativeImageClassPath, that.nativeImageClassPath);
Expand All @@ -258,7 +268,12 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
return Objects.hash(
project, jdkMajorVersion, implementationClassPath, nativeImageClassPath, useBundledImplementation);
project,
jdkMajorVersion,
implementationClassPath,
nativeImageClassPath,
useBundledImplementation,
skipReflowingLongStrings);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?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="6" 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="8" 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"/>
Expand All @@ -16,73 +16,81 @@
<text value="Enable palantir-java-format"/>
</properties>
</component>
<vspacer id="19e83">
<component id="skipReflow" class="javax.swing.JCheckBox" binding="skipReflowingLongStrings" default-binding="true">
<constraints>
<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"/>
<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>
</vspacer>
<properties>
<text value="Skip reflowing long strings (Java 15+)"/>
</properties>
</component>
<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">
<component id="6e9b7" 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"/>
</constraints>
<properties>
<text value="Implementation version"/>
<text value="Plugin version"/>
</properties>
</component>
<component id="f9300" class="javax.swing.JLabel" binding="formatterVersion">
<component id="ba751" class="javax.swing.JLabel" binding="pluginVersion">
<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="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="what version are we running with?"/>
<text value="plugin version"/>
</properties>
</component>
<component id="6e9b7" class="javax.swing.JLabel">
<component id="d2ce8" 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="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="Plugin version"/>
<text value="Implementation version"/>
</properties>
</component>
<component id="ba751" class="javax.swing.JLabel" binding="pluginVersion">
<component id="f9300" class="javax.swing.JLabel" binding="formatterVersion">
<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="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="plugin version"/>
<text value="what version are we running with?"/>
</properties>
</component>
<component id="ce92" class="javax.swing.JLabel">
<constraints>
<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"/>
<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"/>
</constraints>
<properties>
<text value="Formatter type"/>
</properties>
</component>
<component id="1ad4" class="javax.swing.JLabel" binding="isUsingNativeImage">
<constraints>
<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"/>
<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"/>
</constraints>
<properties>
<text value="any notes"/>
</properties>
</component>
<vspacer id="19e83">
<constraints>
<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"/>
</constraints>
</vspacer>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class PalantirJavaFormatConfigurable extends BaseConfigurable implements Searcha
private final Project project;
private JPanel panel;
private JCheckBox enable;
private JCheckBox skipReflowingLongStrings;

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ void setStyle(JavaFormatterOptions.Style style) {
state.style = style;
}

boolean isSkipReflowingLongStrings() {
return state.skipReflowingLongStrings;
}

void setSkipReflowingLongStrings(boolean skipReflowingLongStrings) {
state.skipReflowingLongStrings = skipReflowingLongStrings;
}

/**
* The paths to jars that provide an alternative implementation of the formatter. If set, this implementation will
* be used instead of the bundled version.
Expand Down Expand Up @@ -147,6 +155,7 @@ static class State {
private Optional<URI> nativeImageClassPath = Optional.empty();

public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.PALANTIR;
public boolean skipReflowingLongStrings = false;

public void setImplementationClassPath(@Nullable List<String> value) {
implementationClassPath = Optional.ofNullable(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ public void defaultFormatSettings() throws Exception {
assertThat(delegatingFormatter.wasInvoked()).isTrue();
}

@Test
public void skipReflowingLongStringsSettingPersists() throws Exception {
settings.setSkipReflowingLongStrings(true);
assertThat(settings.isSkipReflowingLongStrings()).isTrue();

settings.setSkipReflowingLongStrings(false);
assertThat(settings.isSkipReflowingLongStrings()).isFalse();
}

protected Project getProject() {
return fixture.getProject();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat.intellij;

import static org.assertj.core.api.Assertions.assertThat;

import com.palantir.javaformat.intellij.PalantirJavaFormatSettings.State;
import org.junit.jupiter.api.Test;

public final class PalantirJavaFormatSettingsTest {

@Test
void skipReflowingLongStrings_defaultIsFalse() {
State state = new State();
assertThat(state.skipReflowingLongStrings).isFalse();
}

@Test
void skipReflowingLongStrings_canBeSetToTrue() {
State state = new State();
state.skipReflowingLongStrings = true;
assertThat(state.skipReflowingLongStrings).isTrue();
}

@Test
void skipReflowingLongStrings_canBeSetToFalse() {
State state = new State();
state.skipReflowingLongStrings = true;
state.skipReflowingLongStrings = false;
assertThat(state.skipReflowingLongStrings).isFalse();
}

@Test
void skipReflowingLongStrings_persistsAcrossStateInstances() {
State state1 = new State();
state1.skipReflowingLongStrings = true;

State state2 = new State();
state2.skipReflowingLongStrings = state1.skipReflowingLongStrings;

assertThat(state2.skipReflowingLongStrings).isTrue();
}
}
Loading