How can I use DocumentationProvider for all tokens?

Hi,
I'm using DocumentationProvider in my plugin. It seems to only work for PsiElements which implement PsiReference. Otherwise my documentation provider is not being called.

Is it possible to provide documentation for all kinds of elements?

Thanks a lot,
Wallaby

5 comments
Comment actions Permalink

One idea would be to create a dummy PsiElement as target reference for the PsiElements which have no applicable reference. Let these PsiElements implement PsiReference as well and return your dummy PsiElement. This way, the documentation provider will be called for these elements as well. Don't know about the navigation. But on the other hand you can make a PsiElement to reference itself instead of the dummy. This way you solve the navigation issue.

0
Comment actions Permalink

Dan,
thank you for your reply. This was a pointer in the right direction.
I ended up with a PsiReference implementation which resolves to itseld. And it overrides the canNavigate() methods with "return false" so ctrl+b has no effect on it.
This way it behaves like a normal element but provides documentation.

Wallaby

0
Comment actions Permalink

Hi. In 2014, this looks disallowed now in 12.1.x. See CtrlMouseHandler:

    public boolean isValid(Document document) {
      if (!myTargetElement.isValid()) return false;
      if (!myElementAtPointer.isValid()) return false;
      if (myTargetElement == myElementAtPointer || myTargetElement == myElementAtPointer.getParent()) return false;

     ...
    }

It won't allow you to point to yourself for this purpose. grrr...

Just for others to know.
0
Comment actions Permalink

I have the same issue, I want to provide documentation for all kinds of elements.

what is the best way to resolve it ?

0
Comment actions Permalink

A possible solution would be to have all your PsiElements return a reference. If they don't have a real element for the reference then they can return a reference to a fake element that your document provider can use to provide documentation for the element.

Here is what I use in my plugin to create references to a URL when the element does not really have a reference to a PsiElementfor navigation.

You can use something similar to provide documentation for these type of elements.

public class UrlFakePsiElement extends FakePsiElement {
    final PsiElement myElement;
    final String myUrl;

    public UrlFakePsiElement(final PsiElement element, final String url) {
        myElement = element;
        myUrl = url;
    }

    public String getUrl() {
        return myUrl;
    }

    @Override
    public boolean isPhysical() {
        return false;
    }

    @Override
    public PsiElement getParent() {
        return myElement;
    }

    @Override
    public PsiFile getContainingFile() {
        return null;
    }

    @Override
    public boolean canNavigate() {
        return true;
    }

    @Override
    public boolean canNavigateToSource() {
        return false;
    }

    @Override
    public void navigate(final boolean requestFocus) {
        MultiMarkdownPathResolver.launchExternalLink(getProject(), myUrl);
    }

    @Nullable
    @Override
    public Icon getIcon(final boolean open) {
        return GitHubLinkResolver.getIcon(myUrl);
    }
}
0

Please sign in to leave a comment.