Showing custom usage results (ReferenceSearch) in Find toolwindow

I was trying to customize Find Usages to suit my needs. I used ReferenceSearch to find all the usages. But then I wanted to show these usages in the Find toolwindow. I searched a bit and couldn't find the page where this is documented, so posting this here. This is how I did it, and if there is a better way (or if this way is deprecated), kindly let me know the right way of accomplishing this.


Getting the custom usages:

 
final Query<PsiReference> search = ReferencesSearch.search(psiMethod); // psiMethod is the PsiMethod for which I wanted to find usages.
Collection<PsiReference> psiReferences = search.findAll();


I then did some filtering in the "psiReferences" collection. I ran the ReferencesSearch for other PsiMethods and added them to the Collection too.

Showing this results in the Find toolwindow:

 
final List<Usage> usages = new ArrayList<Usage>();
for (PsiReference reference : psiReferences) {
    if(conditingForFiltering) {
        final UsageInfo usageInfo = new UsageInfo(reference);

        Usage usage = new UsageInfo2UsageAdapter(usageInfo);

        usages.add(usage);
    }
}

UsageViewManager.getInstance(project).showUsages(UsageTarget.EMPTY_ARRAY, usages.toArray(new Usage[usages.size()]), new UsageViewPresentation());

Please sign in to leave a comment.