Skip to content

Commit 789e9ea

Browse files
committed
Remove too much coupling
1 parent 1985d55 commit 789e9ea

4 files changed

Lines changed: 45 additions & 42 deletions

File tree

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,31 @@
1616

1717
package com.palantir.javaformat.intellij;
1818

19-
import static com.intellij.formatting.service.FormattingService.EP_NAME;
20-
import static java.util.Optional.ofNullable;
21-
2219
import com.github.benmanes.caffeine.cache.Caffeine;
2320
import com.github.benmanes.caffeine.cache.LoadingCache;
21+
import com.intellij.formatting.service.FormattingService;
2422
import com.intellij.openapi.extensions.PluginDescriptor;
2523
import com.intellij.openapi.project.Project;
2624
import com.intellij.openapi.util.SystemInfo;
2725
import com.palantir.javaformat.bootstrap.BootstrappingFormatterService;
2826
import com.palantir.javaformat.bootstrap.NativeImageFormatterService;
2927
import com.palantir.javaformat.java.FormatterService;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
3031
import java.io.IOException;
3132
import java.io.UncheckedIOException;
3233
import java.net.URI;
3334
import java.nio.file.Files;
3435
import java.nio.file.Path;
3536
import java.util.List;
37+
import java.util.NoSuchElementException;
3638
import java.util.Objects;
3739
import java.util.Optional;
3840
import java.util.stream.Collectors;
3941
import java.util.stream.Stream;
40-
import org.slf4j.Logger;
41-
import org.slf4j.LoggerFactory;
42+
43+
import static java.util.Optional.ofNullable;
4244

4345
final class FormatterProvider {
4446
private static final Logger log = LoggerFactory.getLogger(FormatterProvider.class);
@@ -47,17 +49,6 @@ final class FormatterProvider {
4749
private final LoadingCache<FormatterCacheKey, Optional<FormatterService>> implementationCache =
4850
Caffeine.newBuilder().maximumSize(1).build(FormatterProvider::createFormatter);
4951

50-
/**
51-
* The descriptor of this plugin: where its version and the directory of the bundled formatter come from. The
52-
* platform hands it to the formatting service when it creates that service from plugin.xml (PluginAware), and the
53-
* extension point finds the service by class. Every way of looking a plugin up by id or by class became
54-
* {@code @ApiStatus.Internal} in 2026.2; PluginAware and the extension point are public API in every supported IDE.
55-
*/
56-
static Optional<PluginDescriptor> getPluginDescriptor() {
57-
return ofNullable(EP_NAME.findExtension(PalantirJavaFormatFormattingService.class))
58-
.map(PalantirJavaFormatFormattingService::getPluginDescriptor);
59-
}
60-
6152
Optional<FormatterService> get(Project project, PalantirJavaFormatSettings settings) {
6253
return implementationCache.get(new FormatterCacheKey(
6354
project,
@@ -91,9 +82,10 @@ private static List<Path> getProvidedImplementationUrls(List<URI> implementation
9182
@SuppressWarnings("for-rollout:Slf4jLogsafeArgs")
9283
private static List<Path> getBundledImplementationUrls() {
9384
// Load from the jars bundled with the plugin.
94-
Path implDir = getPluginDescriptor()
85+
Path implDir = ofNullable(FormattingService.EP_NAME.findExtension(PalantirJavaFormatFormattingService.class))
86+
.flatMap(PalantirJavaFormatFormattingService::getPluginDescriptor)
9587
.map(PluginDescriptor::getPluginPath)
96-
.orElseThrow()
88+
.orElseThrow(() -> new NoSuchElementException("The platform has not set the plugin descriptor"))
9789
.resolve("impl");
9890

9991
log.debug("Using open-java-format implementation bundled with plugin: {}", implDir);

‎open-java-format-idea-plugin/src/main/java/com/palantir/javaformat/intellij/PalantirJavaFormatFormattingService.java‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package com.palantir.javaformat.intellij;
1818

19-
import static java.util.Comparator.comparing;
20-
21-
import com.google.common.base.Preconditions;
2219
import com.google.common.collect.ImmutableList;
2320
import com.google.common.collect.Range;
2421
import com.intellij.formatting.service.AsyncDocumentFormattingService;
@@ -36,13 +33,17 @@
3633
import com.palantir.javaformat.java.FormatterException;
3734
import com.palantir.javaformat.java.FormatterService;
3835
import com.palantir.javaformat.java.Replacement;
36+
import org.jetbrains.annotations.NotNull;
37+
38+
import javax.annotation.Nullable;
3939
import java.util.ArrayList;
4040
import java.util.Collection;
4141
import java.util.List;
4242
import java.util.Optional;
4343
import java.util.Set;
44-
import javax.annotation.Nullable;
45-
import org.jetbrains.annotations.NotNull;
44+
45+
import static java.util.Comparator.comparing;
46+
import static java.util.Optional.ofNullable;
4647

4748
class PalantirJavaFormatFormattingService extends AsyncDocumentFormattingService implements PluginAware {
4849
private static final Logger logger = Logger.getInstance(PalantirJavaFormatFormattingService.class);
@@ -59,8 +60,8 @@ public void setPluginDescriptor(@NotNull PluginDescriptor pluginDescriptor) {
5960
this.pluginDescriptor = pluginDescriptor;
6061
}
6162

62-
PluginDescriptor getPluginDescriptor() {
63-
return Preconditions.checkNotNull(pluginDescriptor, "The platform has not set the plugin descriptor");
63+
Optional<PluginDescriptor> getPluginDescriptor() {
64+
return ofNullable(pluginDescriptor);
6465
}
6566

6667
@Override
@@ -127,7 +128,7 @@ public void run() {
127128
if (logger.isDebugEnabled()) {
128129
logger.debug(String.format(
129130
"Received request to format file=%s, length=%s with ranges=%s",
130-
Optional.ofNullable(request.getIOFile())
131+
ofNullable(request.getIOFile())
131132
.map(file -> file.toPath().toString())
132133
.orElse("null"),
133134
preFormatText.length(),

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616

1717
package com.palantir.javaformat.intellij;
1818

19-
import static com.palantir.javaformat.intellij.FormatterProvider.getPluginDescriptor;
20-
19+
import com.intellij.formatting.service.FormattingService;
2120
import com.intellij.openapi.components.PersistentStateComponent;
2221
import com.intellij.openapi.components.State;
2322
import com.intellij.openapi.components.Storage;
2423
import com.intellij.openapi.extensions.PluginDescriptor;
2524
import com.intellij.openapi.project.Project;
2625
import com.palantir.javaformat.java.FormatterService;
2726
import com.palantir.javaformat.java.JavaFormatterOptions;
27+
28+
import javax.annotation.Nullable;
2829
import java.io.IOException;
2930
import java.io.UncheckedIOException;
3031
import java.net.URI;
@@ -33,7 +34,8 @@
3334
import java.util.jar.JarFile;
3435
import java.util.stream.Collectors;
3536
import java.util.stream.Stream;
36-
import javax.annotation.Nullable;
37+
38+
import static java.util.Optional.ofNullable;
3739

3840
@SuppressWarnings("for-rollout:SameNameButDifferent")
3941
@State(
@@ -112,7 +114,9 @@ boolean injectedVersionIsOutdated() {
112114
}
113115

114116
Optional<String> getImplementationVersion() {
115-
return getPluginDescriptor().map(PluginDescriptor::getVersion);
117+
return ofNullable(FormattingService.EP_NAME.findExtension(PalantirJavaFormatFormattingService.class))
118+
.flatMap(PalantirJavaFormatFormattingService::getPluginDescriptor)
119+
.map(PluginDescriptor::getVersion);
116120
}
117121

118122
Optional<String> computeFormatterVersion() {

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
package com.palantir.javaformat.intellij;
1818

19-
import static com.palantir.javaformat.intellij.FormatterProvider.getPluginDescriptor;
20-
import static org.assertj.core.api.Assertions.assertThat;
21-
22-
import com.google.common.collect.ImmutableList;
2319
import com.intellij.codeInsight.actions.ReformatCodeProcessor;
2420
import com.intellij.formatting.service.AsyncFormattingRequest;
2521
import com.intellij.formatting.service.FormattingService;
@@ -40,16 +36,20 @@
4036
import com.intellij.testFramework.fixtures.JavaTestFixtureFactory;
4137
import com.intellij.testFramework.fixtures.TestFixtureBuilder;
4238
import com.palantir.javaformat.intellij.PalantirJavaFormatSettings.State;
39+
import org.jetbrains.annotations.NotNull;
40+
import org.junit.jupiter.api.AfterEach;
41+
import org.junit.jupiter.api.BeforeEach;
42+
import org.junit.jupiter.api.Test;
43+
4344
import java.io.File;
4445
import java.io.IOException;
4546
import java.io.UncheckedIOException;
4647
import java.nio.file.Files;
4748
import java.nio.file.Path;
4849
import java.nio.file.Paths;
49-
import org.jetbrains.annotations.NotNull;
50-
import org.junit.jupiter.api.AfterEach;
51-
import org.junit.jupiter.api.BeforeEach;
52-
import org.junit.jupiter.api.Test;
50+
import java.util.List;
51+
52+
import static org.assertj.core.api.Assertions.assertThat;
5353

5454
public class PalantirJavaFormatFormattingServiceTest {
5555
private JavaCodeInsightTestFixture fixture;
@@ -64,13 +64,12 @@ public void setUp() throws Exception {
6464
fixture.setUp();
6565

6666
delegatingFormatter = new DelegatingFormatter();
67-
// Only the service the platform creates from plugin.xml is handed the plugin descriptor. This one is created
68-
// here, so it takes the descriptor from that service before masking it.
69-
delegatingFormatter.setPluginDescriptor(getPluginDescriptor().orElseThrow());
67+
7068
ExtensionTestUtil.maskExtensions(
71-
FormattingService.EP_NAME, ImmutableList.of(delegatingFormatter), fixture.getProjectDisposable());
69+
FormattingService.EP_NAME, List.of(delegatingFormatter), fixture.getProjectDisposable());
7270

7371
settings = PalantirJavaFormatSettings.getInstance(fixture.getProject());
72+
7473
State resetState = new State();
7574
resetState.setEnabled("true");
7675
settings.loadState(resetState);
@@ -130,6 +129,13 @@ private PsiFile createPsiFile(String path, String... contents) throws IOExceptio
130129
}
131130

132131
private static final class DelegatingFormatter extends PalantirJavaFormatFormattingService {
132+
@SuppressWarnings("DataFlowIssue")
133+
public DelegatingFormatter() {
134+
this.setPluginDescriptor(FormattingService.EP_NAME
135+
.findExtension(PalantirJavaFormatFormattingService.class)
136+
.getPluginDescriptor()
137+
.orElseThrow());
138+
}
133139

134140
private boolean invoked = false;
135141

0 commit comments

Comments
 (0)