Skip to content

Commit 41d4254

Browse files
plumpycushon
authored andcommitted
Allow configuring google-java-format on a per-module basis.
Regarding the enum changes: it turns out that IntelliJ serializes the toString() version of the enum rather than the name(). So I had to remove the toString and then create separate Ui* versions that have a toString() for presenting in the UI. MOE_MIGRATED_REVID=134579273
1 parent 8e53c2f commit 41d4254

File tree

6 files changed

+98
-81
lines changed

6 files changed

+98
-81
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2015 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.google.googlejavaformat.java.Formatter;
20+
import com.google.googlejavaformat.java.JavaFormatterOptions;
21+
import com.intellij.psi.PsiFile;
22+
import com.intellij.psi.codeStyle.CodeStyleManager;
23+
import java.util.Optional;
24+
import org.jetbrains.annotations.NotNull;
25+
26+
final class BasicGoogleJavaFormatCodeStyleManager extends GoogleJavaFormatCodeStyleManager {
27+
28+
private final Formatter formatter;
29+
30+
public BasicGoogleJavaFormatCodeStyleManager(
31+
@NotNull CodeStyleManager original, JavaFormatterOptions.Style style) {
32+
super(original);
33+
this.formatter = new Formatter(JavaFormatterOptions.builder().style(style).build());
34+
}
35+
36+
@Override
37+
protected Optional<Formatter> getFormatterForFile(@NotNull PsiFile file) {
38+
return Optional.of(formatter);
39+
}
40+
}

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.common.collect.Range;
2323
import com.google.googlejavaformat.java.Formatter;
2424
import com.google.googlejavaformat.java.FormatterException;
25-
import com.google.googlejavaformat.java.JavaFormatterOptions;
2625
import com.google.googlejavaformat.java.Replacement;
2726
import com.intellij.openapi.application.ApplicationManager;
2827
import com.intellij.openapi.command.WriteCommandAction;
@@ -36,6 +35,7 @@
3635
import com.intellij.util.IncorrectOperationException;
3736
import java.util.Collection;
3837
import java.util.List;
38+
import java.util.Optional;
3939
import java.util.stream.Collectors;
4040
import org.jetbrains.annotations.NotNull;
4141

@@ -45,21 +45,19 @@
4545
*
4646
* @author [email protected] (Brian Chang)
4747
*/
48-
public class GoogleJavaFormatCodeStyleManager extends CodeStyleManagerDecorator {
48+
public abstract class GoogleJavaFormatCodeStyleManager extends CodeStyleManagerDecorator {
4949

50-
private final Formatter formatter;
51-
52-
public GoogleJavaFormatCodeStyleManager(
53-
@NotNull CodeStyleManager original, @NotNull JavaFormatterOptions formatterOptions) {
50+
public GoogleJavaFormatCodeStyleManager(@NotNull CodeStyleManager original) {
5451
super(original);
55-
formatter = new Formatter(formatterOptions);
5652
}
5753

5854
@Override
5955
public void reformatText(@NotNull PsiFile file, int startOffset, int endOffset)
6056
throws IncorrectOperationException {
61-
if (StdFileTypes.JAVA.equals(file.getFileType()) && useGoogleFormatterForFile(file)) {
62-
formatInternal(file, ImmutableList.of(Range.closedOpen(startOffset, endOffset)));
57+
Optional<Formatter> formatter = getFormatterForFile(file);
58+
if (formatter.isPresent() && StdFileTypes.JAVA.equals(file.getFileType())) {
59+
formatInternal(
60+
formatter.get(), file, ImmutableList.of(Range.closedOpen(startOffset, endOffset)));
6361
} else {
6462
super.reformatText(file, startOffset, endOffset);
6563
}
@@ -68,8 +66,9 @@ public void reformatText(@NotNull PsiFile file, int startOffset, int endOffset)
6866
@Override
6967
public void reformatText(@NotNull PsiFile file, @NotNull Collection<TextRange> ranges)
7068
throws IncorrectOperationException {
71-
if (StdFileTypes.JAVA.equals(file.getFileType()) && useGoogleFormatterForFile(file)) {
72-
formatInternal(file, convertToRanges(ranges));
69+
Optional<Formatter> formatter = getFormatterForFile(file);
70+
if (formatter.isPresent() && StdFileTypes.JAVA.equals(file.getFileType())) {
71+
formatInternal(formatter.get(), file, convertToRanges(ranges));
7372
} else {
7473
super.reformatText(file, ranges);
7574
}
@@ -78,18 +77,21 @@ public void reformatText(@NotNull PsiFile file, @NotNull Collection<TextRange> r
7877
@Override
7978
public void reformatTextWithContext(@NotNull PsiFile file, @NotNull Collection<TextRange> ranges)
8079
throws IncorrectOperationException {
81-
if (StdFileTypes.JAVA.equals(file.getFileType()) && useGoogleFormatterForFile(file)) {
82-
formatInternal(file, convertToRanges(ranges));
80+
Optional<Formatter> formatter = getFormatterForFile(file);
81+
if (formatter.isPresent() && StdFileTypes.JAVA.equals(file.getFileType())) {
82+
formatInternal(formatter.get(), file, convertToRanges(ranges));
8383
} else {
8484
super.reformatTextWithContext(file, ranges);
8585
}
8686
}
8787

88-
protected boolean useGoogleFormatterForFile(@NotNull PsiFile file) {
89-
return true;
90-
}
88+
/**
89+
* Get the {@link Formatter} to be used with the given file, or absent to use the built-in
90+
* IntelliJ formatter.
91+
*/
92+
protected abstract Optional<Formatter> getFormatterForFile(PsiFile file);
9193

92-
private void formatInternal(PsiFile file, List<Range<Integer>> ranges)
94+
private void formatInternal(Formatter formatter, PsiFile file, List<Range<Integer>> ranges)
9395
throws IncorrectOperationException {
9496
ApplicationManager.getApplication().assertWriteAccessAllowed();
9597
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,30 @@ public JComponent createComponent() {
7979
public void apply() throws ConfigurationException {
8080
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
8181
settings.setEnabled(enable.isSelected());
82-
settings.setStyle((FormatterStyle) styleComboBox.getSelectedItem());
82+
settings.setStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert());
8383
}
8484

8585
@Override
8686
public void reset() {
8787
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
8888
enable.setSelected(settings.isEnabled());
89-
styleComboBox.setSelectedItem(settings.getStyle());
89+
styleComboBox.setSelectedItem(UiFormatterStyle.convert(settings.getStyle()));
9090
}
9191

9292
@Override
9393
public boolean isModified() {
9494
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
9595
return enable.isSelected() != settings.isEnabled()
96-
|| !styleComboBox.getSelectedItem().equals(settings.getStyle());
96+
|| !styleComboBox.getSelectedItem().equals(UiFormatterStyle.convert(settings.getStyle()));
9797
}
9898

9999
@Override
100100
public void disposeUIResources() {}
101101

102102
private void createUIComponents() {
103-
styleComboBox = new ComboBox<>(FormatterStyle.values());
103+
styleComboBox = new ComboBox<>(UiFormatterStyle.values());
104104
}
105105

106-
// IntelliJ's UI designer generated this ugly code and then google-java-format made it even
107-
// uglier. C'est la vie.
108106
{
109107
// GUI initializer generated by IntelliJ IDEA GUI Designer
110108
// >>> IMPORTANT!! <<<

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

Lines changed: 13 additions & 42 deletions
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.java.JavaFormatterOptions;
1920
import com.intellij.lifecycle.PeriodicalTasksCloser;
2021
import com.intellij.openapi.components.AbstractProjectComponent;
2122
import com.intellij.openapi.components.PersistentStateComponent;
@@ -31,8 +32,7 @@
3132
class GoogleJavaFormatSettings extends AbstractProjectComponent
3233
implements PersistentStateComponent<GoogleJavaFormatSettings.State> {
3334

34-
private boolean enabled = false;
35-
private FormatterStyle formatterStyle = FormatterStyle.GOOGLE;
35+
private State state = new State();
3636

3737
protected GoogleJavaFormatSettings(Project project) {
3838
super(project);
@@ -46,72 +46,43 @@ static GoogleJavaFormatSettings getInstance(Project project) {
4646
@Nullable
4747
@Override
4848
public State getState() {
49-
State state = new State();
50-
state.setEnabled(enabled);
51-
state.setStyle(formatterStyle);
5249
return state;
5350
}
5451

5552
@Override
5653
public void loadState(State state) {
57-
setEnabled(state.isEnabled());
58-
setStyle(state.getStyle());
54+
this.state = state;
5955
}
6056

6157
boolean isEnabled() {
62-
return enabled;
58+
return state.enabled;
6359
}
6460

6561
void setEnabled(boolean enabled) {
66-
this.enabled = enabled;
67-
updateFormatterState();
62+
state.enabled = enabled;
6863
}
6964

70-
FormatterStyle getStyle() {
71-
return formatterStyle;
65+
JavaFormatterOptions.Style getStyle() {
66+
return state.style;
7267
}
7368

74-
void setStyle(FormatterStyle formatterStyle) {
75-
// formatterStyle can be null when users upgrade to the first version of the plugin with style
76-
// support (since it was never saved before). If so, keep the default value.
77-
if (formatterStyle == null) {
78-
this.formatterStyle = FormatterStyle.GOOGLE;
79-
} else {
80-
this.formatterStyle = formatterStyle;
81-
}
69+
void setStyle(JavaFormatterOptions.Style style) {
70+
state.style = style;
8271
updateFormatterState();
8372
}
8473

8574
private void updateFormatterState() {
86-
if (enabled) {
75+
if (state.enabled) {
8776
GoogleJavaFormatInstaller.installFormatter(
8877
myProject,
89-
(delegate) ->
90-
new GoogleJavaFormatCodeStyleManager(
91-
delegate, formatterStyle.getJavaFormatterOptions()));
78+
(delegate) -> new BasicGoogleJavaFormatCodeStyleManager(delegate, state.style));
9279
} else {
9380
GoogleJavaFormatInstaller.removeFormatter(myProject);
9481
}
9582
}
9683

9784
static class State {
98-
private boolean enabled = false;
99-
private FormatterStyle formatterStyle = FormatterStyle.GOOGLE;
100-
101-
boolean isEnabled() {
102-
return enabled;
103-
}
104-
105-
void setEnabled(boolean enabled) {
106-
this.enabled = enabled;
107-
}
108-
109-
FormatterStyle getStyle() {
110-
return formatterStyle;
111-
}
112-
113-
void setStyle(FormatterStyle formatterStyle) {
114-
this.formatterStyle = formatterStyle;
115-
}
85+
public boolean enabled = false;
86+
public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE;
11687
}
11788
}

idea_plugin/src/com/google/googlejavaformat/intellij/FormatterStyle.java renamed to idea_plugin/src/com/google/googlejavaformat/intellij/UiFormatterStyle.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,35 @@
1818

1919
import com.google.googlejavaformat.java.JavaFormatterOptions;
2020
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
21+
import java.util.Arrays;
22+
import java.util.Objects;
2123

22-
/**
23-
* Configuration options for the formatting style.
24-
*/
25-
public enum FormatterStyle {
24+
/** Configuration options for the formatting style. */
25+
public enum UiFormatterStyle {
2626
GOOGLE("Default Google Java style", Style.GOOGLE),
2727
AOSP("Android Open Source Project (AOSP) style", Style.AOSP);
2828

2929
private final String description;
30-
private final JavaFormatterOptions javaFormatterOptions;
30+
private final JavaFormatterOptions.Style style;
3131

32-
FormatterStyle(String description, JavaFormatterOptions.Style style) {
32+
UiFormatterStyle(String description, JavaFormatterOptions.Style style) {
3333
this.description = description;
34-
this.javaFormatterOptions = JavaFormatterOptions.builder().style(style).build();
34+
this.style = style;
3535
}
3636

3737
@Override
3838
public String toString() {
3939
return description;
4040
}
4141

42-
public JavaFormatterOptions getJavaFormatterOptions() {
43-
return javaFormatterOptions;
42+
public JavaFormatterOptions.Style convert() {
43+
return style;
44+
}
45+
46+
static UiFormatterStyle convert(JavaFormatterOptions.Style style) {
47+
return Arrays.stream(UiFormatterStyle.values())
48+
.filter(value -> Objects.equals(value.style, style))
49+
.findFirst()
50+
.get();
4451
}
4552
}

idea_plugin/test/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManagerTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.intellij.openapi.util.TextRange;
2525
import com.intellij.psi.codeStyle.CodeStyleManager;
2626
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
27-
2827
import java.net.URL;
2928
import java.net.URLClassLoader;
3029

@@ -56,8 +55,8 @@ protected void setUp() throws Exception {
5655
public void testFormatFile() {
5756
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
5857
final GoogleJavaFormatCodeStyleManager googleJavaFormat =
59-
new GoogleJavaFormatCodeStyleManager(
60-
codeStyleManager, JavaFormatterOptions.defaultOptions());
58+
new BasicGoogleJavaFormatCodeStyleManager(
59+
codeStyleManager, JavaFormatterOptions.Style.GOOGLE);
6160
myFixture.configureByText(
6261
StdFileTypes.JAVA,
6362
"public class Test {public static void main(String[]args){System.out.println();}}");
@@ -79,8 +78,8 @@ public void testFormatFile() {
7978
public void testDontFormatNonJavaFile() {
8079
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
8180
final GoogleJavaFormatCodeStyleManager googleJavaFormat =
82-
new GoogleJavaFormatCodeStyleManager(
83-
codeStyleManager, JavaFormatterOptions.defaultOptions());
81+
new BasicGoogleJavaFormatCodeStyleManager(
82+
codeStyleManager, JavaFormatterOptions.Style.GOOGLE);
8483
myFixture.configureByText(
8584
StdFileTypes.PLAIN_TEXT,
8685
"public class Test {public static void main(String[]args){System.out.println();}}");
@@ -96,8 +95,8 @@ public void testDontFormatNonJavaFile() {
9695
public void testFormatRanges() {
9796
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
9897
final GoogleJavaFormatCodeStyleManager googleJavaFormat =
99-
new GoogleJavaFormatCodeStyleManager(
100-
codeStyleManager, JavaFormatterOptions.defaultOptions());
98+
new BasicGoogleJavaFormatCodeStyleManager(
99+
codeStyleManager, JavaFormatterOptions.Style.GOOGLE);
101100
String content =
102101
join(
103102
"public class Test {", //

0 commit comments

Comments
 (0)