Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add maxWidth option to IDEA plugin, fixes #351 #492

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.facebook.ktfmt.intellij.KtfmtConfigurable">
<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">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="4" 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 @@ -18,7 +18,7 @@
</component>
<vspacer id="19e83">
<constraints>
<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"/>
<grid row="3" 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">
Expand All @@ -35,6 +35,20 @@
</constraints>
<properties/>
</component>
<component id="fcf22" class="javax.swing.JLabel">
<constraints>
<grid row="2" 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="Max width"/>
</properties>
</component>
<component id="bc1eb" class="javax.swing.JFormattedTextField" binding="maxWidthField">
<constraints>
<grid row="2" 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 @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -96,20 +102,31 @@ 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
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's bold! Not big deal, but how about making this something like 10 or at the very least 1?

formatter.setMaximum(500);
formatter.setAllowsInvalid(false);

maxWidthField = new JFormattedTextField(formatter);
}

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
Loading