Skip to content

Commit c0d0b74

Browse files
authored
Merge pull request #142 from SeeSharpSoft/master
Release 2.6.0
2 parents b617b22 + eb9ac98 commit c0d0b74

23 files changed

+446
-81
lines changed

CHANGELOG

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2.6.0
2+
Jul 18, 2019
3+
4+
NEW: Table column width calculation and adjustment based on content
5+
16
2.5.1
27
Jun 25, 2019
38

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ Set whether soft wrapping should be activated for CSV/TSV. It still can be chang
148148

149149
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.
150150

151+
##### Default column width
152+
153+
The default and initial width of a single table column in _px_.
154+
155+
##### Maximum column width
156+
157+
The maximum width of a single table column in _px_, which is used when adjusting the column widths automatically. 0 deactivates the limitation. This value has no effect when sizing the columns manually in the table editor.
158+
159+
##### Adjust column width on open (default)
160+
161+
If selected, the table column widths are adjusted based on the column contents automatically when the table editor is opened. This setting can be changed in the table editor itself per file.
162+
151163
##### Keep/ignore linebreak at end of file
152164

153165
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 existing empty line is kept at the end of the file.
@@ -293,6 +305,14 @@ The action to switch the value separator used for CSV syntax validation of a spe
293305
This action defines how the parser/validator/highlighter/etc. behaves. It does intentionally not change the file content.
294306
To be more precise: It **does not replace** previous separator characters by new ones or adjust the escaped texts.
295307

308+
#### Adjust column widths (table editor only)
309+
310+
Calculates and sets the maximum width for all table columns based on their content. The maximum table column width can be changed via [Editor Settings](#maximum-column-width).
311+
312+
#### Reset column widths to default (table editor only)
313+
314+
Set the width of all table columns back to default. The default table column width can be changed via [Editor Settings](#default-column-width)).
315+
296316
### Inspections
297317

298318
- _File > Settings > Editor > Inspections > CSV_

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# https://www.jetbrains.com/intellij-repository/snapshots
44

55
name='CSV Plugin'
6-
pluginVersion=2.5.1
6+
pluginVersion=2.6.0
77
javaVersion=1.8
88
javaTargetVersion=1.8
99
downloadIntellijSources=false

src/main/java/net/seesharpsoft/intellij/plugins/csv/CsvColumnInfo.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ public class CsvColumnInfo<T> {
99

1010
private int myColumnIndex;
1111
private int myMaxLength;
12+
private int myMaxLengthRowIndex;
1213
private Map<T, RowInfo> myElementInfos;
1314
private T myHeaderElement;
1415
private int mySize;
1516

16-
public CsvColumnInfo(int columnIndex, int maxLength) {
17+
public CsvColumnInfo(int columnIndex, int maxLength, int maxLengthRowIndex) {
1718
this.myColumnIndex = columnIndex;
18-
this.myMaxLength = maxLength;
19+
setMaxLength(maxLength, maxLengthRowIndex);
1920
this.myElementInfos = new HashMap<>();
2021
this.myHeaderElement = null;
2122
this.mySize = 0;
@@ -39,8 +40,13 @@ public int getMaxLength() {
3940
return myMaxLength;
4041
}
4142

42-
public void setMaxLength(int maxLength) {
43+
public void setMaxLength(int maxLength, int maxLengthRowIndex) {
4344
this.myMaxLength = maxLength;
45+
this.myMaxLengthRowIndex = maxLengthRowIndex;
46+
}
47+
48+
public int getMaxLengthRowIndex() {
49+
return this.myMaxLengthRowIndex;
4450
}
4551

4652
public int getSize() {

src/main/java/net/seesharpsoft/intellij/plugins/csv/CsvHelper.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import net.seesharpsoft.intellij.plugins.csv.psi.CsvFile;
2222
import net.seesharpsoft.intellij.plugins.csv.psi.CsvRecord;
2323
import net.seesharpsoft.intellij.plugins.csv.psi.CsvTypes;
24+
import org.jetbrains.annotations.NotNull;
2425

2526
import java.util.HashMap;
2627
import java.util.Map;
28+
import java.util.function.Function;
2729

2830
public final class CsvHelper {
2931

@@ -154,11 +156,11 @@ public static CsvColumnInfoMap<PsiElement> createColumnInfoMap(CsvFile csvFile)
154156
for (CsvRecord record : records) {
155157
int column = 0;
156158
for (CsvField field : record.getFieldList()) {
157-
Integer length = field.getTextLength();
159+
Integer length = CsvHelper.getMaxTextLineLength(unquoteCsvValue(field.getText()));
158160
if (!columnInfoMap.containsKey(column)) {
159-
columnInfoMap.put(column, new CsvColumnInfo(column, length));
161+
columnInfoMap.put(column, new CsvColumnInfo(column, length, row));
160162
} else if (columnInfoMap.get(column).getMaxLength() < length) {
161-
columnInfoMap.get(column).setMaxLength(length);
163+
columnInfoMap.get(column).setMaxLength(length, row);
162164
}
163165
columnInfoMap.get(column).addElement(field, row, getFieldStartOffset(field), getFieldEndOffset(field));
164166
++column;
@@ -199,6 +201,24 @@ public static <T> T[][] deepCopy(T[][] matrix) {
199201
return java.util.Arrays.stream(matrix).map(el -> el.clone()).toArray($ -> matrix.clone());
200202
}
201203

204+
public static int getMaxTextLineLength(String text, @NotNull Function<String, Integer> calcCallback) {
205+
if (text == null) {
206+
return 0;
207+
}
208+
int maxLength = -1;
209+
for (String line : text.split("(\\r?\\n|\\r)+")) {
210+
int length = calcCallback.apply(line);
211+
if (length > maxLength) {
212+
maxLength = length;
213+
}
214+
}
215+
return maxLength;
216+
}
217+
218+
public static int getMaxTextLineLength(String text) {
219+
return getMaxTextLineLength(text, input -> input == null ? 0 : input.length());
220+
}
221+
202222
private CsvHelper() {
203223
// static utility class
204224
}

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/CsvEditorSettingsExternalizable.java

+35-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
@SuppressWarnings("all")
2121
public class CsvEditorSettingsExternalizable implements PersistentStateComponent<CsvEditorSettingsExternalizable.OptionSet> {
2222

23-
public static final int TABLE_EDITOR_MIN_ROW_HEIGHT = 0;
24-
public static final int TABLE_EDITOR_MAX_ROW_HEIGHT = 10;
25-
public static final int TABLE_EDITOR_DEFAULT_ROW_HEIGHT = 3;
23+
public static final int TABLE_EDITOR_ROW_HEIGHT_MIN = 0;
24+
public static final int TABLE_EDITOR_ROW_HEIGHT_MAX = 10;
25+
public static final int TABLE_EDITOR_ROW_HEIGHT_DEFAULT = 3;
26+
public static final int TABLE_AUTO_MAX_COLUMN_WIDTH_DEFAULT = 300;
27+
public static final int TABLE_DEFAULT_COLUMN_WIDTH_DEFAULT = 100;
2628

2729
public enum EditorPrio {
2830
TEXT_FIRST,
@@ -39,6 +41,9 @@ public static final class OptionSet {
3941
public String TAB_HIGHLIGHT_COLOR;
4042
public EditorPrio EDITOR_PRIO;
4143
public int TABLE_EDITOR_ROW_HEIGHT;
44+
public int TABLE_AUTO_MAX_COLUMN_WIDTH;
45+
public int TABLE_DEFAULT_COLUMN_WIDTH;
46+
public boolean TABLE_AUTO_COLUMN_WIDTH_ON_OPEN;
4247
public boolean TABLE_COLUMN_HIGHTLIGHTING;
4348
public boolean ZERO_BASED_COLUMN_NUMBERING;
4449

@@ -56,7 +61,10 @@ public OptionSet() {
5661
TAB_HIGHLIGHT_COLOR = "-7984";
5762
EDITOR_PRIO = EditorPrio.TEXT_FIRST;
5863
SHOW_TABLE_EDITOR_INFO_PANEL = true;
59-
TABLE_EDITOR_ROW_HEIGHT = TABLE_EDITOR_DEFAULT_ROW_HEIGHT;
64+
TABLE_EDITOR_ROW_HEIGHT = TABLE_EDITOR_ROW_HEIGHT_DEFAULT;
65+
TABLE_AUTO_MAX_COLUMN_WIDTH = TABLE_AUTO_MAX_COLUMN_WIDTH_DEFAULT;
66+
TABLE_DEFAULT_COLUMN_WIDTH = TABLE_DEFAULT_COLUMN_WIDTH_DEFAULT;
67+
TABLE_AUTO_COLUMN_WIDTH_ON_OPEN = false;
6068
QUOTING_ENFORCED = false;
6169
TABLE_COLUMN_HIGHTLIGHTING = true;
6270
ZERO_BASED_COLUMN_NUMBERING = false;
@@ -160,8 +168,8 @@ public int getTableEditorRowHeight() {
160168
}
161169
public void setTableEditorRowHeight(int rowHeight) {
162170
int finalRowHeight = rowHeight;
163-
if (finalRowHeight > TABLE_EDITOR_MAX_ROW_HEIGHT) finalRowHeight = TABLE_EDITOR_MAX_ROW_HEIGHT;
164-
if (finalRowHeight < TABLE_EDITOR_MIN_ROW_HEIGHT) finalRowHeight = TABLE_EDITOR_MIN_ROW_HEIGHT;
171+
if (finalRowHeight > TABLE_EDITOR_ROW_HEIGHT_MAX) finalRowHeight = TABLE_EDITOR_ROW_HEIGHT_MAX;
172+
if (finalRowHeight < TABLE_EDITOR_ROW_HEIGHT_MIN) finalRowHeight = TABLE_EDITOR_ROW_HEIGHT_MIN;
165173
getState().TABLE_EDITOR_ROW_HEIGHT = finalRowHeight;
166174
}
167175

@@ -192,4 +200,25 @@ public boolean isFileEndLineBreak() {
192200
public void setFileEndLineBreak(boolean fileEndLineBreak) {
193201
getState().FILE_END_LINE_BREAK = fileEndLineBreak;
194202
}
203+
204+
public int getTableAutoMaxColumnWidth() {
205+
return getState().TABLE_AUTO_MAX_COLUMN_WIDTH;
206+
}
207+
public void setTableAutoMaxColumnWidth(int tableAutoMaxColumnWidth) {
208+
getState().TABLE_AUTO_MAX_COLUMN_WIDTH = tableAutoMaxColumnWidth;
209+
}
210+
211+
public int getTableDefaultColumnWidth() {
212+
return getState().TABLE_DEFAULT_COLUMN_WIDTH;
213+
}
214+
public void setTableDefaultColumnWidth(int tableDefaultColumnWidth) {
215+
getState().TABLE_DEFAULT_COLUMN_WIDTH = tableDefaultColumnWidth;
216+
}
217+
218+
public boolean isTableAutoColumnWidthOnOpen() {
219+
return getState().TABLE_AUTO_COLUMN_WIDTH_ON_OPEN;
220+
}
221+
public void setTableAutoColumnWidthOnOpen(boolean tableAutoColumnWidthOnOpen) {
222+
getState().TABLE_AUTO_COLUMN_WIDTH_ON_OPEN = tableAutoColumnWidthOnOpen;
223+
}
195224
}

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/CsvEditorSettingsProvider.form

+85-40
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
6-
<xy x="20" y="20" width="1180" height="648"/>
6+
<xy x="20" y="20" width="1180" height="735"/>
77
</constraints>
88
<properties>
99
<enabled value="true"/>
@@ -91,84 +91,129 @@
9191
</grid>
9292
</children>
9393
</grid>
94-
<grid id="3e325" layout-manager="GridLayoutManager" row-count="7" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
94+
<grid id="3e325" layout-manager="GridLayoutManager" row-count="10" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
9595
<margin top="10" left="10" bottom="10" right="10"/>
9696
<constraints>
9797
<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"/>
9898
</constraints>
9999
<properties/>
100100
<border type="line" title="Table Editor"/>
101101
<children>
102-
<grid id="a07e0" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="0">
103-
<constraints>
104-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
105-
</constraints>
106-
<properties/>
107-
<border type="none"/>
108-
<children>
109-
<component id="bfa35" class="javax.swing.JLabel">
110-
<constraints/>
111-
<properties>
112-
<text value="Text-lines per row (default):"/>
113-
</properties>
114-
</component>
115-
<component id="d6ab5" class="javax.swing.JComboBox" binding="cbRowHeight">
116-
<constraints/>
117-
<properties>
118-
<model>
119-
<item value="Auto"/>
120-
<item value="1"/>
121-
<item value="2"/>
122-
<item value="3"/>
123-
<item value="4"/>
124-
<item value="5"/>
125-
<item value="6"/>
126-
<item value="7"/>
127-
<item value="8"/>
128-
<item value="9"/>
129-
<item value="10"/>
130-
</model>
131-
</properties>
132-
</component>
133-
</children>
134-
</grid>
135102
<component id="97488" class="javax.swing.JCheckBox" binding="cbShowInfoPanel">
136103
<constraints>
137-
<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"/>
104+
<grid row="5" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
138105
</constraints>
139106
<properties>
140107
<text value="Show info panel"/>
141108
</properties>
142109
</component>
143110
<component id="d6a8e" class="javax.swing.JCheckBox" binding="cbQuotingEnforced">
144111
<constraints>
145-
<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"/>
112+
<grid row="6" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
146113
</constraints>
147114
<properties>
148115
<text value="Enforce value quoting"/>
149116
</properties>
150117
</component>
151118
<component id="f25dc" class="javax.swing.JCheckBox" binding="cbTableColumnHighlighting">
152119
<constraints>
153-
<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"/>
120+
<grid row="7" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
154121
</constraints>
155122
<properties>
156123
<text value="Enable column highlighting (Editor &gt; Color Scheme &gt; CSV &gt; Column Hightlighting Colors)"/>
157124
</properties>
158125
</component>
159126
<hspacer id="a028b">
160127
<constraints>
161-
<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"/>
128+
<grid row="8" column="0" row-span="1" col-span="6" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
162129
</constraints>
163130
</hspacer>
164131
<component id="e470c" class="javax.swing.JCheckBox" binding="cbFileEndLineBreak">
165132
<constraints>
166-
<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"/>
133+
<grid row="4" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
167134
</constraints>
168135
<properties>
169136
<text value="Keep/ignore linebreak at file end"/>
170137
</properties>
171138
</component>
139+
<component id="77ade" class="javax.swing.JLabel">
140+
<constraints>
141+
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
142+
</constraints>
143+
<properties>
144+
<text value="Maximum column width (0 = no maximum):"/>
145+
</properties>
146+
</component>
147+
<hspacer id="d4de4">
148+
<constraints>
149+
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
150+
</constraints>
151+
</hspacer>
152+
<component id="8554c" class="javax.swing.JFormattedTextField" binding="tfMaxColumnWidth" custom-create="true">
153+
<constraints>
154+
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
155+
<preferred-size width="150" height="-1"/>
156+
</grid>
157+
</constraints>
158+
<properties/>
159+
</component>
160+
<component id="b4509" class="javax.swing.JLabel">
161+
<constraints>
162+
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
163+
</constraints>
164+
<properties>
165+
<text value="Default column width (10 - 10000):"/>
166+
</properties>
167+
</component>
168+
<component id="15921" class="javax.swing.JFormattedTextField" binding="tfDefaultColumnWidth" custom-create="true">
169+
<constraints>
170+
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="0" indent="0" use-parent-layout="false">
171+
<preferred-size width="150" height="-1"/>
172+
</grid>
173+
</constraints>
174+
<properties/>
175+
</component>
176+
<hspacer id="9bde3">
177+
<constraints>
178+
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
179+
</constraints>
180+
</hspacer>
181+
<component id="5214d" class="javax.swing.JCheckBox" binding="cbAdjustColumnWidthOnOpen">
182+
<constraints>
183+
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
184+
</constraints>
185+
<properties>
186+
<text value="Adjust column width on open (default)"/>
187+
</properties>
188+
</component>
189+
<component id="bfa35" class="javax.swing.JLabel">
190+
<constraints>
191+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
192+
</constraints>
193+
<properties>
194+
<text value="Text-lines per row (default):"/>
195+
</properties>
196+
</component>
197+
<component id="d6ab5" class="javax.swing.JComboBox" binding="cbRowHeight">
198+
<constraints>
199+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
200+
</constraints>
201+
<properties>
202+
<model>
203+
<item value="Auto"/>
204+
<item value="1"/>
205+
<item value="2"/>
206+
<item value="3"/>
207+
<item value="4"/>
208+
<item value="5"/>
209+
<item value="6"/>
210+
<item value="7"/>
211+
<item value="8"/>
212+
<item value="9"/>
213+
<item value="10"/>
214+
</model>
215+
</properties>
216+
</component>
172217
</children>
173218
</grid>
174219
<vspacer id="ff292">

0 commit comments

Comments
 (0)