Search in sources :

Example 21 with SearchEngine

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();
    }
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InvocationTargetException(java.lang.reflect.InvocationTargetException) Comparator(java.util.Comparator) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) TreeSet(java.util.TreeSet) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

Aggregations

SearchEngine (org.eclipse.jdt.core.search.SearchEngine)21 CoreException (org.eclipse.core.runtime.CoreException)11 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)10 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)9 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)8 ArrayList (java.util.ArrayList)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 JavaModelException (org.eclipse.jdt.core.JavaModelException)7 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)6 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)5 IJavaElement (org.eclipse.jdt.core.IJavaElement)4 RefactoringSearchEngine (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine)4 HashSet (java.util.HashSet)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 IType (org.eclipse.jdt.core.IType)3 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)3 CollectingSearchRequestor (org.eclipse.jdt.internal.corext.refactoring.CollectingSearchRequestor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IMethod (org.eclipse.jdt.core.IMethod)2