Skip to content

Commit

Permalink
Fixes #47 align by column history
Browse files Browse the repository at this point in the history
  • Loading branch information
krasa committed Aug 20, 2018
1 parent 404dd53 commit 60f96c8
Show file tree
Hide file tree
Showing 11 changed files with 425 additions and 197 deletions.
2 changes: 1 addition & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@
<h4>Version 5.10<h4>
<ul>
<li>Added: Repeat last action</li>
<li>Option to align into columns but make the separator stick to the left</li>
<li>Align by column: added history, option to align by separators or values</li>
<li>Fixed: Incrementing/decrementing long literals removes the 'L' after the number</li>
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,70 @@
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.intellij.util.xmlb.annotations.Transient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import osmedile.intellij.stringmanip.align.ColumnAlignerModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@State(name = "StringManipulationState", storages = {@Storage(id = "StringManipulationState", file = "$APP_CONFIG$/stringManipulation.xml")})
public class PluginPersistentStateComponent implements PersistentStateComponent<PluginPersistentStateComponent> {

private ColumnAlignerModel columnAlignerModel = new ColumnAlignerModel();
public static final int LIMIT = 20;
private List<ColumnAlignerModel> history = new ArrayList<ColumnAlignerModel>();

public List<ColumnAlignerModel> getHistory() {
return new ArrayList<ColumnAlignerModel>(history);
}

public void setHistory(List<ColumnAlignerModel> history) {
this.history = history;
}

@NotNull
@Transient
public ColumnAlignerModel getLastModel() {
if (history.size() > 0) {
return history.get(history.size() - 1);
}
return new ColumnAlignerModel();
}

@Transient
public void addToHistory(ColumnAlignerModel columnAlignerModel) {
List<ColumnAlignerModel> newList = new ArrayList<ColumnAlignerModel>(history.size() + 1);

int startIndex = history.size() >= LIMIT ? 1 : 0;
for (int i = startIndex; i < history.size(); i++) {
ColumnAlignerModel alignerModel = history.get(i);
if (!alignerModel.equals(columnAlignerModel)) {
newList.add(alignerModel);
}
}

public ColumnAlignerModel getColumnAlignerModel() {
return columnAlignerModel;
}
columnAlignerModel.setAdded(new Date());
newList.add(columnAlignerModel);

public void setColumnAlignerModel(ColumnAlignerModel columnAlignerModel) {
this.columnAlignerModel = columnAlignerModel;
}
history = newList;
}

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

@Nullable
@Override
public PluginPersistentStateComponent getState() {
return this;
}
@Nullable
@Override
public PluginPersistentStateComponent getState() {
return this;
}

@Override
public void loadState(PluginPersistentStateComponent o) {
XmlSerializerUtil.copyBean(o, this);
@Override
public void loadState(PluginPersistentStateComponent o) {
XmlSerializerUtil.copyBean(o, this);

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected AlignToColumnsAction(boolean setupHandler) {
@Override
protected Pair<Boolean, ColumnAlignerModel> beforeWriteAction(Editor editor, DataContext dataContext) {
PluginPersistentStateComponent stateComponent = PluginPersistentStateComponent.getInstance();
final TextAlignmentForm textAlignmentForm = new TextAlignmentForm(stateComponent.getColumnAlignerModel());
final TextAlignmentForm textAlignmentForm = new TextAlignmentForm(stateComponent.getLastModel());
DialogWrapper dialogWrapper = new DialogWrapper(editor.getProject()) {
{
init();
Expand Down Expand Up @@ -66,7 +66,7 @@ protected void doOKAction() {
}

ColumnAlignerModel model = textAlignmentForm.getModel();
stateComponent.setColumnAlignerModel(textAlignmentForm.getModel());
stateComponent.addToHistory(textAlignmentForm.getModel());
return continueExecution(model);
}

Expand Down
4 changes: 2 additions & 2 deletions src/osmedile/intellij/stringmanip/align/ColumnAligner.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public List<String> process(List<ColumnAlignerLine> lines) {
}


if (model.getAlignSeparator() == ColumnAlignerModel.Align.RIGHT) {
if (model.getAlignBy() == ColumnAlignerModel.Align.SEPARATORS) {
int maxLength = getMaxLength(lines);
for (ColumnAlignerLine line : lines) {
line.appendSpace(maxLength);
Expand All @@ -99,7 +99,7 @@ public List<String> process(List<ColumnAlignerLine> lines) {
line.appendSpaceAfterSeparator();
}

if (model.getAlignSeparator() == ColumnAlignerModel.Align.LEFT) {
if (model.getAlignBy() == ColumnAlignerModel.Align.VALUES) {
int maxLength = getMaxLength(lines);
for (ColumnAlignerLine line : lines) {
line.appendSpace(maxLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public ColumnAlignerLine(ColumnAlignerModel model, String separator, String text
}
hasSeparatorBeforeFirstToken = split.length > 0 && split[0].length() == 0;

this.appendSpaceBeforeSeparator = model.isSpaceBefore();
this.appendSpaceAfterSeparator = model.isSpaceAfter();
this.appendSpaceBeforeSeparator = model.isSpaceBeforeSeparator();
this.appendSpaceAfterSeparator = model.isSpaceAfterSeparator();
this.trimValues = model.isTrimValues();
this.trimLines = model.isTrimLines();
}
Expand Down
88 changes: 72 additions & 16 deletions src/osmedile/intellij/stringmanip/align/ColumnAlignerModel.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package osmedile.intellij.stringmanip.align;

import osmedile.intellij.stringmanip.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class ColumnAlignerModel {
private Date added;
private List<String> separators = new ArrayList<String>();
private boolean spaceBefore = true;
private boolean spaceAfter = true;
private boolean spaceBeforeSeparator = true;
private boolean spaceAfterSeparator = true;
private boolean trimValues = true;
private boolean trimLines = false;
private Align alignSeparator = Align.RIGHT;
private Align alignBy = Align.SEPARATORS;

public ColumnAlignerModel() {
}
Expand All @@ -27,20 +32,20 @@ public void setSeparators(List<String> separators) {
this.separators = separators;
}

public boolean isSpaceBefore() {
return spaceBefore;
public boolean isSpaceBeforeSeparator() {
return spaceBeforeSeparator;
}

public void setSpaceBefore(final boolean spaceBefore) {
this.spaceBefore = spaceBefore;
public void setSpaceBeforeSeparator(final boolean spaceBeforeSeparator) {
this.spaceBeforeSeparator = spaceBeforeSeparator;
}

public boolean isSpaceAfter() {
return spaceAfter;
public boolean isSpaceAfterSeparator() {
return spaceAfterSeparator;
}

public void setSpaceAfter(final boolean spaceAfter) {
this.spaceAfter = spaceAfter;
public void setSpaceAfterSeparator(final boolean spaceAfterSeparator) {
this.spaceAfterSeparator = spaceAfterSeparator;
}

public boolean isTrimValues() {
Expand All @@ -59,15 +64,66 @@ public void setTrimLines(final boolean trimLines) {
this.trimLines = trimLines;
}

public Align getAlignSeparator() {
return alignSeparator;
public Align getAlignBy() {
return alignBy;
}

public void setAlignBy(Align alignBy) {
this.alignBy = alignBy;
}

public void setAlignSeparator(Align alignSeparator) {
this.alignSeparator = alignSeparator;
public Date getAdded() {
return added;
}

public void setAdded(Date added) {
this.added = added;
}

enum Align {
LEFT, RIGHT
VALUES, SEPARATORS
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ColumnAlignerModel that = (ColumnAlignerModel) o;

if (spaceBeforeSeparator != that.spaceBeforeSeparator) return false;
if (spaceAfterSeparator != that.spaceAfterSeparator) return false;
if (trimValues != that.trimValues) return false;
if (trimLines != that.trimLines) return false;
if (separators != null ? !separators.equals(that.separators) : that.separators != null) return false;
return alignBy == that.alignBy;
}

@Override
public int hashCode() {
int result = separators != null ? separators.hashCode() : 0;
result = 31 * result + (spaceBeforeSeparator ? 1 : 0);
result = 31 * result + (spaceAfterSeparator ? 1 : 0);
result = 31 * result + (trimValues ? 1 : 0);
result = 31 * result + (trimLines ? 1 : 0);
result = 31 * result + (alignBy != null ? alignBy.hashCode() : 0);
return result;
}

@Override
public String toString() {
String format = "";
if (added != null) {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(added);
}


StringBuilder s = new StringBuilder();
for (String separator : separators) {
if (StringUtils.isNotEmpty(separator)) {
s.append(separator).append(" ");
}
}
return format + " - " + s.toString();
}
}
40 changes: 24 additions & 16 deletions src/osmedile/intellij/stringmanip/align/TextAlignmentForm.form
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<children>
<component id="77bfc" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" 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="Separators:"/>
Expand All @@ -24,41 +24,33 @@
<border type="none"/>
<children/>
</grid>
<component id="7e093" class="javax.swing.JButton" binding="resetButton" default-binding="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Reset "/>
</properties>
</component>
<component id="239c8" class="javax.swing.JCheckBox" binding="addSpaceBeforeSeparatorCheckBox" default-binding="true">
<constraints>
<grid row="3" 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="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>
<text value="Add space before separator"/>
</properties>
</component>
<component id="dcd21" class="javax.swing.JCheckBox" binding="addSpaceAfterSeparatorCheckBox">
<constraints>
<grid row="4" 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="3" 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>
<text value="Add space after separator"/>
</properties>
</component>
<component id="c32eb" class="javax.swing.JCheckBox" binding="trimValues">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Trim values"/>
</properties>
</component>
<component id="2be38" class="javax.swing.JCheckBox" binding="trimLines">
<constraints>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Trim lines"/>
Expand All @@ -69,15 +61,31 @@
<grid row="5" 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>
<text value="Align seperator to the left"/>
<text value="Align only values"/>
</properties>
</component>
<component id="786c6" class="javax.swing.JButton" binding="historyButton" default-binding="true">
<constraints>
<grid row="0" column="1" 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="History"/>
</properties>
</component>
<component id="7e093" class="javax.swing.JButton" binding="resetButton" default-binding="true">
<constraints>
<grid row="6" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Reset "/>
</properties>
</component>
<component id="a9cb7" class="javax.swing.JRadioButton" binding="alignSeparatorRight">
<constraints>
<grid row="6" 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="4" 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>
<text value="Align seperator to the right"/>
<text value="Align seperators"/>
</properties>
</component>
</children>
Expand Down
Loading

0 comments on commit 60f96c8

Please sign in to comment.