Skip to content

Commit 401d5c9

Browse files
plumpygoogle-java-format Team
authored and
google-java-format Team
committedMar 10, 2023
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
1 parent 4c1aeff commit 401d5c9

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed
 

‎idea_plugin/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
plugins { id("org.jetbrains.intellij") version "1.13.1" }
17+
plugins { id("org.jetbrains.intellij") version "1.13.2" }
1818

1919
apply(plugin = "org.jetbrains.intellij")
2020

@@ -37,7 +37,7 @@ intellij {
3737

3838
tasks {
3939
patchPluginXml {
40-
version.set("${googleJavaFormatVersion}.0")
40+
version.set("${googleJavaFormatVersion}.1")
4141
sinceBuild.set("213")
4242
untilBuild.set("")
4343
}

‎idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public class GoogleJavaFormatFormattingService extends AsyncDocumentFormattingSe
4444
@Override
4545
protected FormattingTask createFormattingTask(AsyncFormattingRequest request) {
4646
Project project = request.getContext().getProject();
47+
48+
if (!JreConfigurationChecker.checkJreConfiguration(project)) {
49+
return null;
50+
}
51+
4752
Style style = GoogleJavaFormatSettings.getInstance(project).getStyle();
4853
Formatter formatter = createFormatter(style, request.canChangeWhitespaceOnly());
4954
return new GoogleJavaFormatFormattingTask(formatter, request);
@@ -75,8 +80,7 @@ private static Formatter createFormatter(Style style, boolean canChangeWhiteSpac
7580
@Override
7681
public boolean canFormat(@NotNull PsiFile file) {
7782
return JavaFileType.INSTANCE.equals(file.getFileType())
78-
&& GoogleJavaFormatSettings.getInstance(file.getProject()).isEnabled()
79-
&& JreConfigurationChecker.checkJreConfiguration(file.getProject());
83+
&& GoogleJavaFormatSettings.getInstance(file.getProject()).isEnabled();
8084
}
8185

8286
@Override

‎idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ public class GoogleJavaFormatImportOptimizer implements ImportOptimizer {
3535
@Override
3636
public boolean supports(@NotNull PsiFile file) {
3737
return JavaFileType.INSTANCE.equals(file.getFileType())
38-
&& GoogleJavaFormatSettings.getInstance(file.getProject()).isEnabled()
39-
&& JreConfigurationChecker.checkJreConfiguration(file.getProject());
38+
&& GoogleJavaFormatSettings.getInstance(file.getProject()).isEnabled();
4039
}
4140

4241
@Override
4342
public @NotNull Runnable processFile(@NotNull PsiFile file) {
4443
Project project = file.getProject();
44+
45+
if (!JreConfigurationChecker.checkJreConfiguration(file.getProject())) {
46+
return Runnables.doNothing();
47+
}
48+
4549
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
4650
Document document = documentManager.getDocument(file);
4751

‎idea_plugin/src/main/java/com/google/googlejavaformat/intellij/JreConfigurationChecker.java

+21-19
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,44 @@
2626

2727
class JreConfigurationChecker {
2828

29-
private final Supplier<Boolean> hasAccess = Suppliers.memoize(this::checkJreConfiguration);
29+
private static final Supplier<Boolean> hasAccess =
30+
Suppliers.memoize(JreConfigurationChecker::checkJreConfiguration);
31+
private static final Logger logger = Logger.getInstance(JreConfigurationChecker.class);
3032

3133
private final Project project;
32-
private final Logger logger = Logger.getInstance(JreConfigurationChecker.class);
3334

3435
public JreConfigurationChecker(Project project) {
3536
this.project = project;
3637
}
3738

3839
static boolean checkJreConfiguration(Project project) {
39-
return project.getService(JreConfigurationChecker.class).hasAccess.get();
40+
var success = hasAccess.get();
41+
if (!success) {
42+
project.getService(JreConfigurationChecker.class).displayConfigurationErrorNotification();
43+
}
44+
return success;
4045
}
4146

4247
/**
4348
* Determine whether the JRE is configured to work with the google-java-format plugin. If not,
4449
* display a notification with instructions and return false.
4550
*/
46-
private boolean checkJreConfiguration() {
51+
private static boolean checkJreConfiguration() {
4752
try {
48-
boolean hasAccess =
49-
testClassAccess(
50-
"com.sun.tools.javac.api.JavacTrees",
51-
"com.sun.tools.javac.code.Flags",
52-
"com.sun.tools.javac.file.JavacFileManager",
53-
"com.sun.tools.javac.parser.JavacParser",
54-
"com.sun.tools.javac.tree.JCTree",
55-
"com.sun.tools.javac.util.Log");
56-
if (!hasAccess) {
57-
displayConfigurationErrorNotification();
58-
}
59-
return hasAccess;
53+
return testClassAccess(
54+
"com.sun.tools.javac.api.JavacTrees",
55+
"com.sun.tools.javac.code.Flags",
56+
"com.sun.tools.javac.file.JavacFileManager",
57+
"com.sun.tools.javac.parser.JavacParser",
58+
"com.sun.tools.javac.tree.JCTree",
59+
"com.sun.tools.javac.util.Log");
6060
} catch (ClassNotFoundException e) {
6161
logger.error("Error checking jre configuration for google-java-format", e);
6262
return false;
6363
}
6464
}
6565

66-
private boolean testClassAccess(String... classNames) throws ClassNotFoundException {
66+
private static boolean testClassAccess(String... classNames) throws ClassNotFoundException {
6767
for (String className : classNames) {
6868
if (!testClassAccess(className)) {
6969
return false;
@@ -72,14 +72,16 @@ private boolean testClassAccess(String... classNames) throws ClassNotFoundExcept
7272
return true;
7373
}
7474

75-
private boolean testClassAccess(String className) throws ClassNotFoundException {
75+
private static boolean testClassAccess(String className) throws ClassNotFoundException {
7676
Class<?> klass = Class.forName(className);
7777
return klass
7878
.getModule()
7979
// isExported returns true if the package is either open or exported. Either one is
8080
// sufficient
8181
// to run the google-java-format code (even though the documentation specifies --add-opens).
82-
.isExported(klass.getPackageName(), getClass().getClassLoader().getUnnamedModule());
82+
.isExported(
83+
klass.getPackageName(),
84+
JreConfigurationChecker.class.getClassLoader().getUnnamedModule());
8385
}
8486

8587
private void displayConfigurationErrorNotification() {

‎idea_plugin/src/main/resources/META-INF/plugin.xml

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
]]></description>
3636
<change-notes><![CDATA[
3737
<dl>
38+
<dt>1.16.0.1</dt>
39+
<dd>When the plugin isn't configured correctly, show the error on every
40+
format command. Previously it was only being shown at startup and going
41+
unnoticed.
3842
<dt>1.16.0.0</dt>
3943
<dd>Updated to use google-java-format 1.16.0.</dd>
4044
<dd>Use the new IDE formatting APIs for a simplified plugin.</dd>

0 commit comments

Comments
 (0)