Skip to content

Commit

Permalink
Merge ef2b197 into af79fab
Browse files Browse the repository at this point in the history
  • Loading branch information
SeeSharpSoft committed May 9, 2019
2 parents af79fab + ef2b197 commit 6dcbdd7
Show file tree
Hide file tree
Showing 24 changed files with 348 additions and 78 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
@@ -1,3 +1,9 @@
2.4.0
May 7, 2019

NEW: option to keep/add a linebreak at the end of a file
NEW: improved change detection of table editor to avoid overwriting original text representation without editing any values

2.3.1
Mar 31, 2019

Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -148,6 +148,10 @@ Set whether soft wrapping should be activated for CSV/TSV. It still can be chang

Defines how many lines of text are shown in one editor cell by default. *Auto* does recalculate the height on the fly that can cause some flickering while editing. This setting can be changed in the table editor itself per file.

##### Keep/add linebreak at end of file

If the file ends with a completely empty line (no spaces or tabs either), the table editor will not show this line as empty values but ignore it. When table data is serialized, an empty line is always added (or kept) at the end of the file.

##### Show info panel

Enables/disables the info panel at the bottom of the table editor.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -3,7 +3,7 @@
# https://www.jetbrains.com/intellij-repository/snapshots

name='CSV Plugin'
pluginVersion=2.3.1
pluginVersion=2.4.0
javaVersion=1.8
javaTargetVersion=1.8
downloadIntellijSources=false
Expand Up @@ -25,6 +25,12 @@ public RowInfo getRowInfo(T element) {
return myElementInfos.get(element);
}

public RowInfo getRowInfo(int index) {
return myElementInfos.values().stream()
.filter(rowInfo -> rowInfo.getRowIndex() == index)
.findFirst().orElse(null);
}

public int getColumnIndex() {
return myColumnIndex;
}
Expand Down
Expand Up @@ -2,6 +2,7 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CsvColumnInfoMap<T> {
Expand Down Expand Up @@ -54,4 +55,19 @@ public boolean hasErrors() {
public void setHasErrors(boolean hasErrorsArg) {
hasErrors = hasErrorsArg;
}

public boolean hasEmptyLastLine() {
CsvColumnInfo<T> columnInfo = myInfoColumnMap.get(0);
int size = columnInfo.getSize();
if (!columnInfo.getRowInfo(size - 1).getTextRange().isEmpty()) {
return false;
}
for (int columnIndex = 1; columnIndex < myInfoColumnMap.size(); ++columnIndex) {
if (myInfoColumnMap.get(columnIndex).getSize() == size) {
return false;
}
}
return true;
}

}
Expand Up @@ -43,6 +43,7 @@ public static final class OptionSet {

public boolean SHOW_TABLE_EDITOR_INFO_PANEL;
public boolean QUOTING_ENFORCED;
public boolean FILE_END_LINE_BREAK;

public OptionSet() {
EditorSettingsExternalizable editorSettingsExternalizable = EditorSettingsExternalizable.getInstance();
Expand All @@ -58,6 +59,7 @@ public OptionSet() {
QUOTING_ENFORCED = false;
TABLE_COLUMN_HIGHTLIGHTING = true;
ZERO_BASED_COLUMN_NUMBERING = false;
FILE_END_LINE_BREAK = true;
}
}

Expand Down Expand Up @@ -182,4 +184,11 @@ public boolean isZeroBasedColumnNumbering() {
public void setZeroBasedColumnNumbering(boolean zeroBasedColumnNumbering) {
getState().ZERO_BASED_COLUMN_NUMBERING = zeroBasedColumnNumbering;
}

public boolean isFileEndLineBreak() {
return getState().FILE_END_LINE_BREAK;
}
public void setFileEndLineBreak(boolean fileEndLineBreak) {
getState().FILE_END_LINE_BREAK = fileEndLineBreak;
}
}
Expand Up @@ -91,7 +91,7 @@
</grid>
</children>
</grid>
<grid id="3e325" layout-manager="GridLayoutManager" row-count="6" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="3e325" layout-manager="GridLayoutManager" row-count="7" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand Down Expand Up @@ -134,33 +134,41 @@
</grid>
<component id="97488" class="javax.swing.JCheckBox" binding="cbShowInfoPanel">
<constraints>
<grid row="1" 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="Show info panel"/>
</properties>
</component>
<component id="d6a8e" class="javax.swing.JCheckBox" binding="cbQuotingEnforced">
<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"/>
<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="Enforce value quoting"/>
</properties>
</component>
<component id="f25dc" class="javax.swing.JCheckBox" binding="cbTableColumnHighlighting">
<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="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="Enable column highlighting (Editor &gt; Color Scheme &gt; CSV &gt; Column Hightlighting Colors)"/>
</properties>
</component>
<hspacer id="a028b">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="e470c" class="javax.swing.JCheckBox" binding="cbFileEndLineBreak">
<constraints>
<grid row="1" 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="Keep/add linebreak at file end"/>
</properties>
</component>
</children>
</grid>
<vspacer id="ff292">
Expand Down
Expand Up @@ -26,6 +26,7 @@ public class CsvEditorSettingsProvider implements SearchableConfigurable {
private JCheckBox cbQuotingEnforced;
private JCheckBox cbTableColumnHighlighting;
private JCheckBox cbZeroBasedColumnNumbering;
private JCheckBox cbFileEndLineBreak;

@NotNull
@Override
Expand Down Expand Up @@ -69,7 +70,8 @@ public boolean isModified() {
isModified(cbQuotingEnforced, csvEditorSettingsExternalizable.isQuotingEnforced()) ||
!Objects.equals(cbEditorUsage.getSelectedIndex(), csvEditorSettingsExternalizable.getEditorPrio().ordinal()) ||
isModified(cbTableColumnHighlighting, csvEditorSettingsExternalizable.isTableColumnHighlightingEnabled()) ||
isModified(cbZeroBasedColumnNumbering, csvEditorSettingsExternalizable.isZeroBasedColumnNumbering());
isModified(cbZeroBasedColumnNumbering, csvEditorSettingsExternalizable.isZeroBasedColumnNumbering()) ||
isModified(cbFileEndLineBreak, csvEditorSettingsExternalizable.isFileEndLineBreak());
}

@Override
Expand All @@ -87,6 +89,7 @@ public void reset() {
cbQuotingEnforced.setSelected(csvEditorSettingsExternalizable.isQuotingEnforced());
cbTableColumnHighlighting.setSelected(csvEditorSettingsExternalizable.isTableColumnHighlightingEnabled());
cbZeroBasedColumnNumbering.setSelected(csvEditorSettingsExternalizable.isZeroBasedColumnNumbering());
cbFileEndLineBreak.setSelected(csvEditorSettingsExternalizable.isFileEndLineBreak());
}

@Override
Expand All @@ -104,6 +107,7 @@ public void apply() throws ConfigurationException {
csvEditorSettingsExternalizable.setQuotingEnforced(cbQuotingEnforced.isSelected());
csvEditorSettingsExternalizable.setTableColumnHighlightingEnabled(cbTableColumnHighlighting.isSelected());
csvEditorSettingsExternalizable.setZeroBasedColumnNumbering(cbZeroBasedColumnNumbering.isSelected());
csvEditorSettingsExternalizable.setFileEndLineBreak(cbFileEndLineBreak.isSelected());
}

protected void createUIComponents() {
Expand Down
Expand Up @@ -158,7 +158,8 @@ protected String generateCsv(Object[][] data) {
result.append(this.currentSeparator);
}
}
if (row < data.length - 1) {
if (row < data.length - 1 ||
(CsvEditorSettingsExternalizable.getInstance().isFileEndLineBreak() && getColumnInfoMap().hasEmptyLastLine())) {
result.append("\n");
}
}
Expand Down Expand Up @@ -212,6 +213,7 @@ public boolean isValid() {

@Override
public void selectNotify() {
this.initialState = null;
updateUIComponents();
this.initialState = dataManagement.getCurrentState();
}
Expand Down Expand Up @@ -311,9 +313,10 @@ public Object[][] addRow(int focusedRowIndex, boolean before) {
int index = (before ? (focusedRowIndex == -1 ? 0 : focusedRowIndex) : (focusedRowIndex == -1 ? getRowCount() : focusedRowIndex + 1)) +
(getFileEditorState().getFixedHeaders() ? 1 : 0);
TableDataHandler dataHandler = getDataHandler();
Object[][] currentData = ArrayUtil.insert(dataHandler.getCurrentState(), index, new Object[getColumnCount()]);
updateTableComponentData(dataHandler.addState(currentData));
return currentData;
Object[][] currentData = dataHandler.getCurrentState();
Object[][] newData = ArrayUtil.insert(currentData, Math.min(index, currentData.length), new Object[getColumnCount()]);
updateTableComponentData(dataHandler.addState(newData));
return newData;
}

public Object[][] removeRows(int[] indices) {
Expand Down
Expand Up @@ -24,12 +24,32 @@ public TableDataHandler(CsvTableEditor csvTableEditor, int maxSizeArg) {
}

public boolean equalsCurrentState(@NotNull Object[][] state) {
return Arrays.deepEquals(getCurrentState(), state);
return Arrays.deepEquals(getCurrentState(), sanitizeState(state));
}

public Object[][] addState(Object[][] state) {
if (equalsCurrentState(state)) {
return state;
public Object[][] sanitizeState(@NotNull Object[][] state) {
Object[][] newState = null;
if (state.length == 0 || state[0].length == 0) {
newState = new Object[1][1];
} else {
newState = CsvHelper.deepCopy(state);
}

for (int i = 0; i < newState.length; ++i) {
for (int j = 0; j < newState[i].length; ++j) {
if (newState[i][j] == null) {
newState[i][j] = "";
}
}
}

return newState;
}

public Object[][] addState(@NotNull Object[][] state) {
Object[][] newState = sanitizeState(state);
if (equalsCurrentState(newState)) {
return newState;
}

while (states.size() - 1 > currentStateIndex) {
Expand All @@ -39,11 +59,11 @@ public Object[][] addState(Object[][] state) {
states.remove(0);
--currentStateIndex;
}
states.add(state);
states.add(newState);
++currentStateIndex;
fireStateUpdated();

return state;
return newState;
}

public Object[][] getCurrentState() {
Expand Down
Expand Up @@ -82,11 +82,11 @@ public void actionPerformed(ActionEvent e) {
}

private void selectCell(JTable table, int row, int column) {
if (row == -1 || column == -1) {
return;
}
int actualRow = Math.min(row, table.getRowCount() - 1);
int actualColumn = Math.min(column, table.getColumnCount() - 1);
if (actualRow < 0 || actualColumn < 0) {
return;
}
table.setRowSelectionInterval(actualRow, actualRow);
table.setColumnSelectionInterval(actualColumn, actualColumn);
}
Expand Down
Expand Up @@ -349,11 +349,15 @@ protected void updateUIComponents() {
DefaultTableModel tableModel = new DefaultTableModel(0, 0);
if (!columnInfoMap.hasErrors()) {
int startRow = getFileEditorState().getFixedHeaders() ? 1 : 0;
for (int i = 0; i < columnInfoMap.getColumnInfos().size(); ++i) {
CsvColumnInfo<PsiElement> columnInfo = columnInfoMap.getColumnInfo(i);
for (int columnIndex = 0; columnIndex < columnInfoMap.getColumnInfos().size(); ++columnIndex) {
CsvColumnInfo<PsiElement> columnInfo = columnInfoMap.getColumnInfo(columnIndex);
List<PsiElement> elements = columnInfo.getElements();
if (columnIndex == 0 && CsvEditorSettingsExternalizable.getInstance().isFileEndLineBreak()
&& lastColumnInfoMap.hasEmptyLastLine()) {
elements.remove(elements.size() - 1);
}

tableModel.addColumn(String.format("Column %s (%s entries)", i + 1, elements.size()),
tableModel.addColumn(String.format("Column %s (%s entries)", columnIndex + 1, elements.size()),
elements.stream()
.skip(startRow)
.map(psiElement -> psiElement == null ? "" : CsvHelper.unquoteCsvValue(psiElement.getText()))
Expand Down Expand Up @@ -492,7 +496,7 @@ public int getPreferredRowHeight() {
private void setFontSize(int size) {
Font font = getTable().getFont();
if (font.getSize() != size) {
Font newFont = font.deriveFont((float)size);
Font newFont = font.deriveFont((float) size);
getTable().setFont(newFont);
}
}
Expand Down
Expand Up @@ -63,7 +63,7 @@ private TableRowUtilities() {
}

private static JTable createRowHeadersTable(final JTable userTable, int startingNumber) {
final JTable rowHeadersTable = new JBTable(new RowHeadersTableModel(userTable.getModel().getRowCount(), startingNumber));
final JBTable rowHeadersTable = new JBTable(new RowHeadersTableModel(userTable.getModel().getRowCount(), startingNumber));

// this is where you set the width of the row headers
rowHeadersTable.createDefaultColumnsFromModel();
Expand All @@ -79,6 +79,7 @@ private static JTable createRowHeadersTable(final JTable userTable, int starting
rowHeadersTable.setFocusable(true);
rowHeadersTable.setDragEnabled(true);
rowHeadersTable.setSelectionMode(userTable.getSelectionModel().getSelectionMode());
rowHeadersTable.getEmptyText().setText("");

return rowHeadersTable;
}
Expand Down Expand Up @@ -186,7 +187,7 @@ private static final class RowHeadersTableModel extends AbstractTableModel {
this.startNumber = startingNumber;
int j = 0;
for (int i = startingNumber; i < maxNumber + startNumber; i++) {
numbersList.add(new Integer(j + startNumber));
numbersList.add(j + startNumber);
j++;
}
}
Expand Down Expand Up @@ -225,7 +226,7 @@ public Object getValueAt(int rowIndex, int columnIndex) {

public void addNumber() {
if (numbersList.isEmpty()) {
numbersList.add(0, new Integer(startNumber));
numbersList.add(0, startNumber);
} else {
Integer maxNum = numbersList.get(numbersList.size() - 1);
numbersList.add(numbersList.size(), maxNum.intValue() + 1);
Expand Down

0 comments on commit 6dcbdd7

Please sign in to comment.