PsiReferences and DOM

I've having a heck of a time getting my references in my DOM to show they point to elements with a name I choose instead of names like "attribute value".

I found a spot which calls the PomService.convertToPsi(...) method.  I tried it and it seems to work, but I'm not sure if it's the correct way to go.  Also, it doesn't work for performing a find usages operation from the declaration element.  Find Usages still reports the element as the base XML PSI type instead of my DOM type.

I found a few posts about PsiMetaData, but I'm still unsure how this fits into the grand scheme of things related to DOM.

9 comments
Comment actions Permalink

So I have XML content similar to this:


<root>
     <namedElement name="someName">
                    <content />
     </namedElement>
</root>


I have the appropriate DOM interfaces and classes created.  I have the MetaData instance finally recognizing the DOM elements appropriately.

However, I'm having an issue with the PsiReferences.  I want them to function similar to Spring Bean definitions where issue the "Find Usages" command works for both the tag as a whole and the "id" element of the Spring bean.  I thought I had it, but the find usages is finding my self reference because my TextRange is out of whack.  It seems when I use PsiReferenceBase.createSelfReference(...) and pass in an XmlAttribute element and the XmlTag to resolve to, the text range in the editor is shift further to the right than expected.  So, when I try to hack around and correct it, the find usages will identify the self reference when it shouldn't.

0
Comment actions Permalink

I think I've figured out my problem.  I misunderstood what could be done and how the Spring Bean references worked.  Specifically the reference from the "id" attribute to its own bean declaration.

One thing that still confuses me is the relationship between DomMetaData instances and PsiReferences.  Is there a post that outlines this relationship, or could someone shed some light on this?

0
Comment actions Permalink

DomMetaData is an old-fashioned way of saying that an XML tag is more
than it seems to be, and it holds some model's element which can be
referred to and searched for usages. If a PsiReference.resolve() returns
such a tag, it's treated like its PsiMetaData and delegates some
functions to it (e.g. getName() and find usages presentation).

A more modern way is to resolve to a PomTarget instead. This is done via
PomService.convertToPsi() method, and the target can be anything you
like. If you wish to resolve to a DomElement with a @NameValue
annotation on some of its members, then a DomTarget is what you need.
With this strategy, you also don't need an self-reference in the id/name
attribute, DomDeclarationSearcher will figure it out for you.

0
Comment actions Permalink

So if I'm understanding this correctly.

What I should be doing is removing my @Referencing annotations for the "name" attribute of my DomElements that already have a @NameValue attribute. Currently I have the @Referencing annotation pulling in a CustomReferenceConverter which creates the self reference.

Then I should change all of my PsiReference classes to resolve to DomTargets which are converted to PsiElements using PomService.convertToPsi(...).

With this approach, the value of the @NameValue attribute will have an implicit PsiReference which will allow me to issue a Find Usages search from there or from other elements where I've created my custom PsiReferences, correct?

0
Comment actions Permalink

Almost correct, except that there will be no self-reference at all (but
the find usages from the id should nevertheless work). If something goes
wrong, please do post the problems here.

0
Comment actions Permalink

Hello, Peter.

I have successfully implemented some of the stuff described in this discussion.

DomElement: https://github.com/ishchenko/idea-mini-ibatis/blob/master/src/net/ishchenko/idea/minibatis/model/IdentifiableStatement.java
A reference that points to it via PomTarget: https://github.com/ishchenko/idea-mini-ibatis/blob/master/src/net/ishchenko/idea/minibatis/IdentifiableStatementReference.java
Note, that some of this code has not been deployed to JetBrains plugin repo yet.

I can Go to Declaration via this reference, I can find id attribute value usages (finds elements that provide IdentifiableStatementReference), and that's ok. The problem is that I find self-reference, and when I hit Find Usages when on tag name it will search just for same tag name inside file, and even finds closing tags as well, but I expect same results like when Find Usages hit on an attribute value.

Please, have a look at my code and at screenshots attached. What did I miss?



Attachment(s):
idea-domelement-2.png
idea-domelement.png
0
Comment actions Permalink

Ok, I have taken a look at how spring plugin works. It behaves like my code does. I.e. when invoking "Find Usages" when on bean tag name it finds all opening and closing bean tags, when invoking on id attribute it finds itself as well.
But I still want to remove self-reference from the list of usages. How do I do that?

0
Comment actions Permalink

This should have worked automatically, but looking at the code I can't see how. It appears that currently there's no such way, and this is a bug to be fixed. Could you file it to YouTrack please?

0

Please sign in to leave a comment.