Skip to content

Commit 4fa9d49

Browse files
plumpycushon
authored andcommitted
Flip the default state for the google-java-format plugin back to disabled.
If a project doesn't have an existing google-java-format.xml, one will be created (with enabled=false) and then a notification will be presented offering to enable the plugin for this project. MOE_MIGRATED_REVID=202181203
1 parent c01b2e4 commit 4fa9d49

File tree

6 files changed

+133
-11
lines changed

6 files changed

+133
-11
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ A [google-java-format IntelliJ
3333
plugin](https://plugins.jetbrains.com/plugin/8527) is available from the plugin
3434
repository.
3535

36-
The plugin will be enabled by default. To disable it in the current project, go
36+
The plugin will be disabled by default. To enable it in the current project, go
3737
to `File→Settings...→google-java-format Settings` (or `IntelliJ
3838
IDEA→Preferences...→Other Settings→google-java-format Settings` on macOS) and
39-
uncheck the `Enable google-java-format` checkbox.
39+
uncheck the `Enable google-java-format` checkbox. (A notification will be
40+
presented when you first open a project offering to do this for you.)
4041

41-
To disable it by default in new projects, use `File→Other Settings→Default
42+
To enable it by default in new projects, use `File→Other Settings→Default
4243
Settings...`.
4344

4445
When enabled, it will replace the normal `Reformat Code` action, which can be

idea_plugin/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ apply plugin: 'java'
3232
intellij {
3333
pluginName = "google-java-format"
3434
updateSinceUntilBuild = true
35-
version = "IC-182.2757.3"
35+
version = "IC-182.3458.5"
3636
}
3737

3838
patchPluginXml {
3939
pluginDescription = "Formats source code using the google-java-format tool. This version of " +
4040
"the plugin uses version ${googleJavaFormatVersion} of the tool."
41-
version = "${googleJavaFormatVersion}.0"
41+
version = "${googleJavaFormatVersion}.1"
4242
sinceBuild = '173'
4343
}
4444

idea_plugin/resources/META-INF/plugin.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,28 @@
1010
reason. It won't crash PyCharm or anything, so whatever. -->
1111
<depends>com.intellij.modules.lang</depends>
1212

13+
<change-notes><![CDATA[
14+
<dl>
15+
<dt>1.6.1</dt>
16+
<dd>Reverted the default to disabled. A notification will be presented the first time you
17+
open a project asking if you want to enable google-java-format for that project.</dd>
18+
<dt>1.6.0</dt>
19+
<dd>Upgraded to google-java-format 1.6. Enabled the plugin by default.</dd>
20+
</dl>
21+
]]></change-notes>
22+
1323
<project-components>
1424
<component>
1525
<implementation-class>
1626
com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller
1727
</implementation-class>
1828
<loadForDefaultProject/>
1929
</component>
30+
<component>
31+
<implementation-class>
32+
com.google.googlejavaformat.intellij.InitialConfigurationComponent
33+
</implementation-class>
34+
</component>
2035
</project-components>
2136

2237
<extensions defaultExtensionNs="com.intellij">

idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.googlejavaformat.intellij;
1818

19+
import com.google.googlejavaformat.intellij.GoogleJavaFormatSettings.EnabledState;
1920
import com.intellij.openapi.options.BaseConfigurable;
2021
import com.intellij.openapi.options.ConfigurationException;
2122
import com.intellij.openapi.options.SearchableConfigurable;
@@ -78,10 +79,17 @@ public JComponent createComponent() {
7879
@Override
7980
public void apply() throws ConfigurationException {
8081
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
81-
settings.setEnabled(enable.isSelected());
82+
settings.setEnabled(enable.isSelected() ? EnabledState.ENABLED : getDisabledState());
8283
settings.setStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert());
8384
}
8485

86+
private EnabledState getDisabledState() {
87+
// The default settings (inherited by new projects) are either 'enabled' or
88+
// 'show notification'. There's no way to default new projects to disabled. If someone wants
89+
// that, we can add another checkbox, I suppose.
90+
return project.isDefault() ? EnabledState.UNKNOWN : EnabledState.DISABLED;
91+
}
92+
8593
@Override
8694
public void reset() {
8795
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);

idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
import javax.annotation.Nullable;
2626

2727
@State(
28-
name = "GoogleJavaFormatSettings",
29-
storages = {@Storage("google-java-format.xml")}
30-
)
28+
name = "GoogleJavaFormatSettings",
29+
storages = {@Storage("google-java-format.xml")})
3130
class GoogleJavaFormatSettings implements PersistentStateComponent<GoogleJavaFormatSettings.State> {
3231

3332
private State state = new State();
@@ -48,13 +47,21 @@ public void loadState(State state) {
4847
}
4948

5049
boolean isEnabled() {
51-
return state.enabled;
50+
return state.enabled.equals(EnabledState.ENABLED);
5251
}
5352

5453
void setEnabled(boolean enabled) {
54+
setEnabled(enabled ? EnabledState.ENABLED : EnabledState.DISABLED);
55+
}
56+
57+
void setEnabled(EnabledState enabled) {
5558
state.enabled = enabled;
5659
}
5760

61+
boolean isUninitialized() {
62+
return state.enabled.equals(EnabledState.UNKNOWN);
63+
}
64+
5865
JavaFormatterOptions.Style getStyle() {
5966
return state.style;
6067
}
@@ -63,8 +70,37 @@ void setStyle(JavaFormatterOptions.Style style) {
6370
state.style = style;
6471
}
6572

73+
enum EnabledState {
74+
UNKNOWN,
75+
ENABLED,
76+
DISABLED;
77+
}
78+
6679
static class State {
67-
public boolean enabled = true;
80+
81+
private EnabledState enabled = EnabledState.UNKNOWN;
6882
public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE;
83+
84+
// enabled used to be a boolean so we use bean property methods for backwards compatibility
85+
public void setEnabled(@Nullable String enabledStr) {
86+
if (enabledStr == null) {
87+
enabled = EnabledState.UNKNOWN;
88+
} else if (Boolean.valueOf(enabledStr)) {
89+
enabled = EnabledState.ENABLED;
90+
} else {
91+
enabled = EnabledState.DISABLED;
92+
}
93+
}
94+
95+
public String getEnabled() {
96+
switch (enabled) {
97+
case ENABLED:
98+
return "true";
99+
case DISABLED:
100+
return "false";
101+
default:
102+
return null;
103+
}
104+
}
69105
}
70106
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2016 Google 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.google.googlejavaformat.intellij;
18+
19+
import com.intellij.notification.Notification;
20+
import com.intellij.notification.NotificationDisplayType;
21+
import com.intellij.notification.NotificationGroup;
22+
import com.intellij.notification.NotificationType;
23+
import com.intellij.openapi.components.ProjectComponent;
24+
import com.intellij.openapi.project.Project;
25+
26+
final class InitialConfigurationComponent implements ProjectComponent {
27+
28+
private static final String NOTIFICATION_TITLE = "Enable google-java-format";
29+
private static final NotificationGroup NOTIFICATION_GROUP =
30+
new NotificationGroup(NOTIFICATION_TITLE, NotificationDisplayType.STICKY_BALLOON, true);
31+
32+
private final Project project;
33+
private final GoogleJavaFormatSettings settings;
34+
35+
public InitialConfigurationComponent(Project project, GoogleJavaFormatSettings settings) {
36+
this.project = project;
37+
this.settings = settings;
38+
}
39+
40+
@Override
41+
public void projectOpened() {
42+
if (settings.isUninitialized()) {
43+
settings.setEnabled(false);
44+
displayNewUserNotification();
45+
}
46+
}
47+
48+
private void displayNewUserNotification() {
49+
Notification notification =
50+
new Notification(
51+
NOTIFICATION_GROUP.getDisplayId(),
52+
NOTIFICATION_TITLE,
53+
"The google-java-format plugin is disabled by default. "
54+
+ "<a href=\"enable\">Enable for this project</a>.",
55+
NotificationType.INFORMATION,
56+
(n, e) -> {
57+
settings.setEnabled(true);
58+
n.expire();
59+
});
60+
notification.notify(project);
61+
}
62+
}

0 commit comments

Comments
 (0)