Problem with adding additional usages to the results of the standard "Find Usages" operation

I have implemented find usages by adding an extension to the com.intellij.referencesSearch extension point

it doesn't add custom usages until I implement `FindUsagesHandlerFactory`

 
public class CustomFindUsagesHandlerFactory extends FindUsagesHandlerFactory {
    @Override
    public boolean canFindUsages
(PsiElement psiElement) {
        return psiElement instanceOf CustomElement || psiElement instanceof PsiMethod;
    
}

    @Nullable
    @Override
    public
FindUsagesHandler createFindUsagesHandler(PsiElement psiElement, boolean b) {
        return new CustomFindUsagesHandler(psiElement);
    
}
}


But After adding CustomFindUsagesHandlerFactory it stopped showing the usages for the interface's base java method as
`CustomFindUsagesHandlerFactory.canFindUsages(PsiElement)` overrides handling of java method (PsiMethod) by JavaFindUsagesHandlerFactory

Please suggest a way which prevents the overriding of java source code find usages and adds additional usages to find usages result?

7 comments
Comment actions Permalink

heya,

in canFindUsages you could test PsiElement type by comparing it against TokenSet defined in your language

 
IElementType type = PsiElement.getNode().getElementType();
TokenSet.create(list all your elements here).contains(type);

tokensets are usually defined in XParserDefinition

if PsiElement is not element in your language elements' tokenset, return false

;)

0
Comment actions Permalink

The find usage action is invoked on a java method, so

IElementType type = PsiElement.getNode().getElementType();   returns `method` type

The `method` type is not in my custom plugin tokenSet as it is a java method, so it will never return true.

0
Comment actions Permalink

>  it will never return true.

... for java methods, so other handlers will be tried - including java one.


if it returns true for your symbols, default handlers will not be tried.

or so it seems.

0
Comment actions Permalink

> ... for java methods, so other handlers will be tried - including java one.

But I want to run both my custom handler and the java handler for java methods.

How will i be able to do that with this approach?

0
Comment actions Permalink

> both my custom handler and the java handler for java methods.

does your handler find additional usages for java symbols, and you'd like both handlers (custom & default java find usage handler) to find usage of java symbols?

0
Comment actions Permalink

Yes, my custom find usages handler find additional usages for java symbols, and I'd like both handlers (custom & default java find usage handler) to find usage of java symbols.

0
Comment actions Permalink
I'd try this:


CustomFindUsagesHandler extends JavaFindUsagesHandler
   
@Override
public PsiElement[] getPrimaryElements() {...}

 
@Override
public PsiElement[] getSecondaryElements() {...}

 
@Override
public boolean processElementUsages(@NotNull final PsiElement element,
                                    @NotNull final
Processor<UsageInfo> processor,
                                    @NotNull final
FindUsagesOptions options) {...}



in these overriden methods I'd call super methods and merge the results

0

Please sign in to leave a comment.