Find Usages of custom reference problem
Hello !
I'm developing plugin with custom references. Those references must resolve to "bean properties", i.e. pair of getXXX/setXXX method.
When one clicks the link with CTRL he should be moved to setter method of bean. Those references are registered via custom PsiReferenceContributor.
There's 2 types of them: ones come from JSP others from JAVA code. JSP related references are provided for 'name' attribute of custom tag in JSP.
JAVA related references are provided for custom annotation attribute of type java.lang.String.
Let me illlustrate with code. Code of "bean"
class MyBean {
private int prop;
public int getProp() {
return prop;
}
public void setProp(int prop) {
this.prop = prop;
}
}
Here's JSP sample containing my custom reference. it's value of attribute name of custom:tag JSP TAG.
<custom:root >
<custom:tag name="prop"
</custom:root>
Here's JAVA sample containing my custom reference. it's value of name attribute of @MyAnnotation.
One of problems i can't solve is - when i call Alt+F7 on setProp method of MyBean i obtain only one usage - it's from JSP......
@MyAnnotation(name = "prop", clazz = MyBean.class)
public void doSmth() {...}
...
The other containing in JAVA code is never included into search results ?
But when i provide references not for properties for normal methods (total name match) - i see then in find usages result.
Please can someone point how can i solve this ? Or maybe if you need additional information i will be glad to publish it on request.
Thnx in advance.
Please sign in to leave a comment.
The simplest way is to implement CustomPropertyScopeProvider extension.
An example can be found in Velocity plugin sources. You should return
scope restricted by Java file type. Note that enabling reference search
in Java may slow down the performance very significantly (when you
search for getName/setName property, IDEA will parse all the files that
contain 'name', whose quantity is usually rather big), so would be
better if you restrict the scope by some other problem-specific
criteria. For example, the file should also contain a specific
annotation, IIUC.
BTW if you tune your reference's isReferenceTo, you can simply allow to
find those property usages also when searching for getters.
Thnx for idea about impelmenting CustomPropertyScopeProviderю. Apparently I will try it to see if i can create workaround for my problem.
But there's still pending question ?
Why if i provide reference in JAVA code in PsiLiteralExpression for some method with method's name exactly equals to reference text - it's included into results.
But when i provide reference to setter/getter method (reference text not equal to method name) - such references are not included into find usage results.
Well, i understood idea about tuning isReferenceTo, but i can't realize - this is solution for reported problem ? Or smth else ?
IDEA searches for method usages as follows. It finds Java files with
exact method name occurences, parses them, tries to find reference at
the occurence offset and checks isReferenceTo. Then, if it's property
accessor, it finds also files with property name occurences and performs
the same isReferenceTo test. But. The search scope is very restricted in
this case, namely, to JSP, JSPX and XML. Java is not included, because
of the described performance problems. That's what
CustomPropertyScopeProvider is for, it allows to extend the scope for
possible bare property references, like for FreeMarker and Velocity.
Seam framework support now does limited property search for Java files,
but the files it searches in are only those related to Seam framework,
so the performance isn't very much harmed.
No, it isn't related, just an advice ;)
Well, thanx for your your help ! I will try to implement my code the way you adviced.
Fortunately the search should be limited to small number of files. So i hope it will not be so sloooow :)