Search code examples
c#visual-studio-extensionsenvdtevisual-studio-sdk

VS SDK ContentType does not work


I am trying to include a custom language support for Visual Studio.

To start with, I need to have GoToDefinition support. And I am struggling to get the context menu to include this command.

I have defined a ContentTypeDefinition and have included the FileExtensionToContentTypeDefinition such as:

internal sealed class GaugeFileContentType
{
    [Export]
    [Name("Gauge")]
    [BaseDefinition("code")]
    internal static ContentTypeDefinition GaugeContentTypeDefinition = null;

    [Export]
    [FileExtension(".spec")]
    [ContentType("Gauge")]
    internal static FileExtensionToContentTypeDefinition GaugeFileExtensionDefinition = null;
}

Now, despite this, on debugging, I see that DTE.ActiveDocument.Type is text, despite me adding the [BaseDefinition('code')] attribute. What am I missing here?

Are the above definitions enough to tell Visual Studio to bring up Context menu for code?

I am using Visual Studio 2013 Ultimate.


Solution

  • After a few days of head banging, I managed to figure out a way.

    I was using the Experimental Instance for debugging, and it did not clean and reinstall the extension, and thus Visual Studio continued to treat the ContentType as 'Plain Text', since that was what I had originally.

    When I build a VSIX and installed, opened the same file in a new instance of Visual Studio, it brought up the right context menu.

    However, it brought out more than what I wanted (i.e Run Unit Tests from Resharper). So I did some more digging up.

    In order to ensure that Visual Studio can handle a command, it checks for it by calling IOleCommandTarget.QueryStatus method.

    All I had to do was set the CommandFlag as (uint)OLECMDF.OLECMDF_ENABLED | (uint)OLECMDF.OLECMDF_SUPPORTED and return VSConstants.S_OK when the cmdId is VSConstants.VSStd97CmdID.GotoDefn.

    The final method looks like this:

    public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
    {
        if ((VSConstants.VSStd97CmdID)prgCmds[0].cmdID == VSConstants.VSStd97CmdID.GotoDefn)
        {
            prgCmds[0].cmdf = (uint)OLECMDF.OLECMDF_ENABLED | (uint)OLECMDF.OLECMDF_SUPPORTED;
            return VSConstants.S_OK;
        }
        return Next.QueryStatus(pguidCmdGroup, cCmds, prgCmds, pCmdText);
    }