1616
1717package com .google .googlejavaformat .intellij ;
1818
19+ import com .google .googlejavaformat .Doc ;
1920import com .google .googlejavaformat .intellij .GoogleJavaFormatSettings .EnabledState ;
2021import com .intellij .openapi .options .BaseConfigurable ;
2122import com .intellij .openapi .options .ConfigurationException ;
2223import com .intellij .openapi .options .SearchableConfigurable ;
2324import com .intellij .openapi .project .Project ;
2425import com .intellij .openapi .ui .ComboBox ;
26+ import com .intellij .ui .SimpleListCellRenderer ;
27+ import com .intellij .ui .components .JBTextField ;
2528import com .intellij .uiDesigner .core .GridConstraints ;
2629import com .intellij .uiDesigner .core .GridLayoutManager ;
2730import com .intellij .uiDesigner .core .Spacer ;
@@ -41,6 +44,7 @@ class GoogleJavaFormatConfigurable extends BaseConfigurable implements Searchabl
4144 private JPanel panel ;
4245 private JCheckBox enable ;
4346 private JComboBox styleComboBox ;
47+ private JBTextField maxLineLengthField ;
4448
4549 public GoogleJavaFormatConfigurable (Project project ) {
4650 this .project = project ;
@@ -81,6 +85,17 @@ public void apply() throws ConfigurationException {
8185 GoogleJavaFormatSettings settings = GoogleJavaFormatSettings .getInstance (project );
8286 settings .setEnabled (enable .isSelected () ? EnabledState .ENABLED : getDisabledState ());
8387 settings .setStyle (((UiFormatterStyle ) styleComboBox .getSelectedItem ()).convert ());
88+ int maxLineLength ;
89+ try {
90+ maxLineLength = Integer .parseInt (maxLineLengthField .getText ().trim ());
91+ } catch (NumberFormatException e ) {
92+ throw new ConfigurationException ("Max line length must be a valid integer" );
93+ }
94+ if (maxLineLength <= 0 || maxLineLength >= Doc .MAX_LINE_WIDTH ) {
95+ throw new ConfigurationException (
96+ String .format ("Max line length must be between 1 and %d" , Doc .MAX_LINE_WIDTH - 1 ));
97+ }
98+ settings .setMaxLineLength (maxLineLength );
8499 }
85100
86101 private EnabledState getDisabledState () {
@@ -95,20 +110,25 @@ public void reset() {
95110 GoogleJavaFormatSettings settings = GoogleJavaFormatSettings .getInstance (project );
96111 enable .setSelected (settings .isEnabled ());
97112 styleComboBox .setSelectedItem (UiFormatterStyle .convert (settings .getStyle ()));
113+ maxLineLengthField .setText (String .valueOf (settings .getMaxLineLength ()));
98114 }
99115
100116 @ Override
101117 public boolean isModified () {
102118 GoogleJavaFormatSettings settings = GoogleJavaFormatSettings .getInstance (project );
103119 return enable .isSelected () != settings .isEnabled ()
104- || !styleComboBox .getSelectedItem ().equals (UiFormatterStyle .convert (settings .getStyle ()));
120+ || !styleComboBox .getSelectedItem ().equals (UiFormatterStyle .convert (settings .getStyle ()))
121+ || !maxLineLengthField .getText ().trim ().equals (String .valueOf (settings .getMaxLineLength ()));
105122 }
106123
107124 @ Override
108125 public void disposeUIResources () {}
109126
110127 private void createUIComponents () {
111128 styleComboBox = new ComboBox <>(UiFormatterStyle .values ());
129+ styleComboBox .setRenderer (SimpleListCellRenderer .create ("" , UiFormatterStyle ::getDescription ));
130+ maxLineLengthField = new JBTextField ();
131+ maxLineLengthField .setColumns (4 );
112132 }
113133
114134 {
@@ -127,7 +147,7 @@ private void createUIComponents() {
127147 private void $$$setupUI$$$ () {
128148 createUIComponents ();
129149 panel = new JPanel ();
130- panel .setLayout (new GridLayoutManager (3 , 2 , new Insets (0 , 0 , 0 , 0 ), -1 , -1 ));
150+ panel .setLayout (new GridLayoutManager (4 , 2 , new Insets (0 , 0 , 0 , 0 ), -1 , -1 ));
131151 enable = new JCheckBox ();
132152 enable .setText ("Enable google-java-format" );
133153 panel .add (
@@ -150,7 +170,7 @@ private void createUIComponents() {
150170 panel .add (
151171 spacer1 ,
152172 new GridConstraints (
153- 2 ,
173+ 3 ,
154174 0 ,
155175 1 ,
156176 2 ,
@@ -197,6 +217,40 @@ private void createUIComponents() {
197217 null ,
198218 1 ,
199219 false ));
220+ final JLabel label2 = new JLabel ();
221+ label2 .setText ("Max line length" );
222+ panel .add (
223+ label2 ,
224+ new GridConstraints (
225+ 2 ,
226+ 0 ,
227+ 1 ,
228+ 1 ,
229+ GridConstraints .ANCHOR_WEST ,
230+ GridConstraints .FILL_NONE ,
231+ GridConstraints .SIZEPOLICY_FIXED ,
232+ GridConstraints .SIZEPOLICY_FIXED ,
233+ null ,
234+ null ,
235+ null ,
236+ 0 ,
237+ false ));
238+ panel .add (
239+ maxLineLengthField ,
240+ new GridConstraints (
241+ 2 ,
242+ 1 ,
243+ 1 ,
244+ 1 ,
245+ GridConstraints .ANCHOR_WEST ,
246+ GridConstraints .FILL_NONE ,
247+ GridConstraints .SIZEPOLICY_FIXED ,
248+ GridConstraints .SIZEPOLICY_FIXED ,
249+ null ,
250+ null ,
251+ null ,
252+ 1 ,
253+ false ));
200254 }
201255
202256 /**
0 commit comments