Resharper or CodeRush - global rename

筅森魡賤 提交于 2019-12-11 10:13:22

问题


Is there a way to rename all methods, properties etc. suggested by R#. I have code that I converted from java and all methods and properties are in the format like this "onBeforeInsertExpression" and I want them to follow camel casing that is common in .NET.

This question is also for CodeRush.


回答1:


No, unfortunately there isn't a way. Resharper's Code Cleanup / Reformat Code options work nicely for formatting, namepaces, etc, but will not do any automatic member renaming. You're kinda stuck doing a "Quick Fix" on each member. If you have a lot of them, this can be a pain...




回答2:


I needed the same functionality and couldn't find it. I considered writing an add-in to ReSharper using the Api but decided on a regular Visual Studio macro instead. This macro renames methods and private fields in the current document to the default ReSharper settings, but can easily be modified to iterate through all files in a project or solution.

Save this code as a .vb file and import it into your VS Macros.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module RenameMembers

    Enum NamingStyle
        UpperCamelCase
        LowerCamelCase
    End Enum


    Public Sub RenameMembers()

        Try
            'Iterate through all code elements in the open document
            IterateCodeElements(ActiveDocument.ProjectItem.FileCodeModel.CodeElements)
        Catch ex As System.Exception
        End Try

    End Sub



    'Iterate through all the code elements in the provided element
    Private Sub IterateCodeElements(ByVal colCodeElements As CodeElements)

        Dim objCodeElement As EnvDTE.CodeElement

        If Not (colCodeElements Is Nothing) Then
            For Each objCodeElement In colCodeElements
                Try
                    Dim element As CodeElement2 = CType(objCodeElement, CodeElement2)
                    If element.Kind = vsCMElement.vsCMElementVariable Then
                        RenameField(element)
                    ElseIf element.Kind = vsCMElement.vsCMElementFunction Then
                        'Rename the methods
                        ApplyNamingStyle(element, NamingStyle.UpperCamelCase)
                    ElseIf TypeOf objCodeElement Is EnvDTE.CodeNamespace Then
                        Dim objCodeNamespace = CType(objCodeElement, EnvDTE.CodeNamespace)
                        IterateCodeElements(objCodeNamespace.Members)
                    ElseIf TypeOf objCodeElement Is EnvDTE.CodeClass Then
                        Dim objCodeClass = CType(objCodeElement, EnvDTE.CodeClass)
                        IterateCodeElements(objCodeClass.Members)
                    End If
                Catch
                End Try
            Next

        End If


    End Sub


    'Rename the field members according to our code specifications
    Private Sub RenameField(ByRef element As CodeElement2)
        If element.Kind = vsCMElement.vsCMElementVariable Then
            Dim field As EnvDTE.CodeVariable = CType(element, EnvDTE.CodeVariable)
            If (field.Access = vsCMAccess.vsCMAccessPrivate) Then
                'private static readonly
                If (field.IsShared AndAlso field.IsConstant) Then
                    ApplyNamingStyle(element, NamingStyle.UpperCamelCase)
                ElseIf (Not field.IsShared) Then
                    'private field (readonly but not static)
                    ApplyNamingStyle(element, NamingStyle.LowerCamelCase, "_")
                Else
                    ApplyNamingStyle(element, NamingStyle.UpperCamelCase)
                End If
            Else
                'if is public, the first letter should be made uppercase
                ToUpperCamelCase(element)
            End If
            'if public or protected field, start with uppercase
        End If

    End Sub

    Private Function ApplyNamingStyle(ByRef element As CodeElement2, ByVal style As NamingStyle, Optional ByVal prefix As String = "", Optional ByVal suffix As String = "")
        Dim the_string As String = element.Name

        If (Not the_string Is Nothing AndAlso the_string.Length > 2) Then
            If (style = NamingStyle.LowerCamelCase) Then
                ToLowerCamelCase(the_string)
            ElseIf (style = NamingStyle.UpperCamelCase) Then
                ToUpperCamelCase(the_string)
            Else
                'add additional styles here
            End If
        End If

        AddPrefixOrSuffix(the_string, prefix, suffix)

        If (Not element.Name.Equals(the_string)) Then
            element.RenameSymbol(the_string)
        End If

    End Function


    Private Function ToLowerCamelCase(ByRef the_string As String)
        the_string = the_string.Substring(0, 1).ToLower() & the_string.Substring(1)
    End Function

    Private Function AddPrefixOrSuffix(ByRef the_string As String, Optional ByVal prefix As String = "", Optional ByVal suffix As String = "")
        If (Not the_string.StartsWith(prefix)) Then
            the_string = prefix + the_string
        End If

        If (Not the_string.EndsWith(suffix)) Then
            the_string = the_string + suffix
        End If

    End Function


    Private Function ToUpperCamelCase(ByRef the_string As String)
        the_string = the_string.Substring(0, 1).ToUpper() & the_string.Substring(1)
    End Function

End Module



回答3:


CodeRush's approach to this kind of fix is more of an interactive process.

Which is to say you have to physically be in the location of the variable whose name you wish to change and you have to change each one individually.

That said, there is a very powerful engine under CodeRush called the DXCore, which can be used to create a very wide variety of functionality. Indeed it is this layer on which the whole of CodeRush and RefactoPro are built.

I have no doubt that it could be used to create the functionality you are after. However I doubt that you would use the existing rename technology. I will have to look into this a little further, but I am optimistic about being able to produce something.




回答4:


This feature is introduced in resharper 9 as explained here, Now you can apply re-factoring in scope, file, project or solution.



来源:https://stackoverflow.com/questions/1414078/resharper-or-coderush-global-rename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!