From 7bd962bd8ac37c25d0c45b73ba32d9d4385313da Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 12 Aug 2019 14:54:14 +0200 Subject: [PATCH] Sort search results in Find in Path preview table (IDEA-173424) GitOrigin-RevId: ece09b47a4f73010e6c1b57084d132b4700ca81a --- .../intellij/find/impl/FindPopupPanel.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/platform/lang-impl/src/com/intellij/find/impl/FindPopupPanel.java b/platform/lang-impl/src/com/intellij/find/impl/FindPopupPanel.java index a2ecff4565ce..83f18f8c20e6 100644 --- a/platform/lang-impl/src/com/intellij/find/impl/FindPopupPanel.java +++ b/platform/lang-impl/src/com/intellij/find/impl/FindPopupPanel.java @@ -83,6 +83,7 @@ import java.awt.event.*; import java.lang.ref.Reference; import java.lang.ref.WeakReference; import java.util.List; +import java.util.Vector; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -1089,10 +1090,40 @@ public class FindPopupPanel extends JBPanel implements FindUI { final int hash = System.identityHashCode(myResultsPreviewSearchProgress); final DefaultTableModel model = new DefaultTableModel() { + private final Comparator> COMPARATOR = (v1, v2) -> { + UsageInfo2UsageAdapter u1 = v1.get(0); + UsageInfo2UsageAdapter u2 = v2.get(0); + int c = u1.getFile().getPath().compareTo(u2.getFile().getPath()); + if (c != 0) return c; + c = Integer.compare(u1.getLine(), u2.getLine()); + if (c != 0) return c; + return Integer.compare(u1.getUsageInfo().getNavigationOffset(), u2.getUsageInfo().getNavigationOffset()); + }; + @Override public boolean isCellEditable(int row, int column) { return false; } + + @SuppressWarnings({"UseOfObsoleteCollectionType", "unchecked"}) + @Override + //Inserts search results in sorted order + public void addRow(Object[] rowData) { + final Vector v = convertToVector(rowData); + final int p = Collections.binarySearch(dataVector, v, COMPARATOR); + assert p < 0 : "duplicate result found"; + + int row = -(p + 1); + boolean fixSelection = row == myResultsPreviewTable.getSelectedRow() && myResultsPreviewTable.getSelectedRowCount() == 1; + insertRow(row, v); + + if (fixSelection) { + // Inserting a row at the selection position grows the selection by 1. + // Prevent that growing and set the selection back to the row only. + if (row != 0) row++; // non-default selection, keep the item + myResultsPreviewTable.setRowSelectionInterval(row, row); + } + } }; model.addColumn("Usages");