Search code examples
visual-studiovimcode-templates

Scripting the Visual Studio editor?


I do use a templating tool for VS. I have used JustCode's and ReSharper's. But I find that often I could use something more dynamic.

Here is a trivial example:

Given the class

class Dog
{
    public string Name { get; set; }
    public int NumLegs { get; set; }
    public DateTime Birthdate { get; set; }
}

I would like to convert it to:

class Dog
{
    public string Name { get; set; }
    public int NumLegs { get; set; }
    public DateTime Birthdate { get; set; }

    public Dog CloneMe()
    {
        return new Dog
        {
            Name = this.Name,
            NumLegs = this.NumLegs,
            Birthdate = this.Birthdate,
        };
    }
}

In the past I have just opened the file in Vim, and done it. I have also written ruby scripts to transform the file.

Both are a little cumbersome. Any suggestions on a tool designed for doing something like this?


Solution

  • Visual Studio has built-in "scripting" engine. For example, you can find properties of the selected class in the editor with (DTE.ActiveWindow.Selection as EnvDTE.TextSelection).ActivePoint.CodeElement[vsCMElement.vsCMElementClass] as EnvDTE.CodeClass and you can write to the current edit point in the VS editor with DTE.ActiveDocument.Selection.