开发者

Remove unused Usings across entire assembly

I am wondering if maybe ReSharper is able to run through every class and remove unused usings? I looked but I don't see an option like this in R# 4.5.开发者_如何学Python Has anyone seen this in Resharper outside of just being able to remove usings in a single class?


Since Resharper 9, you can just select "in solution" scope when you clean up a using block.

Remove unused Usings across entire assembly


I believe that cleanup across a project is a new feature in ReSharper 5.

I take that back, the feature is in ReSharper 4.5. If you right click on the solution, there's a Cleanup Code... item, which allows you to apply a cleanup profile to the solution. You can create a new cleanup profile from the Code Cleanup node within ReSharper options, if you want a profile to just adjust the using directives.


There is also another way I found here, using Macros.

Step 1: Create a new macro in Visual Studio through the Tools | Macros menu.

Step 2: Paste the code below into the Module and save it

Public Module Module1
    Sub OrganizeSolution()
        Dim sol As Solution = DTE.Solution
        For i As Integer = 1 To sol.Projects.Count
            OrganizeProject(sol.Projects.Item(i))
        Next
    End Sub

    Private Sub OrganizeProject(ByVal proj As Project)
        For i As Integer = 1 To proj.ProjectItems.Count
            OrganizeProjectItem(proj.ProjectItems.Item(i))
        Next
    End Sub

    Private Sub OrganizeProjectItem(ByVal projectItem As ProjectItem)
        Dim fileIsOpen As Boolean = False
        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
            'If this is a c# file 
            If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
                'Set flag to true if file is already open 
                fileIsOpen = projectItem.IsOpen
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                window.Activate()
                projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")
                'Only close the file if it was not already open 
                If Not fileIsOpen Then
                    window.Close(vsSaveChanges.vsSaveChangesYes)
                End If
            End If
        End If
        'Be sure to apply RemoveAndSort on all of the ProjectItems. 
        If Not projectItem.ProjectItems Is Nothing Then
            For i As Integer = 1 To projectItem.ProjectItems.Count
                OrganizeProjectItem(projectItem.ProjectItems.Item(i))
            Next
        End If
        'Apply RemoveAndSort on a SubProject if it exists. 
        If Not projectItem.SubProject Is Nothing Then
            OrganizeProject(projectItem.SubProject)
        End If
    End Sub
End Module

Step 3: Run the macro on any solution that you'd like and there you have it! Enjoy :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜