Skip to content

Commit

Permalink
Flip the default state for the google-java-format plugin back to disa…
Browse files Browse the repository at this point in the history
…bled.

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
  • Loading branch information
plumpy authored and cushon committed Jun 26, 2018
1 parent c01b2e4 commit 4fa9d49
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 11 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -33,12 +33,13 @@ A [google-java-format IntelliJ
plugin](https://plugins.jetbrains.com/plugin/8527) is available from the plugin plugin](https://plugins.jetbrains.com/plugin/8527) is available from the plugin
repository. repository.


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


To disable it by default in new projects, use `File→Other Settings→Default To enable it by default in new projects, use `File→Other Settings→Default
Settings...`. Settings...`.


When enabled, it will replace the normal `Reformat Code` action, which can be When enabled, it will replace the normal `Reformat Code` action, which can be
Expand Down
4 changes: 2 additions & 2 deletions idea_plugin/build.gradle
Expand Up @@ -32,13 +32,13 @@ apply plugin: 'java'
intellij { intellij {
pluginName = "google-java-format" pluginName = "google-java-format"
updateSinceUntilBuild = true updateSinceUntilBuild = true
version = "IC-182.2757.3" version = "IC-182.3458.5"
} }


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


Expand Down
15 changes: 15 additions & 0 deletions idea_plugin/resources/META-INF/plugin.xml
Expand Up @@ -10,13 +10,28 @@
reason. It won't crash PyCharm or anything, so whatever. --> reason. It won't crash PyCharm or anything, so whatever. -->
<depends>com.intellij.modules.lang</depends> <depends>com.intellij.modules.lang</depends>


<change-notes><![CDATA[
<dl>
<dt>1.6.1</dt>
<dd>Reverted the default to disabled. A notification will be presented the first time you
open a project asking if you want to enable google-java-format for that project.</dd>
<dt>1.6.0</dt>
<dd>Upgraded to google-java-format 1.6. Enabled the plugin by default.</dd>
</dl>
]]></change-notes>

<project-components> <project-components>
<component> <component>
<implementation-class> <implementation-class>
com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller
</implementation-class> </implementation-class>
<loadForDefaultProject/> <loadForDefaultProject/>
</component> </component>
<component>
<implementation-class>
com.google.googlejavaformat.intellij.InitialConfigurationComponent
</implementation-class>
</component>
</project-components> </project-components>


<extensions defaultExtensionNs="com.intellij"> <extensions defaultExtensionNs="com.intellij">
Expand Down
Expand Up @@ -16,6 +16,7 @@


package com.google.googlejavaformat.intellij; package com.google.googlejavaformat.intellij;


import com.google.googlejavaformat.intellij.GoogleJavaFormatSettings.EnabledState;
import com.intellij.openapi.options.BaseConfigurable; import com.intellij.openapi.options.BaseConfigurable;
import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable; import com.intellij.openapi.options.SearchableConfigurable;
Expand Down Expand Up @@ -78,10 +79,17 @@ public JComponent createComponent() {
@Override @Override
public void apply() throws ConfigurationException { public void apply() throws ConfigurationException {
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
settings.setEnabled(enable.isSelected()); settings.setEnabled(enable.isSelected() ? EnabledState.ENABLED : getDisabledState());
settings.setStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert()); settings.setStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert());
} }


private EnabledState getDisabledState() {
// The default settings (inherited by new projects) are either 'enabled' or
// 'show notification'. There's no way to default new projects to disabled. If someone wants
// that, we can add another checkbox, I suppose.
return project.isDefault() ? EnabledState.UNKNOWN : EnabledState.DISABLED;
}

@Override @Override
public void reset() { public void reset() {
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
Expand Down
Expand Up @@ -25,9 +25,8 @@
import javax.annotation.Nullable; import javax.annotation.Nullable;


@State( @State(
name = "GoogleJavaFormatSettings", name = "GoogleJavaFormatSettings",
storages = {@Storage("google-java-format.xml")} storages = {@Storage("google-java-format.xml")})
)
class GoogleJavaFormatSettings implements PersistentStateComponent<GoogleJavaFormatSettings.State> { class GoogleJavaFormatSettings implements PersistentStateComponent<GoogleJavaFormatSettings.State> {


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


boolean isEnabled() { boolean isEnabled() {
return state.enabled; return state.enabled.equals(EnabledState.ENABLED);
} }


void setEnabled(boolean enabled) { void setEnabled(boolean enabled) {
setEnabled(enabled ? EnabledState.ENABLED : EnabledState.DISABLED);
}

void setEnabled(EnabledState enabled) {
state.enabled = enabled; state.enabled = enabled;
} }


boolean isUninitialized() {
return state.enabled.equals(EnabledState.UNKNOWN);
}

JavaFormatterOptions.Style getStyle() { JavaFormatterOptions.Style getStyle() {
return state.style; return state.style;
} }
Expand All @@ -63,8 +70,37 @@ void setStyle(JavaFormatterOptions.Style style) {
state.style = style; state.style = style;
} }


enum EnabledState {
UNKNOWN,
ENABLED,
DISABLED;
}

static class State { static class State {
public boolean enabled = true;
private EnabledState enabled = EnabledState.UNKNOWN;
public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE; public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE;

// enabled used to be a boolean so we use bean property methods for backwards compatibility
public void setEnabled(@Nullable String enabledStr) {
if (enabledStr == null) {
enabled = EnabledState.UNKNOWN;
} else if (Boolean.valueOf(enabledStr)) {
enabled = EnabledState.ENABLED;
} else {
enabled = EnabledState.DISABLED;
}
}

public String getEnabled() {
switch (enabled) {
case ENABLED:
return "true";
case DISABLED:
return "false";
default:
return null;
}
}
} }
} }
@@ -0,0 +1,62 @@
/*
* Copyright 2016 Google 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.google.googlejavaformat.intellij;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;

final class InitialConfigurationComponent implements ProjectComponent {

private static final String NOTIFICATION_TITLE = "Enable google-java-format";
private static final NotificationGroup NOTIFICATION_GROUP =
new NotificationGroup(NOTIFICATION_TITLE, NotificationDisplayType.STICKY_BALLOON, true);

private final Project project;
private final GoogleJavaFormatSettings settings;

public InitialConfigurationComponent(Project project, GoogleJavaFormatSettings settings) {
this.project = project;
this.settings = settings;
}

@Override
public void projectOpened() {
if (settings.isUninitialized()) {
settings.setEnabled(false);
displayNewUserNotification();
}
}

private void displayNewUserNotification() {
Notification notification =
new Notification(
NOTIFICATION_GROUP.getDisplayId(),
NOTIFICATION_TITLE,
"The google-java-format plugin is disabled by default. "
+ "<a href=\"enable\">Enable for this project</a>.",
NotificationType.INFORMATION,
(n, e) -> {
settings.setEnabled(true);
n.expire();
});
notification.notify(project);
}
}

0 comments on commit 4fa9d49

Please sign in to comment.