getRangeInElement for PsiReference in comments
Hi Community,
I would like to implement the getRangeInElement Method of the PsiReference properly.
The problem is that the Reference is a Java Comment but it seems that comments can't be underlined, so no reference is found.
If I set the TextRange to something which can be underlined, like a variable or a classname, it works correctly.
I tried different ways but none helped.
Is it possible to set the TextRange of the reference element on a Java Comment?
Thanks in advance,
Markus
请先登录再写评论。
I'm not sure I understansd your question. The getRangeInElement() method returns a relative range within the element where the text corresponding to your reference is found; it's difficult to implement it "properly" or "improperly".
If your problem is that "Find usages" for the target element does not find references in comments, then it can't be solved by changing the implementation of getRangeInElement(). What you need is a searcher implementation that plugs into the referencesSearch extension point and finds the necessary text in comment context. Specifically, you need to call queryParameters.getOptimizer().searchWord() with UsageSearchContext.IN_COMMENTS as the value of the second parameter.
Thank you for your reply.
I implemented PsiReference.getRangeInElement() in two ways. First one is returning the correct TextRange for the element, second one is a TextRange from 1 to eof. The resulting behavior can be seen on the two pictures I attached.
So the correct TextRange should underline the target element and resolve the reference, but this does not happen.
I hope this helps to understand my problem.
Markus
Attachment(s):
complete_Textrange.png
correct_Textrange.png
This method returns a relative range. The correct text range for an element is 0 to element.getTextLength()-1.
That is exactly what I implemented in the first try and is the complete_TextRange picture
This is the implementation I have done so far
So what you're doing here is finding a PsiClass by qualified name (which is very likely located in a different file) and retuning it as the element to which the reference is attached. IntelliJ IDEA kind of does what you ask it to, and highlights the TextRange that you return relative to the range of the PsiClass in your current file, which leads to the broken behavior that you observe.
The getElement() and getRangeInElement() methods need to return the element to which the refererence is attached, not the element that the reference resolves to. The latter is returned from PsiReference.resolve().
On a related note, it's not OK to return an array containing null elements from your getReferencesByElement() implementation. If you don't have any references, return an empty array.
Thank you very much Dmitry, Now I got it and
it works.
Kind regards,
Markus