Macro to Add using System.Linq

Posted by Aligned on Geeks with Blogs See other posts from Geeks with Blogs or by Aligned
Published on Tue, 05 Jun 2012 11:18:53 GMT Indexed on 2012/06/05 22:41 UTC
Read the original article Hit count: 191

Filed under:
I have Visual Studio 2010 setup to remove and sort settings on save with the Power Commands extensions. This is great, but sometimes it removes the using System.Linq at the top. I also find myself scrolling to the top to add the using when I first add System.Linq. So I wrote a quick macro to do it for me.

 Sub AddUsingLinq()
        DTE.ActiveDocument.Selection.StartOfDocument()
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
        DTE.ActiveDocument.Selection.Text = "using System.Linq;"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ExecuteCommand("Edit.FormatDocument")
        DTE.ExecuteCommand("Edit.RemoveAndSort")
        DTE.ActiveDocument.Save()
DTE.ExecuteCommand("View.NavigateBackward")
    End Sub

Hook this up to a shortcut (tools -> options -> keyboard, I chose ctrl + x, c) and you'll be moving faster than ever. 

© Geeks with Blogs or respective owner