Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce freeze times on updating search results on resource changes #2288

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundles/org.eclipse.search/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.search; singleton:=true
Bundle-Version: 3.16.400.qualifier
Bundle-Version: 3.17.0.qualifier
Bundle-Activator: org.eclipse.search.internal.ui.SearchPlugin
Bundle-ActivationPolicy: lazy
Bundle-Vendor: %providerName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -308,6 +309,19 @@ public int getMatchCount() {
return count;
}

/**
* @return {@code true} if the result is not empty
* @since 3.17
*/
public boolean hasMatches() {
for (Entry<Object, Set<Match>> entry : fElementsToMatches.entrySet()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when only values are needed better iterate over Map.values()

if (!entry.getValue().isEmpty()) {
return true;
}
}
return false;
}

/**
* Returns the number of matches reported against a given element. This is
* equivalent to calling <code>getMatches(element).length</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;

import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import org.eclipse.search.ui.text.AbstractTextSearchResult;
import org.eclipse.search.ui.text.Match;
import org.eclipse.search.ui.text.MatchFilter;
Expand Down Expand Up @@ -170,17 +172,27 @@ private boolean hasMatches(Object element) {
if (element instanceof LineElement) {
LineElement lineElement= (LineElement) element;
IResource resource = lineElement.getParent();
if (getMatchCount(resource) > 0) {
if (hasMatches(resource)) {
return lineElement.hasMatches(fResult);
}
}
return fPage.getDisplayedMatchCount(element) > 0;
}

private boolean hasMatches(IResource element) {
if (hasActiveMatchFilters()) {
return fPage.getDisplayedMatchCount(element) > 0;
} else {
return fResult.hasMatches();
}
}

private int getMatchCount(Object element) {
return fResult.getActiveMatchFilters() != null && fResult.getActiveMatchFilters().length > 0
? fPage.getDisplayedMatchCount(element)
: fResult.getMatchCount();
if (hasActiveMatchFilters()) {
return fPage.getDisplayedMatchCount(element);
} else {
return fResult.getMatchCount();
}
}

private void removeFromSiblings(Object element, Object parent) {
Expand Down Expand Up @@ -208,7 +220,11 @@ public Object[] getChildren(Object parentElement) {

@Override
public boolean hasChildren(Object element) {
return getChildren(element).length > 0;
Set<Object> children = fChildrenMap.get(element);
if (children == null) {
return false;
}
return !children.isEmpty();
}

static <T> Stream<T> toStream(Enumeration<T> e) {
Expand Down Expand Up @@ -244,7 +260,7 @@ public synchronized void elementsChanged(Object[] updatedElements) {
Set<LineElement> lineMatches = Collections.emptySet();
// if we have active match filters, we should only use non-filtered FileMatch
// objects to collect LineElements to update
if (fResult.getActiveMatchFilters() != null && fResult.getActiveMatchFilters().length > 0) {
if (hasActiveMatchFilters()) {
lineMatches = Arrays.stream(updatedElements).filter(LineElement.class::isInstance)
// only for distinct files:
.map(u -> ((LineElement) u).getParent()).distinct()
Expand Down Expand Up @@ -293,6 +309,11 @@ public synchronized void elementsChanged(Object[] updatedElements) {
}
}

private boolean hasActiveMatchFilters() {
MatchFilter[] activeMatchFilters = fResult.getActiveMatchFilters();
return activeMatchFilters != null && activeMatchFilters.length > 0;
}

@Override
public void clear() {
initialize(fResult);
Expand Down
Loading