views:

303

answers:

1

Has anyone written a macro that will remove and sort your usings in an entire project? I would like to accomplish this without any third part add-in like ReSharper.

+2  A: 

If you will consider using a Microsoft add-in, you'll find that the Power Commands utility will do this for a project or solution.

Updated: I knew I had a bit of code that did something like this, I found it and and here it is:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module FormatDocuments

    Public Sub FormatSolution()

        Dim sol As Solution = DTE.Solution

        For i As Integer = 1 To sol.Projects.Count

            FormatProject(sol.Projects.Item(i))

        Next


    End Sub

    Private Sub FormatProject(ByVal proj As Project)
        For i As Integer = 1 To proj.ProjectItems.Count

            FormatProjectItem(proj.ProjectItems.Item(i))

        Next


    End Sub

    Private Sub FormatProjectItem(ByVal projectItem As ProjectItem)

        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then

            If projectItem.Name.EndsWith(".cs") Then

                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)

                window.Activate()

                projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")

                window.Close(vsSaveChanges.vsSaveChangesYes)

            End If
        End If

        If projectItem.ProjectItems.Count > 0 Then
            For Each subProjectItem As ProjectItem In projectItem.ProjectItems
                FormatProjectItem(subProjectItem)
            Next
        End If
    End Sub


End Module
Stuart Dunkeld
The problem I have with ReSharper or the other one (I forget the name right now) is (in my experience) the IDE slows down considerably. Have you noticed powercommands doing that?
Joe
Personally I use ReSharper and take the performance hit! I wouldn't expect PowerCommands to slowdown the IDE as it doesn't do the same kind of work in the background that R# does.
Stuart Dunkeld