DGML: how to enable the 'Go To Definition' for custom diagrams?

617 Views Asked by At

I am generating a custom workflow diagram via DGML API where each node corresponds to a C# class. I would like to be able to use the built-in 'Go To Definition' feature but the documentation is lacking.

3

There are 3 best solutions below

3
Matze On

If you know the class´s filename and the position of the symbol definition, you can use the VsShellUtilities class to open the document and scroll the code artifact into the view (by setting the caret position). In one of my extensions I do something like this...

If have a SourceInfo type which I use to store the filename and text-range...

void GotoDefinition(
    IServiceProvider serviceProvider, 
    SourceInfo source)
{
    IVsUIHierarchy hierarchy;
    uint itemId;
    IVsWindowFrame windowFrame;
    IVsTextView view;

    VsShellUtilities.OpenDocument(
        serviceProvider,
        source.Filename,
        Guid.Empty,
        out hierarchy,
        out itemId,
        out windowFrame,
        out view);

    if (view != null)
    {
        int line, column;
        int pos = source.TextRange.Start;
        if (view.GetLineAndColumn(pos, out line, out column) == VSConstants.S_OK)
        {
            view.SetCaretPos(line, column);
            view.CenterLines(line, 1);
        }
    }
}

class SourceInfo
{
    public string Filename { get; set; }

    public TextRange TextRange { get; set; }
}
1
Chris On

You cannot modify goto definition, but you can use "goto reference" instead. If you manually edit the DGML file in a text editor you can add a "Reference" property to a node, like this:

<Node Id="Boomerang" Reference="Boomerang.dgml"/>

Then when you right click this node in VS you will see a new menu appear named "Go To Reference" with a submenu containing "Reference", if you click this it will open the referenced DGML file.

See https://msdn.microsoft.com/en-us/library/ee842619.aspx#AddReferences for more detail.

0
Igor On

Visual studio have it's own property "SourceLocation"

You should declare it in properties

<Properties>
...
<Property Id="SourceLocation" Label="Start Line Number" DataType="Microsoft.VisualStudio.GraphModel.CodeSchema.SourceLocation" />
...
</Properties>

then use it inside Node element f.e.

<Node Id="class1" Label="FirstClass" SourceLocation="(Assembly=file:///D:/Prj/TestApp/AppConsole/Program.cs StartLineNumber=8 StartCharacterOffset=1 EndLineNumber=8 EndCharacterOffset=1)"/>