Skip to content

Commit 0c268ac

Browse files
SougandhSmerks
authored andcommitted
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 a9b8d9a commit 0c268ac

File tree

1 file changed

+73
-9
lines changed

1 file changed

+73
-9
lines changed

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

Lines changed: 73 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,21 @@ 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+
contentProvider.setComparator(sortDirectionAscending ? sorter : sorter.reversed());
995+
contentProvider.sortList();
996+
refreshWidgets();
997+
}
998+
954999
private Composite createNestedComposite(Composite parent, int numRows, boolean equalRows) {
9551000
Composite nested = new Composite(parent, SWT.NONE);
9561001
{
@@ -1468,7 +1513,7 @@ private void applyPathMatcher() {
14681513
private class ContentProvider implements IStructuredContentProvider, ILazyContentProvider {
14691514

14701515
private List items;
1471-
1516+
private Comparator<LineItem> comparator;
14721517
/**
14731518
* Creates new instance of <code>ContentProvider</code>.
14741519
*/
@@ -1551,6 +1596,25 @@ public void updateElement(int index) {
15511596

15521597
}
15531598

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

15561620
/**

0 commit comments

Comments
 (0)