Domanda

C'è un modo per rinominare tutti i metodi, proprietà ecc. suggeriti da R #. Ho un codice che ho convertito da Java e tutti i metodi e le proprietà sono nel formato come questo "onBeforeInsertExpression" e voglio che seguano il rivestimento del cammello che è comune in .NET.

Questa domanda è anche per CodeRush.

È stato utile?

Soluzione

No, sfortunatamente non c'è modo. La pulizia del codice di Resharper / Riformatta Le opzioni del codice funzionano bene per la formattazione, i nomi dei nomi, ecc., Ma non eseguono alcuna ridenominazione automatica dei membri. Sei un po 'bloccato a fare una "soluzione rapida" su ogni membro. Se ne hai molti, questo può essere un dolore ...

Altri suggerimenti

Avevo bisogno della stessa funzionalità e non riuscivo a trovarla. Ho pensato di scrivere un componente aggiuntivo su ReSharper usando l'API, ma ho deciso invece di utilizzare una normale macro di Visual Studio. Questa macro rinomina metodi e campi privati ??nel documento corrente con le impostazioni predefinite di ReSharper, ma può essere facilmente modificata per iterare attraverso tutti i file in un progetto o soluzione.

Salvare questo codice come file .vb e importarlo nei macro VS.

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

L'approccio di CodeRush a questo tipo di correzione è più di un processo interattivo.

Vale a dire che devi trovarti fisicamente nella posizione della variabile di cui desideri cambiare il nome e devi cambiarla singolarmente.

Detto questo, sotto CodeRush esiste un motore molto potente chiamato DXCore, che può essere utilizzato per creare una vasta gamma di funzionalità. In effetti è questo livello su cui sono costruiti l'intero CodeRush e RefactoPro.

Non ho dubbi che potrebbe essere usato per creare la funzionalità che stai cercando. Tuttavia dubito che useresti la tecnologia di rinomina esistente. Dovrò approfondire ulteriormente questo aspetto, ma sono ottimista riguardo alla capacità di produrre qualcosa.

Questa funzione è stata introdotta in resharper 9 come spiegato qui , ora puoi applicare ri factoring in ambito, file, progetto o soluzione.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top