diff --git a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtConfigurable.form b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtConfigurable.form index e515398f..64985e6c 100644 --- a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtConfigurable.form +++ b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtConfigurable.form @@ -1,6 +1,6 @@
diff --git a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtConfigurable.java b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtConfigurable.java index 9fd0e9ae..0cfec65c 100644 --- a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtConfigurable.java +++ b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtConfigurable.java @@ -26,11 +26,14 @@ import com.intellij.uiDesigner.core.GridLayoutManager; import com.intellij.uiDesigner.core.Spacer; import java.awt.Insets; +import java.text.NumberFormat; +import javax.swing.JFormattedTextField; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.text.NumberFormatter; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -41,6 +44,7 @@ public class KtfmtConfigurable extends BaseConfigurable implements SearchableCon private JPanel panel; private JCheckBox enable; private JComboBox styleComboBox; + private JFormattedTextField maxWidthField; public KtfmtConfigurable(Project project) { this.project = project; @@ -80,7 +84,9 @@ public JComponent createComponent() { public void apply() throws ConfigurationException { KtfmtSettings settings = KtfmtSettings.getInstance(project); settings.setEnabled(enable.isSelected() ? EnabledState.ENABLED : getDisabledState()); - settings.setUiFormatterStyle(((UiFormatterStyle) styleComboBox.getSelectedItem())); + UiFormatterStyle selectedStyle = (UiFormatterStyle) styleComboBox.getSelectedItem(); + settings.setMaxWidth((Integer) maxWidthField.getValue()); + settings.setUiFormatterStyle(selectedStyle); } private EnabledState getDisabledState() { @@ -96,13 +102,15 @@ public void reset() { KtfmtSettings settings = KtfmtSettings.getInstance(project); enable.setSelected(settings.isEnabled()); styleComboBox.setSelectedItem(settings.getUiFormatterStyle()); + maxWidthField.setValue(settings.getMaxWidth()); } @Override public boolean isModified() { KtfmtSettings settings = KtfmtSettings.getInstance(project); return enable.isSelected() != settings.isEnabled() - || !styleComboBox.getSelectedItem().equals(settings.getUiFormatterStyle()); + || !styleComboBox.getSelectedItem().equals(settings.getUiFormatterStyle()) + || !maxWidthField.getValue().equals(settings.getMaxWidth()); } @Override @@ -110,6 +118,15 @@ public void disposeUIResources() {} private void createUIComponents() { styleComboBox = new ComboBox<>(UiFormatterStyle.values()); + + NumberFormat format = NumberFormat.getInstance(); + NumberFormatter formatter = new NumberFormatter(format); + formatter.setValueClass(Integer.class); + formatter.setMinimum(0); + formatter.setMaximum(500); + formatter.setAllowsInvalid(false); + + maxWidthField = new JFormattedTextField(formatter); } { diff --git a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtFormattingService.java b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtFormattingService.java index 58aaac7d..b3ba7fb3 100644 --- a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtFormattingService.java +++ b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtFormattingService.java @@ -18,6 +18,7 @@ import static com.facebook.ktfmt.format.Formatter.format; +import com.facebook.ktfmt.format.FormattingOptions; import com.google.googlejavaformat.java.FormatterException; import com.intellij.formatting.service.AsyncDocumentFormattingService; import com.intellij.formatting.service.AsyncFormattingRequest; @@ -34,9 +35,10 @@ public class KtfmtFormattingService extends AsyncDocumentFormattingService { @Override protected FormattingTask createFormattingTask(AsyncFormattingRequest request) { Project project = request.getContext().getProject(); - - UiFormatterStyle style = KtfmtSettings.getInstance(project).getUiFormatterStyle(); - return new KtfmtFormattingTask(request, style); + KtfmtSettings settings = KtfmtSettings.getInstance(project); + UiFormatterStyle style = settings.getUiFormatterStyle(); + int maxWidth = settings.getMaxWidth(); + return new KtfmtFormattingTask(request, style, maxWidth); } @Override @@ -63,16 +65,28 @@ public boolean canFormat(@NotNull PsiFile file) { private static final class KtfmtFormattingTask implements FormattingTask { private final AsyncFormattingRequest request; private final UiFormatterStyle style; + private final int maxWidth; - private KtfmtFormattingTask(AsyncFormattingRequest request, UiFormatterStyle style) { + private KtfmtFormattingTask(AsyncFormattingRequest request, UiFormatterStyle style, int maxWidth) { this.request = request; this.style = style; + this.maxWidth = maxWidth; } @Override public void run() { try { - String formattedText = format(style.getFormattingOptions(), request.getDocumentText()); + FormattingOptions formattingOptions = style.getFormattingOptions(); + // This works as long as the maxWidth is declared as the first in the data class + FormattingOptions overridenOptions = new FormattingOptions( + maxWidth, + formattingOptions.getBlockIndent(), + formattingOptions.getContinuationIndent(), + formattingOptions.getManageTrailingCommas(), + formattingOptions.getRemoveUnusedImports(), + formattingOptions.getDebuggingPrintOpsAfterFormatting() + ); + String formattedText = format(overridenOptions, request.getDocumentText()); request.onTextReady(formattedText); } catch (FormatterException e) { request.onError( diff --git a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtSettings.java b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtSettings.java index 90909e61..30de1f05 100644 --- a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtSettings.java +++ b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/KtfmtSettings.java @@ -69,6 +69,14 @@ void setUiFormatterStyle(UiFormatterStyle uiFormatterStyle) { state.uiFormatterStyle = uiFormatterStyle; } + int getMaxWidth() { + return state.maxWidth; + } + + void setMaxWidth(int maxWidth) { + state.maxWidth = maxWidth; + } + enum EnabledState { UNKNOWN, ENABLED, @@ -79,6 +87,7 @@ static class State { private EnabledState enabled = EnabledState.UNKNOWN; public UiFormatterStyle uiFormatterStyle = UiFormatterStyle.META; + public int maxWidth = 100; // enabled used to be a boolean so we use bean property methods for backwards // compatibility