Skip to content

Commit

Permalink
Add AOSP option to the IntelliJ plugin
Browse files Browse the repository at this point in the history
MOE_MIGRATED_REVID=134426986
  • Loading branch information
plumpy authored and cushon committed Oct 3, 2016
1 parent f9b5473 commit c79f897
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.google.googlejavaformat.java.JavaFormatterOptions;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;

/**
* Configuration options for the formatting style.
*/
public enum FormatterStyle {
GOOGLE("Default Google Java style", Style.GOOGLE),
AOSP("Android Open Source Project (AOSP) style", Style.AOSP);

private final String description;
private final JavaFormatterOptions javaFormatterOptions;

FormatterStyle(String description, JavaFormatterOptions.Style style) {
this.description = description;
this.javaFormatterOptions = JavaFormatterOptions.builder().style(style).build();
}

@Override
public String toString() {
return description;
}

public JavaFormatterOptions getJavaFormatterOptions() {
return javaFormatterOptions;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.google.googlejavaformat.intellij.GoogleJavaFormatConfigurable">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
Expand All @@ -10,17 +10,31 @@
<children>
<component id="4a87f" class="javax.swing.JCheckBox" binding="enable" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Enable google-java-format"/>
</properties>
</component>
<vspacer id="19e83">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="c93e1" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Code style"/>
</properties>
</component>
<component id="31761" class="javax.swing.JComboBox" binding="styleComboBox" custom-create="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="1" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,26 @@
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import java.awt.Insets;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

class GoogleJavaFormatConfigurable extends BaseConfigurable
implements SearchableConfigurable {
class GoogleJavaFormatConfigurable extends BaseConfigurable implements SearchableConfigurable {

private final Project project;
private JPanel panel;
private JCheckBox enable;
private JComboBox styleComboBox;

public GoogleJavaFormatConfigurable(Project project) {
this.project = project;
Expand Down Expand Up @@ -74,26 +77,38 @@ public JComponent createComponent() {

@Override
public void apply() throws ConfigurationException {
GoogleJavaFormatSettings.getInstance(project).setEnabled(enable.isSelected());
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
settings.setEnabled(enable.isSelected());
settings.setStyle((FormatterStyle) styleComboBox.getSelectedItem());
}

@Override
public void reset() {
enable.setSelected(GoogleJavaFormatSettings.getInstance(project).isEnabled());
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
enable.setSelected(settings.isEnabled());
styleComboBox.setSelectedItem(settings.getStyle());
}

@Override
public boolean isModified() {
return enable.isSelected() != GoogleJavaFormatSettings.getInstance(project).isEnabled();
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
return enable.isSelected() != settings.isEnabled()
|| !styleComboBox.getSelectedItem().equals(settings.getStyle());
}

@Override
public void disposeUIResources() {}

private void createUIComponents() {
styleComboBox = new ComboBox<>(FormatterStyle.values());
}

// IntelliJ's UI designer generated this ugly code and then google-java-format made it even
// uglier. C'est la vie.
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
}

Expand All @@ -104,23 +119,81 @@ public void disposeUIResources() {}
* @noinspection ALL
*/
private void $$$setupUI$$$() {
createUIComponents();
panel = new JPanel();
panel.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
panel.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 0, 0), -1, -1));
enable = new JCheckBox();
enable.setText("Enable google-java-format");
panel.add(enable,
new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
panel.add(
enable,
new GridConstraints(
0,
0,
1,
2,
GridConstraints.ANCHOR_WEST,
GridConstraints.FILL_NONE,
GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
GridConstraints.SIZEPOLICY_FIXED,
null,
null,
null,
0,
false));
final Spacer spacer1 = new Spacer();
panel.add(spacer1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0,
false));
panel.add(
spacer1,
new GridConstraints(
2,
0,
1,
2,
GridConstraints.ANCHOR_CENTER,
GridConstraints.FILL_VERTICAL,
1,
GridConstraints.SIZEPOLICY_WANT_GROW,
null,
null,
null,
0,
false));
final JLabel label1 = new JLabel();
label1.setText("Code style");
panel.add(
label1,
new GridConstraints(
1,
0,
1,
1,
GridConstraints.ANCHOR_WEST,
GridConstraints.FILL_NONE,
GridConstraints.SIZEPOLICY_FIXED,
GridConstraints.SIZEPOLICY_FIXED,
null,
null,
null,
0,
false));
panel.add(
styleComboBox,
new GridConstraints(
1,
1,
1,
1,
GridConstraints.ANCHOR_WEST,
GridConstraints.FILL_HORIZONTAL,
GridConstraints.SIZEPOLICY_CAN_GROW,
GridConstraints.SIZEPOLICY_FIXED,
null,
null,
null,
1,
false));
}

/**
* @noinspection ALL
*/
/** @noinspection ALL */
public JComponent $$$getRootComponent$$$() {
return panel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ class GoogleJavaFormatSettings extends AbstractProjectComponent
implements PersistentStateComponent<GoogleJavaFormatSettings.State> {

private boolean enabled = false;
private FormatterStyle formatterStyle = FormatterStyle.GOOGLE;

protected GoogleJavaFormatSettings(Project project) {
super(project);
}

public static GoogleJavaFormatSettings getInstance(Project project) {
static GoogleJavaFormatSettings getInstance(Project project) {
return PeriodicalTasksCloser.getInstance()
.safeGetComponent(project, GoogleJavaFormatSettings.class);
}
Expand All @@ -47,32 +48,63 @@ public static GoogleJavaFormatSettings getInstance(Project project) {
public State getState() {
State state = new State();
state.setEnabled(enabled);
state.setStyle(formatterStyle);
return state;
}

@Override
public void loadState(State state) {
setEnabled(state.isEnabled());
setStyle(state.getStyle());
}

public boolean isEnabled() {
boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
void setEnabled(boolean enabled) {
this.enabled = enabled;
GoogleJavaFormatInstaller.installFormatter(myProject, enabled);
updateFormatterState();
}

FormatterStyle getStyle() {
return formatterStyle;
}

void setStyle(FormatterStyle formatterStyle) {
// formatterStyle can be null when users upgrade to the first version of the plugin with style
// support (since it was never saved before). If so, keep the default value.
if (formatterStyle == null) {
this.formatterStyle = FormatterStyle.GOOGLE;
} else {
this.formatterStyle = formatterStyle;
}
updateFormatterState();
}

private void updateFormatterState() {
GoogleJavaFormatInstaller.installFormatter(
myProject, enabled, this.formatterStyle.getJavaFormatterOptions());
}

static class State {
private boolean enabled = false;
private FormatterStyle formatterStyle = FormatterStyle.GOOGLE;

public boolean isEnabled() {
boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
void setEnabled(boolean enabled) {
this.enabled = enabled;
}

FormatterStyle getStyle() {
return formatterStyle;
}

void setStyle(FormatterStyle formatterStyle) {
this.formatterStyle = formatterStyle;
}
}
}

0 comments on commit c79f897

Please sign in to comment.