Skip to content

Add option to remove new line code #1

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Binary file modified SQLFormatter.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
<depends>com.intellij.modules.lang</depends>

<extensions defaultExtensionNs="com.intellij">
<applicationService serviceInterface="SQLFormatterConfig" serviceImplementation="SQLFormatterConfig"/>
<toolWindow id="SQL Formatter" anchor="right" factoryClass="SQLFormatterToolWindow" />
</extensions>

<actions>
<!-- Add your actions here -->
</actions>

</idea-plugin>
</idea-plugin>
39 changes: 39 additions & 0 deletions src/SQLFormatterConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;

@State(
name = "SQLFormatterConfig",
storages = {
@Storage("intellij-plugin-SQLFormatter.xml")
}
)

public class SQLFormatterConfig implements PersistentStateComponent<SQLFormatterConfig> {
private boolean removeNewLineCode = false;

@Override
public SQLFormatterConfig getState() {
return this;
}

@Override
public void loadState(@NotNull SQLFormatterConfig config) {
XmlSerializerUtil.copyBean(config, this);
}

public static SQLFormatterConfig getInstance() {
return ServiceManager.getService(SQLFormatterConfig.class);
}

public boolean isRemoveNewLineCode() {
return removeNewLineCode;
}

public void setRemoveNewLineCode(boolean removeNewLineCode) {
this.removeNewLineCode = removeNewLineCode;
}
}
15 changes: 12 additions & 3 deletions src/SQLFormatterToolWindow.form
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="SQLFormatterToolWindow">
<grid id="27dc6" binding="toolWindowContent" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="toolWindowContent" layout-manager="GridLayoutManager" row-count="4" column-count="1" 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,7 +10,7 @@
<children>
<component id="e0562" class="javax.swing.JButton" binding="buttonFormat">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Format"/>
Expand Down Expand Up @@ -38,6 +38,15 @@
<text value=""/>
</properties>
</component>
<component id="f9e7e" class="javax.swing.JCheckBox" binding="removeNewLineCode">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="false"/>
<text value="Remove new line code"/>
</properties>
</component>
</children>
</grid>
</form>
</form>
27 changes: 24 additions & 3 deletions src/SQLFormatterToolWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,26 @@ public class SQLFormatterToolWindow implements ToolWindowFactory {
private JButton buttonFormat;
private JTextArea textArea;
private JLabel labelMessage;
private JCheckBox removeNewLineCode;

private Formatter sqlFormatter = new BasicFormatterImpl();

public SQLFormatterToolWindow() {
buttonFormat.addActionListener(e -> {
labelMessage.setText("");
String text = textArea.getText();
boolean shouldRemoveNewLineCode = removeNewLineCode.isSelected();
if (shouldRemoveNewLineCode) {
text = text.replace("\r", "").replace("\n", "");
}
String formattedText = sqlFormatter.format(text);
textArea.setText(formattedText);

SQLFormatterConfig currentConfig = SQLFormatterConfig.getInstance();
if (currentConfig != null) {
currentConfig.setRemoveNewLineCode(shouldRemoveNewLineCode);
SQLFormatterConfig.getInstance().loadState(currentConfig);
}
});
}

Expand All @@ -35,6 +46,11 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
Content content = contentFactory.createContent(toolWindowContent, "", false);
toolWindow.getContentManager().addContent(content);

SQLFormatterConfig config = SQLFormatterConfig.getInstance();
if (config.isRemoveNewLineCode()) {
removeNewLineCode.setSelected(true);
}
}

{
Expand All @@ -53,10 +69,10 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
*/
private void $$$setupUI$$$() {
toolWindowContent = new JPanel();
toolWindowContent.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1));
toolWindowContent.setLayout(new GridLayoutManager(4, 1, new Insets(0, 0, 0, 0), -1, -1));
buttonFormat = new JButton();
buttonFormat.setText("Format");
toolWindowContent.add(buttonFormat, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
toolWindowContent.add(buttonFormat, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final JScrollPane scrollPane1 = new JScrollPane();
toolWindowContent.add(scrollPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
textArea = new JTextArea();
Expand All @@ -65,6 +81,10 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
labelMessage.setForeground(new Color(-2215827));
labelMessage.setText("");
toolWindowContent.add(labelMessage, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
removeNewLineCode = new JCheckBox();
removeNewLineCode.setSelected(false);
removeNewLineCode.setText("Remove new line code");
toolWindowContent.add(removeNewLineCode, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
}

/**
Expand All @@ -73,4 +93,5 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
public JComponent $$$getRootComponent$$$() {
return toolWindowContent;
}
}

}