use of org.eclipse.jdt.core.search.SearchEngine in project bndtools by bndtools.
the class PkgPatternsProposalProvider method doGenerateProposals.
@Override
protected Collection<? extends IContentProposal> doGenerateProposals(String contents, int position) {
String prefix = contents.substring(0, position);
final int replaceFromPos;
if (prefix.startsWith("!")) {
//$NON-NLS-1$
prefix = prefix.substring(1);
replaceFromPos = 1;
} else {
replaceFromPos = 0;
}
Comparator<PkgPatternProposal> comparator = new Comparator<PkgPatternProposal>() {
public int compare(PkgPatternProposal o1, PkgPatternProposal o2) {
int result = o1.getPackageFragment().getElementName().compareTo(o2.getPackageFragment().getElementName());
if (result == 0) {
result = Boolean.valueOf(o1.isWildcard()).compareTo(Boolean.valueOf(o2.isWildcard()));
}
return result;
}
};
final TreeSet<PkgPatternProposal> result = new TreeSet<PkgPatternProposal>(comparator);
final IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { searchContext.getJavaProject() });
final SearchPattern pattern = SearchPattern.createPattern("*" + prefix + "*", IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
final SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
IPackageFragment pkg = (IPackageFragment) match.getElement();
// "java." since these cannot be imported
if (pkg.isDefaultPackage() || pkg.getElementName().startsWith("java."))
return;
result.add(new PkgPatternProposal(pkg, false, replaceFromPos));
result.add(new PkgPatternProposal(pkg, true, replaceFromPos));
}
};
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
}
};
try {
IRunnableContext runContext = searchContext.getRunContext();
if (runContext != null) {
runContext.run(false, false, runnable);
} else {
runnable.run(new NullProgressMonitor());
}
return result;
} catch (InvocationTargetException e) {
logger.logError("Error searching for packages.", e);
return Collections.emptyList();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return Collections.emptyList();
}
}
Aggregations