Skip to content

Commit 834c6c0

Browse files
authored
Move the plugin to the FormattingService & ImportOptimizer APIs (#1078)
1 parent 2ec339a commit 834c6c0

File tree

19 files changed

+697
-471
lines changed

19 files changed

+697
-471
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ generated/
5656
# Blueprint theme
5757
__init__.pyc
5858

59-
node_modules/
59+
node_modules/
60+
/.profileconfig.json

.palantir/revapi.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ acceptedBreaks:
1616
old: "method void com.palantir.javaformat.gradle.ConfigureJavaFormatterXml::configureFormatOnSave(groovy.util.Node)"
1717
new: "method void com.palantir.javaformat.gradle.ConfigureJavaFormatterXml::configureFormatOnSave(groovy.util.Node)"
1818
justification: "Not public api, not split over multiple jars"
19+
"2.43.0":
20+
com.palantir.javaformat:palantir-java-format-spi:
21+
- code: "java.method.addedToInterface"
22+
new: "method java.lang.String com.palantir.javaformat.java.FormatterService::fixImports(java.lang.String)\
23+
\ throws com.palantir.javaformat.java.FormatterException"
24+
justification: "new fixImports only method added to spi"

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ buildscript {
2020
}
2121

2222
plugins {
23-
id "org.jetbrains.intellij" version "1.3.0" apply false
23+
id "org.jetbrains.intellij" version "1.17.3" apply false
2424
id 'org.jetbrains.gradle.plugin.idea-ext' version "1.1.1"
2525
}
2626

changelog/@unreleased/pr-1078.v2.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: fix
2+
fix:
3+
description: Fix palantir-java-format not running on IntelliJ >=2024.1 by moving to new APIs.
4+
links:
5+
- https://github.com/palantir/palantir-java-format/pull/1078

idea-plugin/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def name = "palantir-java-format"
2323
intellij {
2424
pluginName = name
2525
updateSinceUntilBuild = true
26-
version = "IU-202.6397.94"
26+
version = "2024.1"
2727
plugins = ['java']
2828
}
2929

@@ -39,7 +39,7 @@ tasks.named("runIde") {
3939
patchPluginXml {
4040
pluginDescription = "Formats source code using the palantir-java-format tool."
4141
version = project.version
42-
sinceBuild = '193' // TODO: test against this version of IntelliJ to ensure no regressions
42+
sinceBuild = '213' // TODO: test against this version of IntelliJ to ensure no regressions
4343
untilBuild = ''
4444
}
4545

@@ -73,6 +73,7 @@ dependencies {
7373

7474
testImplementation 'org.assertj:assertj-core'
7575
testImplementation 'org.junit.jupiter:junit-jupiter'
76+
testImplementation 'org.junit.platform:junit-platform-launcher'
7677
}
7778

7879
tasks.withType(JavaCompile).configureEach {

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

Lines changed: 0 additions & 233 deletions
This file was deleted.

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
import com.github.benmanes.caffeine.cache.Caffeine;
2020
import com.github.benmanes.caffeine.cache.LoadingCache;
2121
import com.google.common.annotations.VisibleForTesting;
22+
import com.google.common.base.Preconditions;
23+
import com.intellij.ide.plugins.IdeaPluginDescriptor;
24+
import com.intellij.ide.plugins.PluginManager;
2225
import com.intellij.openapi.application.ApplicationInfo;
26+
import com.intellij.openapi.extensions.PluginId;
2327
import com.intellij.openapi.project.Project;
2428
import com.intellij.openapi.projectRoots.JdkUtil;
2529
import com.intellij.openapi.projectRoots.Sdk;
@@ -28,7 +32,6 @@
2832
import com.intellij.openapi.util.SystemInfo;
2933
import com.palantir.javaformat.bootstrap.BootstrappingFormatterService;
3034
import com.palantir.javaformat.java.FormatterService;
31-
import com.palantir.logsafe.Preconditions;
3235
import java.io.IOException;
3336
import java.net.MalformedURLException;
3437
import java.net.URI;
@@ -50,6 +53,8 @@
5053
final class FormatterProvider {
5154
private static final Logger log = LoggerFactory.getLogger(FormatterProvider.class);
5255

56+
private static final String PLUGIN_ID = "palantir-java-format";
57+
5358
// Cache to avoid creating a URLClassloader every time we want to format from IntelliJ
5459
private final LoadingCache<FormatterCacheKey, Optional<FormatterService>> implementationCache =
5560
Caffeine.newBuilder().maximumSize(1).build(FormatterProvider::createFormatter);
@@ -104,7 +109,9 @@ private static List<Path> getProvidedImplementationUrls(List<URI> implementation
104109

105110
private static List<Path> getBundledImplementationUrls() {
106111
// Load from the jars bundled with the plugin.
107-
Path implDir = PalantirCodeStyleManager.PLUGIN.getPath().toPath().resolve("impl");
112+
IdeaPluginDescriptor ourPlugin = Preconditions.checkNotNull(
113+
PluginManager.getPlugin(PluginId.getId(PLUGIN_ID)), "Couldn't find our own plugin: %s", PLUGIN_ID);
114+
Path implDir = ourPlugin.getPath().toPath().resolve("impl");
108115
log.debug("Using palantir-java-format implementation bundled with plugin: {}", implDir);
109116
return listDirAsUrlsUnchecked(implDir);
110117
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 com.intellij.formatting.service.FormattingNotificationService;
20+
import com.intellij.openapi.project.Project;
21+
22+
class Notifications {
23+
24+
static final String GENERIC_ERROR_NOTIFICATION_GROUP = "palantir-java-format error";
25+
static final String PARSING_ERROR_NOTIFICATION_GROUP = "palantir-java-format parsing error";
26+
static final String PARSING_ERROR_TITLE = PARSING_ERROR_NOTIFICATION_GROUP;
27+
28+
static String parsingErrorMessage(String filename) {
29+
return "palantir-java-format failed. Does " + filename + " have syntax errors?";
30+
}
31+
32+
static void displayParsingErrorNotification(Project project, String filename) {
33+
FormattingNotificationService.getInstance(project)
34+
.reportError(
35+
Notifications.PARSING_ERROR_NOTIFICATION_GROUP,
36+
Notifications.PARSING_ERROR_TITLE,
37+
Notifications.parsingErrorMessage(filename));
38+
}
39+
}

0 commit comments

Comments
 (0)