Visual studio macro to refactor namespaces

was working on an rather assiduous task, to refactor the namespaces.  The history is something like this.

1. There is an sdk in which I refactored the namespaces.

2. There is an implementation of the sdk which I want to refactor.

I was looking into a set of tools to do the task, and solving item (1) seems to be straightforward with ResharperÎéÎ÷s Ctrl+R+R command. In 5 minutes I almost refactored all the namespaces for around 50+ interfaces. Solving 2 was bit tricky. Because of a consequence of renaming, now you have a set of 50 new interfaces and a set of 50 other unknown interfaces. Now the compiler is going to complain about these namespaces in all the files and the missing types in all the files. To fix this I decided that I am going to write some macro to do this in visual studio.

1. The following script adds the namespaces you menttion to the start of each file that had reproted an missing type error. Then it removes all the repeated and unused namespaces in all the files. Type the following code in the macro editor in visual studio (Tools ÎíÎñ> Options ÎíÎñ> Macros ÎíÎñ> Macros IDE)

Dim processed = New System.Collections.Generic.List(Of String)

   2:Dim errorItems = DTE.ToolWindows.ErrorList.ErrorItems

   3: Dim namespacesToAdd = "using everything here"


   5:          If (errorItems.Count > 0) Then

   6:              Dim outwindow = DTE.ToolWindows.OutputWindow.ActivePane

   7:              outwindow.Activate()

   8:              For i = 1 To errorItems.Count

   9:                  Dim y As ErrorItem = errorItems.Item(i)

  10:                  Dim fileName = y.FileName

  11:                  If (fileName = Nothing) Then Continue For

  12:                  <span class="rem">'only update the file with namespaces if the cause of the error is because of namespaces

  13:                  If (Not y.Description.Contains(<span class="str">&quot;The type or namespace name&quot;) Or (Not y.Description.Contains(<span class="str">&quot;does not exist in&quot;))) Then Continue For

  14:                  <span class="rem">'skip files that are already processed

  15:                  If (processed.Contains(fileName)) Then Continue For

  16:                  <span class="rem">'don't kiss auto generated files

  17:                  If (fileName.EndsWith(<span class="str">&quot;.g.cs&quot;)) Then Continue For

  18:                  <span class="rem">'only kiss c# files

  19:                  If (System.IO.Path.GetExtension(fileName) &lt;&gt; <span class="str">&quot;.cs&quot;) Then Continue For

  20:                  <span class="rem">'using the details in the error window navigate to the file

  21:                  y.Navigate()

  22:                  DTE.ActiveDocument.Activate()

  23:                  Dim textSelection As EnvDTE.TextSelection

  24:                  textSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)

  25:                  System.Threading.Thread.Sleep(10)

  26:                  textSelection.StartOfDocument()

  27:                  textSelection.Insert(namespacesToAdd)

  28:                  <span class="rem">'Give some time to the background file analyzer to parse the file

  29:                  System.Threading.Thread.Sleep(2000)

  30:                  <span class="rem">'now remove the unused usings

  31:                  DTE.ExecuteCommand(<span class="str">&quot;Edit.RemoveUnusedUsings&quot;)

  32:                  System.Threading.Thread.Sleep(100)

  33:                  DTE.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes)

  34:                  processed.Add(y.FileName)

  35:                  outwindow.OutputString(<span class="str">&quot;Processed&quot; + fileName + Environment.NewLine)

  36:              Next

  37:          End If

This scripts queries the error list window to get the list of errors in the project.  Now run the scripts, and wait till it completes Smile. At the end you should get rid of these nasty errors. I saved several thousand key strokes by not manually editing 1000+ source files. 

Have a good time Smile