Implement Navigate To Source in a Diagram

Hello

I've implemented a diagram using com.intellij.diagram.* and would like to add navigation to source. Meaning, press F4 on a node and the corresponding PsiElement should be opened in the editor.

I tried a bit but it was not successfull.

I tried to use DiagramExtras to register extra actions

 
public class ReferenceDiagramExtras extends DiagramExtras<ReferenceElement> {
    @Override
    public
List getExtraActions() {
        return Arrays.asList(new ViewSourceAction());
    }
}


But then, the BaseNavigateToSourceAction cannot resolve Navigatables and the following returns null:

@Nullable
protected Navigatable[] getNavigatables(DataContext var1) {
    return (Navigatable[])CommonDataKeys.NAVIGATABLE_ARRAY.getData(var1);
}


As I tried to subclass ViewSourceAction and to return Navigatables for my own, then, suprisingly, the overwritten method getNavigatables() was not called.

Has anybody an idea?

Or are there any open source plugins out there using the diagram framework com.intellij.diagram? I just can find the IntelliJ Diagram Demo on GitHub https://github.com/bulenkov/IntelliJIdeaDiagramDemo.

Cheers
Stefan

1 comment
Comment actions Permalink

Hi again

I actually figured it out for my self.

Apparently, the goto source action is already configured. But the missing part is, that the action system needs a representation of the node that is understandable, i.e. a VirtualFile or as in my case a  PsiElement.

That is done by DiagramExtras#getData.

 
public class ReferenceDiagramExtras extends DiagramExtras<ReferenceElement> {

    @Nullable
    @Override
    public
Object getData(String dataId, List<DiagramNode<ReferenceElement>> nodes, DiagramBuilder builder) {
        if (nodes.size() == 1) {
            if (CommonDataKeys.PSI_ELEMENT.is(dataId)) {
                ReferenceElement referenceElement = nodes.get(0).getIdentifyingElement();
                return referenceElement.getPsiElement();
            }
        }
        return super.getData(dataId, nodes, builder);
    }

}


As example, see the corresponding github project https://github.com/Stefku/intellij-reference-diagram

Cheers
Stefan

0

Please sign in to leave a comment.