Skip to content

Commit c066178

Browse files
committed
[INTERNAL] consider line breaks when calculating max text length
1 parent 2541eb5 commit c066178

File tree

6 files changed

+39
-22
lines changed

6 files changed

+39
-22
lines changed

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

Lines changed: 21 additions & 1 deletion
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,7 +156,7 @@ 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)) {
159161
columnInfoMap.put(column, new CsvColumnInfo(column, length, row));
160162
} else if (columnInfoMap.get(column).getMaxLength() < length) {
@@ -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/table/CsvTableEditor.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,9 @@ public Font getFont() {
313313
return EditorColorsManager.getInstance().getGlobalScheme().getFont(EditorFontType.PLAIN);
314314
}
315315

316-
public Dimension getPreferredCellSize(int row, int column) {
317-
Object[][] data = getDataHandler().getCurrentState();
318-
String text = data[row][column].toString();
319-
if (text == null) {
320-
return new Dimension();
321-
}
322-
return new Dimension(getFont().getSize() * text.length(), getPreferredRowHeight());
316+
protected int getStringWidth(String text) {
317+
int fontSize = getFont().getSize();
318+
return CsvHelper.getMaxTextLineLength(text, input -> fontSize * input.length());
323319
}
324320

325321
public final void resetAllColumnWidths() {
@@ -345,8 +341,7 @@ protected int[] calculateDistributedColumnWidths() {
345341

346342
for (Map.Entry<Integer, CsvColumnInfo<PsiElement>> columnInfoEntry : columnInfos.entrySet()) {
347343
CsvColumnInfo<PsiElement> columnInfo = columnInfoEntry.getValue();
348-
Dimension preferredDimension = getPreferredCellSize(columnInfo.getMaxLengthRowIndex(), columnInfo.getColumnIndex());
349-
int currentWidth = preferredDimension.width;
344+
int currentWidth = getStringWidth(data[columnInfo.getMaxLengthRowIndex()][columnInfo.getColumnIndex()].toString());
350345
if (tableAutoMaxColumnWidth != 0) {
351346
currentWidth = Math.min(tableAutoMaxColumnWidth, currentWidth);
352347
}

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/table/swing/CsvTableEditorSwing.form

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<component id="bdd04" class="com.intellij.ui.components.labels.LinkLabel" binding="lnkAdjustColumnWidth">
108108
<constraints/>
109109
<properties>
110+
<icon value="media/icons/empty.png"/>
110111
<text value="Adjust column width"/>
111112
</properties>
112113
</component>

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/table/swing/CsvTableEditorSwing.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
public class CsvTableEditorSwing extends CsvTableEditor implements TableDataChangeEvent.Listener {
3636

3737
private static final int TOTAL_CELL_HEIGHT_SPACING = 3;
38-
private static final int TOTAL_CELL_WIDTH_SPACING = 10;
38+
private static final int TOTAL_CELL_WIDTH_SPACING = 8;
3939

4040
private JBTable tblEditor;
4141
private JPanel panelMain;
@@ -212,7 +212,7 @@ protected void updateEditorLayout() {
212212
getFileEditorState().setColumnWidths(columnWidths);
213213
}
214214

215-
float zoomFactor = calculateZoomFactor();
215+
float zoomFactor = getZoomFactor();
216216
for (int i = 0; i < currentColumnCount; ++i) {
217217
TableColumn column = this.tblEditor.getColumnModel().getColumn(i);
218218
column.setPreferredWidth(Math.round(columnWidths[i] * zoomFactor));
@@ -223,7 +223,7 @@ protected void updateEditorLayout() {
223223
panelInfo.setVisible(getFileEditorState().showInfoPanel());
224224
}
225225

226-
private float calculateZoomFactor() {
226+
private float getZoomFactor() {
227227
float fontHeight = getFontHeight();
228228
return fontHeight / baseFontHeight;
229229
}
@@ -401,7 +401,7 @@ public JComponent getPreferredFocusedComponent() {
401401

402402
public void storeCurrentTableLayout() {
403403
int[] widths = getCurrentColumnsWidths();
404-
float zoomFactor = calculateZoomFactor();
404+
float zoomFactor = getZoomFactor();
405405
for (int i = 0; i < widths.length; i++) {
406406
widths[i] /= zoomFactor;
407407
}
@@ -482,13 +482,13 @@ private int getFontHeight() {
482482
}
483483

484484
@Override
485-
public Dimension getPreferredCellSize(int row, int column) {
486-
JBTable table = getTable();
487-
Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
488-
// rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
489-
// getTable().getFont().getStringBounds()
490-
// return getTable().getFontMetrics(getTable().getFont()).stringWidth(text) + TOTAL_CELL_WIDTH_SPACING;
491-
return comp.getPreferredSize();
485+
protected int getStringWidth(String text) {
486+
if (text == null) {
487+
return TOTAL_CELL_WIDTH_SPACING;
488+
}
489+
JTable table = getTable();
490+
FontMetrics fontMetrics = getTable().getFontMetrics(table.getFont());
491+
return CsvHelper.getMaxTextLineLength(text, input -> (int)Math.ceil((float)(fontMetrics.stringWidth(input) + TOTAL_CELL_WIDTH_SPACING) / getZoomFactor()));
492492
}
493493

494494
public void changeFontSize(int changeAmount) {

src/main/java/net/seesharpsoft/intellij/plugins/csv/formatter/CsvFormatHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.intellij.psi.codeStyle.CodeStyleSettings;
77
import com.intellij.psi.formatter.common.AbstractBlock;
88
import net.seesharpsoft.intellij.plugins.csv.CsvColumnInfo;
9+
import net.seesharpsoft.intellij.plugins.csv.CsvHelper;
910
import net.seesharpsoft.intellij.plugins.csv.CsvLanguage;
1011
import net.seesharpsoft.intellij.plugins.csv.psi.CsvElementType;
1112
import net.seesharpsoft.intellij.plugins.csv.psi.CsvTypes;
@@ -163,7 +164,7 @@ public static int getTextLength(String text, CodeStyleSettings codeStyleSettings
163164
text = END_WHITE_SPACE_PATTERN.matcher(text).replaceFirst("");
164165
length += 2;
165166
}
166-
length += csvCodeStyleSettings.ENABLE_WIDE_CHARACTER_DETECTION ? charWidth(text, csvCodeStyleSettings.TREAT_AMBIGUOUS_CHARACTERS_AS_WIDE) : text.length();
167+
length += CsvHelper.getMaxTextLineLength(text, line -> csvCodeStyleSettings.ENABLE_WIDE_CHARACTER_DETECTION ? charWidth(line, csvCodeStyleSettings.TREAT_AMBIGUOUS_CHARACTERS_AS_WIDE) : line.length());
167168

168169
return length;
169170
}
197 Bytes
Loading

0 commit comments

Comments
 (0)