Skip to content

Commit

Permalink
Move the plugin to the FormattingService & ImportOptimizer APIs (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
crogoz authored Apr 26, 2024
1 parent 2ec339a commit 834c6c0
Show file tree
Hide file tree
Showing 19 changed files with 697 additions and 471 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ generated/
# Blueprint theme
__init__.pyc

node_modules/
node_modules/
/.profileconfig.json
6 changes: 6 additions & 0 deletions .palantir/revapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ acceptedBreaks:
old: "method void com.palantir.javaformat.gradle.ConfigureJavaFormatterXml::configureFormatOnSave(groovy.util.Node)"
new: "method void com.palantir.javaformat.gradle.ConfigureJavaFormatterXml::configureFormatOnSave(groovy.util.Node)"
justification: "Not public api, not split over multiple jars"
"2.43.0":
com.palantir.javaformat:palantir-java-format-spi:
- code: "java.method.addedToInterface"
new: "method java.lang.String com.palantir.javaformat.java.FormatterService::fixImports(java.lang.String)\
\ throws com.palantir.javaformat.java.FormatterException"
justification: "new fixImports only method added to spi"
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buildscript {
}

plugins {
id "org.jetbrains.intellij" version "1.3.0" apply false
id "org.jetbrains.intellij" version "1.17.3" apply false
id 'org.jetbrains.gradle.plugin.idea-ext' version "1.1.1"
}

Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1078.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Fix palantir-java-format not running on IntelliJ >=2024.1 by moving to new APIs.
links:
- https://github.com/palantir/palantir-java-format/pull/1078
5 changes: 3 additions & 2 deletions idea-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def name = "palantir-java-format"
intellij {
pluginName = name
updateSinceUntilBuild = true
version = "IU-202.6397.94"
version = "2024.1"
plugins = ['java']
}

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

Expand Down Expand Up @@ -73,6 +73,7 @@ dependencies {

testImplementation 'org.assertj:assertj-core'
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.junit.platform:junit-platform-launcher'
}

tasks.withType(JavaCompile).configureEach {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JdkUtil;
import com.intellij.openapi.projectRoots.Sdk;
Expand All @@ -28,7 +32,6 @@
import com.intellij.openapi.util.SystemInfo;
import com.palantir.javaformat.bootstrap.BootstrappingFormatterService;
import com.palantir.javaformat.java.FormatterService;
import com.palantir.logsafe.Preconditions;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
Expand All @@ -50,6 +53,8 @@
final class FormatterProvider {
private static final Logger log = LoggerFactory.getLogger(FormatterProvider.class);

private static final String PLUGIN_ID = "palantir-java-format";

// Cache to avoid creating a URLClassloader every time we want to format from IntelliJ
private final LoadingCache<FormatterCacheKey, Optional<FormatterService>> implementationCache =
Caffeine.newBuilder().maximumSize(1).build(FormatterProvider::createFormatter);
Expand Down Expand Up @@ -104,7 +109,9 @@ private static List<Path> getProvidedImplementationUrls(List<URI> implementation

private static List<Path> getBundledImplementationUrls() {
// Load from the jars bundled with the plugin.
Path implDir = PalantirCodeStyleManager.PLUGIN.getPath().toPath().resolve("impl");
IdeaPluginDescriptor ourPlugin = Preconditions.checkNotNull(
PluginManager.getPlugin(PluginId.getId(PLUGIN_ID)), "Couldn't find our own plugin: %s", PLUGIN_ID);
Path implDir = ourPlugin.getPath().toPath().resolve("impl");
log.debug("Using palantir-java-format implementation bundled with plugin: {}", implDir);
return listDirAsUrlsUnchecked(implDir);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* (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 com.intellij.formatting.service.FormattingNotificationService;
import com.intellij.openapi.project.Project;

class Notifications {

static final String GENERIC_ERROR_NOTIFICATION_GROUP = "palantir-java-format error";
static final String PARSING_ERROR_NOTIFICATION_GROUP = "palantir-java-format parsing error";
static final String PARSING_ERROR_TITLE = PARSING_ERROR_NOTIFICATION_GROUP;

static String parsingErrorMessage(String filename) {
return "palantir-java-format failed. Does " + filename + " have syntax errors?";
}

static void displayParsingErrorNotification(Project project, String filename) {
FormattingNotificationService.getInstance(project)
.reportError(
Notifications.PARSING_ERROR_NOTIFICATION_GROUP,
Notifications.PARSING_ERROR_TITLE,
Notifications.parsingErrorMessage(filename));
}
}
Loading

0 comments on commit 834c6c0

Please sign in to comment.