|
18 | 18 |
|
19 | 19 | import com.github.benmanes.caffeine.cache.Caffeine; |
20 | 20 | import com.github.benmanes.caffeine.cache.LoadingCache; |
21 | | -import com.google.common.base.Preconditions; |
22 | | -import com.intellij.ide.plugins.IdeaPluginDescriptor; |
23 | | -import com.intellij.ide.plugins.PluginManager; |
24 | | -import com.intellij.openapi.extensions.PluginId; |
| 21 | +import com.intellij.formatting.service.FormattingService; |
| 22 | +import com.intellij.openapi.extensions.PluginDescriptor; |
25 | 23 | import com.intellij.openapi.project.Project; |
26 | 24 | import com.intellij.openapi.util.SystemInfo; |
27 | 25 | import com.palantir.javaformat.bootstrap.BootstrappingFormatterService; |
28 | 26 | import com.palantir.javaformat.bootstrap.NativeImageFormatterService; |
29 | 27 | import com.palantir.javaformat.java.FormatterService; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
30 | 31 | import java.io.IOException; |
31 | 32 | import java.io.UncheckedIOException; |
32 | 33 | import java.net.URI; |
33 | 34 | import java.nio.file.Files; |
34 | 35 | import java.nio.file.Path; |
35 | 36 | import java.util.List; |
| 37 | +import java.util.NoSuchElementException; |
36 | 38 | import java.util.Objects; |
37 | 39 | import java.util.Optional; |
38 | 40 | import java.util.stream.Collectors; |
39 | 41 | import java.util.stream.Stream; |
40 | | -import org.slf4j.Logger; |
41 | | -import org.slf4j.LoggerFactory; |
| 42 | + |
| 43 | +import static java.util.Optional.ofNullable; |
42 | 44 |
|
43 | 45 | final class FormatterProvider { |
44 | 46 | private static final Logger log = LoggerFactory.getLogger(FormatterProvider.class); |
45 | 47 |
|
46 | | - static final String PLUGIN_ID = "open-java-format"; |
47 | | - |
48 | 48 | // Cache to avoid resolving the formatter every time we want to format from IntelliJ |
49 | 49 | private final LoadingCache<FormatterCacheKey, Optional<FormatterService>> implementationCache = |
50 | 50 | Caffeine.newBuilder().maximumSize(1).build(FormatterProvider::createFormatter); |
51 | 51 |
|
52 | | - static IdeaPluginDescriptor getPluginDescriptor() { |
53 | | - return Preconditions.checkNotNull( |
54 | | - PluginManager.getInstance().findEnabledPlugin(PluginId.getId(PLUGIN_ID)), |
55 | | - "Couldn't find our own plugin: %s", |
56 | | - PLUGIN_ID); |
57 | | - } |
58 | | - |
59 | 52 | Optional<FormatterService> get(Project project, PalantirJavaFormatSettings settings) { |
60 | 53 | return implementationCache.get(new FormatterCacheKey( |
61 | 54 | project, |
@@ -89,9 +82,14 @@ private static List<Path> getProvidedImplementationUrls(List<URI> implementation |
89 | 82 | @SuppressWarnings("for-rollout:Slf4jLogsafeArgs") |
90 | 83 | private static List<Path> getBundledImplementationUrls() { |
91 | 84 | // Load from the jars bundled with the plugin. |
92 | | - IdeaPluginDescriptor ourPlugin = getPluginDescriptor(); |
93 | | - Path implDir = ourPlugin.getPluginPath().resolve("impl"); |
| 85 | + Path implDir = ofNullable(FormattingService.EP_NAME.findExtension(PalantirJavaFormatFormattingService.class)) |
| 86 | + .flatMap(PalantirJavaFormatFormattingService::getPluginDescriptor) |
| 87 | + .map(PluginDescriptor::getPluginPath) |
| 88 | + .orElseThrow(() -> new NoSuchElementException("The platform has not set the plugin descriptor")) |
| 89 | + .resolve("impl"); |
| 90 | + |
94 | 91 | log.debug("Using open-java-format implementation bundled with plugin: {}", implDir); |
| 92 | + |
95 | 93 | return listDirAsUrlsUnchecked(implDir); |
96 | 94 | } |
97 | 95 |
|
|
0 commit comments