Skip to content

Commit fc1b513

Browse files
committed
Add Sorting Functionality for Quick Search Results
This commit adds sorting functionality to the Quick Search results, allowing users to sort entries by Line Number, Text, and Path. Fixes : #1940
1 parent f41ea06 commit fc1b513

File tree

1 file changed

+78
-9
lines changed

1 file changed

+78
-9
lines changed

bundles/org.eclipse.text.quicksearch/src/org/eclipse/text/quicksearch/internal/ui/QuickSearchDialog.java

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2020 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -32,6 +32,7 @@
3232
import java.util.ArrayList;
3333
import java.util.Arrays;
3434
import java.util.Collections;
35+
import java.util.Comparator;
3536
import java.util.Deque;
3637
import java.util.LinkedList;
3738
import java.util.List;
@@ -403,6 +404,9 @@ public void update(ViewerCell cell) {
403404
private Combo searchIn;
404405
private Label listLabel;
405406

407+
private int sortColumnIndex = -1;
408+
private boolean sortDirectionAscending = true;
409+
406410
/**
407411
* Creates a new instance of the class.
408412
*
@@ -840,19 +844,45 @@ public void getName(AccessibleEvent e) {
840844
// new ScrollListener(list.getTable().getVerticalBar());
841845
// new SelectionChangedListener(list);
842846

847+
Table table = list.getTable();
843848
TableViewerColumn col = new TableViewerColumn(list, SWT.RIGHT);
849+
TableColumn column = col.getColumn();
850+
column.setText(Messages.QuickSearchDialog_line);
851+
column.setWidth(40);
852+
column.addSelectionListener(new SelectionAdapter() {
853+
@Override
854+
public void widgetSelected(SelectionEvent e) {
855+
Comparator<LineItem> lineNumberComparator = Comparator.comparingInt(LineItem::getLineNumber);
856+
handleColumnSort( table, 0, lineNumberComparator);
857+
}
858+
});
844859
col.setLabelProvider(LINE_NUMBER_LABEL_PROVIDER);
845-
col.getColumn().setText(Messages.QuickSearchDialog_line);
846-
col.getColumn().setWidth(40);
860+
847861
col = new TableViewerColumn(list, SWT.LEFT);
848-
col.getColumn().setText(Messages.QuickSearchDialog_text);
862+
column = col.getColumn();
863+
column.setText(Messages.QuickSearchDialog_text);
864+
column.setWidth(400);
865+
column.addSelectionListener(new SelectionAdapter() {
866+
@Override
867+
public void widgetSelected(SelectionEvent e) {
868+
Comparator<LineItem> textComparator = Comparator.comparing( LineItem::getText);
869+
handleColumnSort( table, 1, textComparator);
870+
}
871+
});
849872
col.setLabelProvider(LINE_TEXT_LABEL_PROVIDER);
850-
col.getColumn().setWidth(400);
873+
851874
col = new TableViewerColumn(list, SWT.LEFT);
852-
col.getColumn().setText(Messages.QuickSearchDialog_path);
875+
column = col.getColumn();
876+
column.setText(Messages.QuickSearchDialog_path);
877+
column.setWidth(150);
878+
column.addSelectionListener(new SelectionAdapter() {
879+
@Override
880+
public void widgetSelected(SelectionEvent e) {
881+
Comparator<LineItem> lineItemComparator = Comparator.comparing( item -> item.getFile().getFullPath().toString());
882+
handleColumnSort( table, 2,lineItemComparator);
883+
}
884+
});
853885
col.setLabelProvider(LINE_FILE_LABEL_PROVIDER);
854-
col.getColumn().setWidth(150);
855-
856886
new TableResizeHelper(list).enableResizing();
857887

858888
//list.setLabelProvider(getItemsListLabelProvider());
@@ -951,6 +981,25 @@ public void keyPressed(KeyEvent e) {
951981
return dialogArea;
952982
}
953983

984+
private void handleColumnSort(Table table, int columnIndex, Comparator sorter) {
985+
if (sortColumnIndex == columnIndex) {
986+
sortDirectionAscending = !sortDirectionAscending;
987+
} else {
988+
sortColumnIndex = columnIndex;
989+
sortDirectionAscending = true;
990+
}
991+
table.setSortColumn(table.getColumn(columnIndex));
992+
table.setSortDirection(sortDirectionAscending ? SWT.UP : SWT.DOWN);
993+
994+
if (sortDirectionAscending) {
995+
contentProvider.setComparator(sorter);
996+
} else {
997+
contentProvider.setComparator(sorter.reversed());
998+
}
999+
contentProvider.sortList();
1000+
refreshWidgets();
1001+
}
1002+
9541003
private Composite createNestedComposite(Composite parent, int numRows, boolean equalRows) {
9551004
Composite nested = new Composite(parent, SWT.NONE);
9561005
{
@@ -1468,7 +1517,7 @@ private void applyPathMatcher() {
14681517
private class ContentProvider implements IStructuredContentProvider, ILazyContentProvider {
14691518

14701519
private List items;
1471-
1520+
private Comparator<LineItem> comparator;
14721521
/**
14731522
* Creates new instance of <code>ContentProvider</code>.
14741523
*/
@@ -1551,6 +1600,26 @@ public void updateElement(int index) {
15511600

15521601
}
15531602

1603+
/**
1604+
* Sorts the current search results based on current comparator
1605+
*/
1606+
public void sortList() {
1607+
if (comparator == null) {
1608+
return;
1609+
}
1610+
items.sort(comparator);
1611+
}
1612+
1613+
/**
1614+
* Sets a custom comparator for comparing LineItem objects.
1615+
*
1616+
* @param comparator a <code>Comparator<code> object that defines the custom comparison logic.
1617+
*/
1618+
public void setComparator(Comparator<LineItem> comparator) {
1619+
this.comparator = comparator;
1620+
items.sort(comparator);
1621+
}
1622+
15541623
}
15551624

15561625
/**

0 commit comments

Comments
 (0)