Skip to content

Commit dac314e

Browse files
plumpycushon
authored andcommitted
Fix installing the formatter for users with the defaults.
If they're using the defaults, there won't be a settings file, so loadState() is never called. MOE_MIGRATED_REVID=190972759
1 parent d77cda5 commit dac314e

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

idea_plugin/resources/META-INF/plugin.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<project-components>
2222
<component>
2323
<implementation-class>
24-
com.google.googlejavaformat.intellij.GoogleJavaFormatSettings
24+
com.google.googlejavaformat.intellij.GoogleJavaFormatInstaller
2525
</implementation-class>
2626
<loadForDefaultProject/>
2727
</component>
@@ -31,6 +31,7 @@
3131
<projectConfigurable instance="com.google.googlejavaformat.intellij.GoogleJavaFormatConfigurable"
3232
id="google-java-format.settings"
3333
displayName="google-java-format Settings"/>
34+
<projectService serviceImplementation="com.google.googlejavaformat.intellij.GoogleJavaFormatSettings"/>
3435
</extensions>
3536

3637
</idea-plugin>

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,31 @@
1616

1717
package com.google.googlejavaformat.intellij;
1818

19+
import com.intellij.openapi.components.ProjectComponent;
1920
import com.intellij.openapi.project.Project;
2021
import com.intellij.psi.codeStyle.CodeStyleManager;
2122
import org.picocontainer.MutablePicoContainer;
2223

2324
/**
24-
* A utility class to replace (and revert) the default IntelliJ {@link CodeStyleManager} with one
25-
* that formats via google-java-format.
25+
* A component that replaces the default IntelliJ {@link CodeStyleManager} with one that formats via
26+
* google-java-format.
2627
*/
27-
final class GoogleJavaFormatInstaller {
28+
final class GoogleJavaFormatInstaller implements ProjectComponent {
2829

2930
private static final String CODE_STYLE_MANAGER_KEY = CodeStyleManager.class.getName();
3031

31-
private GoogleJavaFormatInstaller() {}
32+
private final Project project;
3233

33-
public static void installFormatter(Project project) {
34+
private GoogleJavaFormatInstaller(Project project) {
35+
this.project = project;
36+
}
37+
38+
@Override
39+
public void projectOpened() {
40+
installFormatter(project);
41+
}
42+
43+
private static void installFormatter(Project project) {
3444
CodeStyleManager currentManager = CodeStyleManager.getInstance(project);
3545

3646
if (currentManager instanceof GoogleJavaFormatCodeStyleManager) {
@@ -40,13 +50,6 @@ public static void installFormatter(Project project) {
4050
setManager(project, new GoogleJavaFormatCodeStyleManager(currentManager));
4151
}
4252

43-
public static void removeFormatter(Project project) {
44-
CodeStyleManager currentManager = CodeStyleManager.getInstance(project);
45-
if (currentManager instanceof GoogleJavaFormatCodeStyleManager) {
46-
setManager(project, ((GoogleJavaFormatCodeStyleManager) currentManager).getDelegate());
47-
}
48-
}
49-
5053
private static void setManager(Project project, CodeStyleManager newManager) {
5154
if (newManager != null) {
5255
MutablePicoContainer container = (MutablePicoContainer) project.getPicoContainer();

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
package com.google.googlejavaformat.intellij;
1818

1919
import com.google.googlejavaformat.java.JavaFormatterOptions;
20-
import com.intellij.lifecycle.PeriodicalTasksCloser;
21-
import com.intellij.openapi.components.AbstractProjectComponent;
2220
import com.intellij.openapi.components.PersistentStateComponent;
21+
import com.intellij.openapi.components.ServiceManager;
2322
import com.intellij.openapi.components.State;
2423
import com.intellij.openapi.components.Storage;
2524
import com.intellij.openapi.project.Project;
@@ -29,18 +28,12 @@
2928
name = "GoogleJavaFormatSettings",
3029
storages = {@Storage("google-java-format.xml")}
3130
)
32-
class GoogleJavaFormatSettings extends AbstractProjectComponent
33-
implements PersistentStateComponent<GoogleJavaFormatSettings.State> {
31+
class GoogleJavaFormatSettings implements PersistentStateComponent<GoogleJavaFormatSettings.State> {
3432

3533
private State state = new State();
3634

37-
protected GoogleJavaFormatSettings(Project project) {
38-
super(project);
39-
}
40-
4135
static GoogleJavaFormatSettings getInstance(Project project) {
42-
return PeriodicalTasksCloser.getInstance()
43-
.safeGetComponent(project, GoogleJavaFormatSettings.class);
36+
return ServiceManager.getService(project, GoogleJavaFormatSettings.class);
4437
}
4538

4639
@Nullable
@@ -52,7 +45,6 @@ public State getState() {
5245
@Override
5346
public void loadState(State state) {
5447
this.state = state;
55-
updateFormatterState();
5648
}
5749

5850
boolean isEnabled() {
@@ -69,15 +61,6 @@ JavaFormatterOptions.Style getStyle() {
6961

7062
void setStyle(JavaFormatterOptions.Style style) {
7163
state.style = style;
72-
updateFormatterState();
73-
}
74-
75-
private void updateFormatterState() {
76-
if (state.enabled) {
77-
GoogleJavaFormatInstaller.installFormatter(myProject);
78-
} else {
79-
GoogleJavaFormatInstaller.removeFormatter(myProject);
80-
}
8164
}
8265

8366
static class State {

0 commit comments

Comments
 (0)