diff --git a/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java b/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java index 5144b15e0bc6..78d0db555c2b 100644 --- a/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java +++ b/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesAction.java @@ -1270,10 +1270,14 @@ public final class ShowUsagesAction extends AnAction implements PopupAction, Hin private static int columnMaxWidth(@NotNull JTable table, int col) { TableColumn column = table.getColumnModel().getColumn(col); int width = 0; - for (int row = 0; row < table.getRowCount(); row++) { - Component component = table.prepareRenderer(column.getCellRenderer(), row, col); + for (int i = 0; i < table.getRowCount(); i++) { + int row = i; + ShowUsagesTable.MyModel model = (ShowUsagesTable.MyModel)table.getModel(); + int rendererWidth = model.getOrCalcCellWidth(i, col, () -> { + Component component = table.prepareRenderer(column.getCellRenderer(), row, col); + return component.getPreferredSize().width; + }); - int rendererWidth = component.getPreferredSize().width; width = Math.max(width, rendererWidth + table.getIntercellSpacing().width); } return width; diff --git a/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesTable.java b/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesTable.java index 83102b174a54..48f26da077cc 100644 --- a/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesTable.java +++ b/platform/lang-impl/src/com/intellij/find/actions/ShowUsagesTable.java @@ -41,6 +41,7 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Supplier; import java.util.stream.Collectors; public final class ShowUsagesTable extends JBTable implements DataProvider { @@ -293,8 +294,11 @@ public final class ShowUsagesTable extends JBTable implements DataProvider { static final class MyModel extends ListTableModel implements ModelDiff.Model { + private final CellSizesCache cellSizesCache; + private MyModel(@NotNull List data, int cols) { super(cols(cols), data, 0); + cellSizesCache = new CellSizesCache(data.size(), cols); } private static ColumnInfo @NotNull [] cols(int cols) { @@ -313,9 +317,11 @@ public final class ShowUsagesTable extends JBTable implements DataProvider { public void addToModel(int idx, UsageNode element) { if (idx < getRowCount()) { insertRow(idx, element); + cellSizesCache.addLine(idx); } else { addRow(element); + cellSizesCache.addLine(); } } @@ -323,7 +329,47 @@ public final class ShowUsagesTable extends JBTable implements DataProvider { public void removeRangeFromModel(int start, int end) { for (int i = end; i >= start; i--) { removeRow(i); + cellSizesCache.removeLine(i); } } + + public int getOrCalcCellWidth(int row, int col, Supplier supplier) { + return cellSizesCache.getOrCalculate(row, col, supplier); + } + } + + static class CellSizesCache { + + private final List table; + private final int colsNumber; + + private CellSizesCache(int rows, int cols) { + colsNumber = cols; + table = new ArrayList<>(rows); + for (int i = 0; i < rows; i++) { + table.add(new Integer[cols]); + } + } + + void addLine() { + table.add(new Integer[colsNumber]); + } + + void addLine(int row) { + table.add(row, new Integer[colsNumber]); + } + + void removeLine(int row) { + table.remove(row); + } + + int getOrCalculate(int row, int col, Supplier supplier) { + Integer cached = table.get(row)[col]; + if (cached != null) return cached; + + Integer newVal = supplier.get(); + table.get(row)[col] = newVal; + return newVal; + } } }