Skip to content

Commit

Permalink
File search: show "results filtered" message also in the tree mode
Browse files Browse the repository at this point in the history
This change unifies "filter" label handling for tree/table mode in the
file search, we show now that the matches are filtered in both cases.

See eclipse-platform#2279
  • Loading branch information
iloveeclipse committed Sep 24, 2024
1 parent eb96e2e commit fc4074b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,18 @@ public Object[] getElements() {
return fElementsToMatches.keySet().toArray();
}

/**
* Returns a number of all elements that have matches. Note that count of
* all elements that contain matches are returned. The filter state of the
* matches is not relevant.
*
* @return the number of elements in this search result
* @since 3.17
*/
public int getElementsCount() {
return fElementsToMatches.size();
}

/**
* Sets the active match filters for this result. If set to non-null, the match filters will be used to update the filter
* state ({@link Match#isFiltered()} of matches and the {@link AbstractTextSearchViewPage} will only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,24 @@
import java.util.Objects;
import java.util.Set;

import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Tree;

import org.eclipse.core.runtime.IAdaptable;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.ErrorDialog;
Expand All @@ -50,21 +60,7 @@
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.search.internal.ui.Messages;
import org.eclipse.search.internal.ui.SearchMessages;
import org.eclipse.search.ui.IContextMenuConstants;
import org.eclipse.search.ui.ISearchResultViewPart;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.search.ui.text.AbstractTextSearchResult;
import org.eclipse.search.ui.text.AbstractTextSearchViewPage;
import org.eclipse.search.ui.text.Match;
import org.eclipse.search2.internal.ui.OpenSearchPreferencesAction;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Tree;

import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IWorkbenchPage;
Expand All @@ -77,6 +73,17 @@
import org.eclipse.ui.part.ResourceTransfer;
import org.eclipse.ui.part.ShowInContext;

import org.eclipse.search.internal.ui.Messages;
import org.eclipse.search.internal.ui.SearchMessages;
import org.eclipse.search.ui.IContextMenuConstants;
import org.eclipse.search.ui.ISearchResultViewPart;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.search.ui.text.AbstractTextSearchResult;
import org.eclipse.search.ui.text.AbstractTextSearchViewPage;
import org.eclipse.search.ui.text.Match;

import org.eclipse.search2.internal.ui.OpenSearchPreferencesAction;


public class FileSearchPage extends AbstractTextSearchViewPage implements IAdaptable {

Expand Down Expand Up @@ -432,29 +439,22 @@ private boolean isQueryRunning() {
@Override
public String getLabel() {
String label= super.getLabel();
StructuredViewer viewer= getViewer();
AbstractTextSearchResult result = getInput();
String msg = label;
if (result != null) {
if (viewer instanceof TableViewer) {
TableViewer tv = (TableViewer) viewer;

int itemCount = ((FileTableContentProvider) tv.getContentProvider())
.getUnfilteredElements(getInput()).length;
if (showLineMatches()) {
int matchCount= getInput().getMatchCount();
if (itemCount < matchCount) {
msg = Messages.format(SearchMessages.FileSearchPage_limited_format_matches,
new Object[] { label, Integer.valueOf(itemCount), Integer.valueOf(matchCount) });
}
} else {
int fileCount= getInput().getElements().length;
if (itemCount < fileCount) {
msg = Messages.format(SearchMessages.FileSearchPage_limited_format_files,
new Object[] { label, Integer.valueOf(itemCount), Integer.valueOf(fileCount) });
}
int itemCount = fContentProvider.getLeafCount(result);
if (showLineMatches()) {
int matchCount = result.getMatchCount();
if (itemCount < matchCount) {
msg = Messages.format(SearchMessages.FileSearchPage_limited_format_matches,
new Object[] { label, Integer.valueOf(itemCount), Integer.valueOf(matchCount) });
}
} else {
int fileCount = result.getElementsCount();
if (itemCount < fileCount) {
msg = Messages.format(SearchMessages.FileSearchPage_limited_format_files,
new Object[] { label, Integer.valueOf(itemCount), Integer.valueOf(fileCount) });
}

}
if (result.getActiveMatchFilters() != null && result.getActiveMatchFilters().length > 0) {
if (isQueryRunning()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;

import org.eclipse.search.ui.text.AbstractTextSearchResult;

public class FileTableContentProvider implements IStructuredContentProvider, IFileSearchContentProvider {
Expand All @@ -37,15 +38,17 @@ public void dispose() {
// nothing to do
}

public Object[] getUnfilteredElements(AbstractTextSearchResult searchResult) {
@Override
public int getLeafCount(Object parentElement) {
if (!(parentElement instanceof AbstractTextSearchResult searchResult)) {
return 0;
}
int elementLimit = getElementLimit();
Object[] elements = searchResult.getElements();
if (elementLimit != -1 && elements.length > elementLimit) {
Object[] shownElements = new Object[elementLimit];
System.arraycopy(elements, 0, shownElements, 0, elementLimit);
return shownElements;
int elementsCount = searchResult.getElementsCount();
if (elementLimit != -1 && elementsCount > elementLimit) {
return elementLimit;
}
return elements;
return elementsCount;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class FileTreeContentProvider implements ITreeContentProvider, IFileSearc
FileTreeContentProvider(FileSearchPage page, AbstractTreeViewer viewer) {
fPage= page;
fTreeViewer= viewer;
fChildrenMap = new HashMap<>();
}

@Override
Expand Down Expand Up @@ -218,6 +219,24 @@ public Object[] getChildren(Object parentElement) {
return children.toArray();
}

@Override
public int getLeafCount(Object parentElement) {
Object[] children = getChildren(parentElement);
if (children.length == 0) {
return 0;
}
int count = 0;
for (Object object : children) {
boolean leaf = !hasChildren(object);
if (leaf) {
count++;
} else {
count += getLeafCount(object);
}
}
return count;
}

@Override
public boolean hasChildren(Object element) {
Set<Object> children = fChildrenMap.get(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ public interface IFileSearchContentProvider {

public abstract void clear();

/**
* @param parentElement
* parent element or input
* @return number of leaf elements in the tree maintained by the provider
*/
public abstract int getLeafCount(Object parentElement);

}

0 comments on commit fc4074b

Please sign in to comment.