Be louder about telling users they need to modify their IDE config to use gjf. Right now if your IDE isn't set up properly and you have gjf enabled in a project, you get a single warning. Any uses of the code formatter after that will instead use the built-in formatter. This is confusing to people who don't notice the notification. They think gjf is formatting their code strangely. Instead, let's warn on every single invocation of the formatter until they either fix the configuration issue or disable gjf in the project. Fixes #914 #919. PiperOrigin-RevId: 515722447
diff --git a/idea_plugin/build.gradle.kts b/idea_plugin/build.gradle.kts index 46a697e..a05b0cb 100644 --- a/idea_plugin/build.gradle.kts +++ b/idea_plugin/build.gradle.kts
@@ -14,7 +14,7 @@ * limitations under the License. */ -plugins { id("org.jetbrains.intellij") version "1.13.1" } +plugins { id("org.jetbrains.intellij") version "1.13.2" } apply(plugin = "org.jetbrains.intellij") @@ -37,7 +37,7 @@ tasks { patchPluginXml { - version.set("${googleJavaFormatVersion}.0") + version.set("${googleJavaFormatVersion}.1") sinceBuild.set("213") untilBuild.set("") }
diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java index 50dc5d0..150a739 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java
@@ -44,6 +44,11 @@ @Override protected FormattingTask createFormattingTask(AsyncFormattingRequest request) { Project project = request.getContext().getProject(); + + if (!JreConfigurationChecker.checkJreConfiguration(project)) { + return null; + } + Style style = GoogleJavaFormatSettings.getInstance(project).getStyle(); Formatter formatter = createFormatter(style, request.canChangeWhitespaceOnly()); return new GoogleJavaFormatFormattingTask(formatter, request); @@ -75,8 +80,7 @@ @Override public boolean canFormat(@NotNull PsiFile file) { return JavaFileType.INSTANCE.equals(file.getFileType()) - && GoogleJavaFormatSettings.getInstance(file.getProject()).isEnabled() - && JreConfigurationChecker.checkJreConfiguration(file.getProject()); + && GoogleJavaFormatSettings.getInstance(file.getProject()).isEnabled(); } @Override
diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java index 5b51750..498c885 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java
@@ -35,13 +35,17 @@ @Override public boolean supports(@NotNull PsiFile file) { return JavaFileType.INSTANCE.equals(file.getFileType()) - && GoogleJavaFormatSettings.getInstance(file.getProject()).isEnabled() - && JreConfigurationChecker.checkJreConfiguration(file.getProject()); + && GoogleJavaFormatSettings.getInstance(file.getProject()).isEnabled(); } @Override public @NotNull Runnable processFile(@NotNull PsiFile file) { Project project = file.getProject(); + + if (!JreConfigurationChecker.checkJreConfiguration(file.getProject())) { + return Runnables.doNothing(); + } + PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); Document document = documentManager.getDocument(file);
diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/JreConfigurationChecker.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/JreConfigurationChecker.java index bf8e6ce..5084b6a 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/JreConfigurationChecker.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/JreConfigurationChecker.java
@@ -26,44 +26,44 @@ class JreConfigurationChecker { - private final Supplier<Boolean> hasAccess = Suppliers.memoize(this::checkJreConfiguration); + private static final Supplier<Boolean> hasAccess = + Suppliers.memoize(JreConfigurationChecker::checkJreConfiguration); + private static final Logger logger = Logger.getInstance(JreConfigurationChecker.class); private final Project project; - private final Logger logger = Logger.getInstance(JreConfigurationChecker.class); public JreConfigurationChecker(Project project) { this.project = project; } static boolean checkJreConfiguration(Project project) { - return project.getService(JreConfigurationChecker.class).hasAccess.get(); + var success = hasAccess.get(); + if (!success) { + project.getService(JreConfigurationChecker.class).displayConfigurationErrorNotification(); + } + return success; } /** * Determine whether the JRE is configured to work with the google-java-format plugin. If not, * display a notification with instructions and return false. */ - private boolean checkJreConfiguration() { + private static boolean checkJreConfiguration() { try { - boolean hasAccess = - testClassAccess( - "com.sun.tools.javac.api.JavacTrees", - "com.sun.tools.javac.code.Flags", - "com.sun.tools.javac.file.JavacFileManager", - "com.sun.tools.javac.parser.JavacParser", - "com.sun.tools.javac.tree.JCTree", - "com.sun.tools.javac.util.Log"); - if (!hasAccess) { - displayConfigurationErrorNotification(); - } - return hasAccess; + return testClassAccess( + "com.sun.tools.javac.api.JavacTrees", + "com.sun.tools.javac.code.Flags", + "com.sun.tools.javac.file.JavacFileManager", + "com.sun.tools.javac.parser.JavacParser", + "com.sun.tools.javac.tree.JCTree", + "com.sun.tools.javac.util.Log"); } catch (ClassNotFoundException e) { logger.error("Error checking jre configuration for google-java-format", e); return false; } } - private boolean testClassAccess(String... classNames) throws ClassNotFoundException { + private static boolean testClassAccess(String... classNames) throws ClassNotFoundException { for (String className : classNames) { if (!testClassAccess(className)) { return false; @@ -72,14 +72,16 @@ return true; } - private boolean testClassAccess(String className) throws ClassNotFoundException { + private static boolean testClassAccess(String className) throws ClassNotFoundException { Class<?> klass = Class.forName(className); return klass .getModule() // isExported returns true if the package is either open or exported. Either one is // sufficient // to run the google-java-format code (even though the documentation specifies --add-opens). - .isExported(klass.getPackageName(), getClass().getClassLoader().getUnnamedModule()); + .isExported( + klass.getPackageName(), + JreConfigurationChecker.class.getClassLoader().getUnnamedModule()); } private void displayConfigurationErrorNotification() {
diff --git a/idea_plugin/src/main/resources/META-INF/plugin.xml b/idea_plugin/src/main/resources/META-INF/plugin.xml index 99b286d..d59c3c2 100644 --- a/idea_plugin/src/main/resources/META-INF/plugin.xml +++ b/idea_plugin/src/main/resources/META-INF/plugin.xml
@@ -35,6 +35,10 @@ ]]></description> <change-notes><![CDATA[ <dl> + <dt>1.16.0.1</dt> + <dd>When the plugin isn't configured correctly, show the error on every + format command. Previously it was only being shown at startup and going + unnoticed. <dt>1.16.0.0</dt> <dd>Updated to use google-java-format 1.16.0.</dd> <dd>Use the new IDE formatting APIs for a simplified plugin.</dd>