Notes on Managed Extensibility Framework

Now that I have my AntlrVSIX extension working for Visual Studio IDE, I have now set my sights on a “meta-language” editor. This picks up the idea of an add-in that supports Antlr, and brings it forward to an add-in that supports any language. For this to work, the grammar for the language would be specified in Antlr. I would also need to add in information on what parts of the parse tree constitute what classified types for tagging in the editor. A corpus would be needed for Codebuff to work on reformatting a file containing code in the language. All seems doable. Or is it?

This idea, however, isn’t quite possible with the current framework of Visual Studio Extensions, at least not dynamically. I was hoping to have the extension read this information and act on it at runtime.

As I pointed out in a Gitter message system–and never received a reply–the problem with this idea is that I use MEF (Managed Extensibility Framework), and virtually all samples that I can find are based in MEF. Unfortunately, MEF works on a statically defined type system, i.e., the types are created upfront and compiled into a .VSIX file. You must write C# attributes to describe the language it implements. E.g., this tagger provider and the file suffix associated with the language is hardwired at compile time:

    internal sealed class AntlrClassifierProvider : ITaggerProvider
        [Name(/* AntlrVSIX.Constants.LanguageName */ "Antlr")]
        internal static ContentTypeDefinition AntlrContentType = null;

        [FileExtension(/* AntlrVSIX.Constants.FileExtension */ “.g;*.g4”)] 
        internal static FileExtensionToContentTypeDefinition AntlrFileType = null;

I can, of course, generate a per-language extension via a template, then build the extension. But I was hoping for something that would be more dynamic. Is there something more modern than MEF for implementing VS extensions, something that isn’t based on a static type system framework? Essentially, no. MEF is integrated very tightly with Visual Studio IDE.

The main problem with MEF is that it’s easy to get in a state where it does not work. MEF looks through your assembly for types tagged with MEF attributes, so it can call your code. The problem is that the type tagged with the MEF attribute must match exactly what it is looking for. Sometimes Microsoft provides an interface for what it is looking for. Sometimes, it doesn’t. It’s easy to forget to include the MEF framework in the assets section of the vsimanifest file. Sometimes if you implement multiple MEF interfaces, you break one or both from being ever called. Once it is broken, you have to start removing parts until you get it to work again in order to know what to fix. There is no debugging tools or switches for MEF, so you don’t know why something isn’t working at all.

Update Aug 19 2019 — As it turns out, it is possible to create the language describing a type system dynamically without using MEF. Also, it seems one can create a tagger with a provider that doesn’t require the FileExtension to be specified. I’ve decided to try to enhance AntlrVSIX to test out whether I can isolate the description of the Antlr itself into a grammar and tables. If it works, I’ll write an extension that will support any language.

Posted in Tip