ANTLR cannot generate ‘org.antlr.v4.codegen.CSharp_v4_5_1Target’ code as of version 4.1-SNAPSHOT

Today I tried to create an ANTLR 4 grammar and compile it to C#. Of course, I used Sam Harwell’s C# target for ANTLR 4. I followed all instructions, added my grammar file, compiled it and got the following error:

ANTLR cannot generate ‘org.antlr.v4.codegen.CSharp_v4_5_1Target’ code as of version 4.1-SNAPSHOT

So what’s the problem?Read More »

Add A Namespace Import Via Visual Studio Automation

Suppose you had a code document represented by a FileCodeModel object. And suppose you were modifying the containing DOM. Sometimes, you might need to add a namespace import to the document (i.e. “using …” in C# of “Import …” in VB). A namespace import is represented by the CodeImport interface. Reading out the namespace imports is easy:

var imports = fileCodeModel.CodeElements.OfType<CodeImport>();

But how do you add a new CodeImport element? Well, TIL that it’s worthwhile to look at all versions of an automation interface (e.g. FileCodeModel2 instead of just FileCodeModel). Sometimes, you might find just the method you were looking for 🙂 So the answer to the question in this case is:

var fileCodeModel = (FileCodeModel2)projectItem.FileCodeModel;
fileCodeModel.AddImport("My.New.Namespace");

Automatically Loading VSIX Packages

So you’re creating a VSIX package that does not have a menu items, tool windows or command? But still you want the package to get loaded (i.e. executed) by the IDE? What a crazy thing to wish for, one might think after spending hours searching the internet on how to do that. But don’t abandon hope, yet, since the answer is nigh.Read More »

Use T4 Templates For XSLT Transformation

Suppose you have XSLT style sheets that produce some code from XML files. Now suppose you want to integrate these style sheets into your Visual Studio solution to generate code artefacts. You could call command line tools from the pre-build event to process the XSLT files. Alternatively, you could somehow bring the XSLT markup into a T4 template and let the regular Visual Studio integration initiate execution of the template. That is actually not that difficult, but there is one major obstacle.Read More »