private static void RenameVariableAccordingToTypeName(
            CodeRefactoringContext context,
            SemanticModel semanticModel,
            VariableDeclarationSyntax variableDeclaration)
        {
            if (variableDeclaration.Type == null)
            {
                return;
            }

            if (variableDeclaration.Parent?.IsKind(SyntaxKind.EventFieldDeclaration) == true)
            {
                return;
            }

            if (variableDeclaration.Variables.Count != 1)
            {
                return;
            }

            VariableDeclaratorSyntax declarator = variableDeclaration.Variables[0];

            if (!declarator.Identifier.Span.Contains(context.Span))
            {
                return;
            }

            ISymbol symbol = semanticModel.GetDeclaredSymbol(declarator, context.CancellationToken);

            if (symbol == null)
            {
                return;
            }

            string newName = NamingHelper.CreateIdentifierName(
                variableDeclaration.Type,
                semanticModel,
                FirstCharToLower(symbol));

            if (string.IsNullOrEmpty(newName))
            {
                return;
            }

            if (symbol.IsKind(SymbolKind.Field) &&
                symbol.DeclaredAccessibility == Accessibility.Private &&
                !((IFieldSymbol)symbol).IsConst)
            {
                newName = NamingHelper.ToCamelCaseWithUnderscore(newName);
            }

            if (string.Equals(declarator.Identifier.ValueText, newName, StringComparison.Ordinal))
            {
                return;
            }

            context.RegisterRefactoring(
                $"Rename {GetName(symbol)} to '{newName}'",
                cancellationToken => symbol.RenameAsync(newName, context.Document, cancellationToken));
        }
Esempio n. 2
0
        internal static void RenameAccordingToTypeName(
            MethodDeclarationSyntax methodDeclaration,
            CodeRefactoringContext context,
            SemanticModel semanticModel)
        {
            if (methodDeclaration == null)
            {
                throw new ArgumentNullException(nameof(methodDeclaration));
            }

            if (semanticModel == null)
            {
                throw new ArgumentNullException(nameof(semanticModel));
            }

            if (methodDeclaration.ReturnType?.IsVoid() == false &&
                methodDeclaration.Identifier.Span.Contains(context.Span))
            {
                string newName = NamingHelper.CreateIdentifierName(methodDeclaration.ReturnType, semanticModel);

                if (!string.IsNullOrEmpty(newName))
                {
                    newName = "Get" + newName;

                    if (!string.Equals(newName, methodDeclaration.Identifier.ToString(), StringComparison.Ordinal))
                    {
                        ISymbol symbol = semanticModel.GetDeclaredSymbol(methodDeclaration, context.CancellationToken);

                        context.RegisterRefactoring(
                            $"Rename method to '{newName}'",
                            cancellationToken => symbol.RenameAsync(newName, context.Document, cancellationToken));
                    }
                }
            }
        }
        private static string CreateParameterName(ITypeSymbol typeSymbol, SemanticModel semanticModel)
        {
            string name = NamingHelper.CreateIdentifierName(
                typeSymbol,
                firstCharToLower: true);

            if (!string.IsNullOrEmpty(name) &&
                !string.Equals(typeSymbol.Name, name, StringComparison.Ordinal))
            {
                return(name);
            }

            return(null);
        }
Esempio n. 4
0
        internal static void RenameAccordingToTypeName(
            PropertyDeclarationSyntax propertyDeclaration,
            CodeRefactoringContext context,
            SemanticModel semanticModel)
        {
            if (propertyDeclaration == null)
            {
                throw new ArgumentNullException(nameof(propertyDeclaration));
            }

            if (semanticModel == null)
            {
                throw new ArgumentNullException(nameof(semanticModel));
            }

            if (propertyDeclaration.Type == null)
            {
                return;
            }

            if (!propertyDeclaration.Identifier.Span.Contains(context.Span))
            {
                return;
            }

            string newName = NamingHelper.CreateIdentifierName(propertyDeclaration.Type, semanticModel);

            if (string.IsNullOrEmpty(newName))
            {
                return;
            }

            if (string.Equals(newName, propertyDeclaration.Identifier.ValueText, StringComparison.Ordinal))
            {
                return;
            }

            ISymbol symbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken);

            context.RegisterRefactoring(
                $"Rename property to '{newName}'",
                cancellationToken => symbol.RenameAsync(newName, context.Document, cancellationToken));
        }
        private static void RenameIdentifierNameAccordingToTypeName(
            CodeRefactoringContext context,
            SemanticModel semanticModel,
            ForEachStatementSyntax forEachStatement)
        {
            if (forEachStatement.Type == null)
            {
                return;
            }

            if (!forEachStatement.Identifier.Span.Contains(context.Span))
            {
                return;
            }

            string newName = NamingHelper.CreateIdentifierName(
                forEachStatement.Type,
                semanticModel,
                firstCharToLower: true);

            if (string.IsNullOrEmpty(newName))
            {
                return;
            }

            if (string.Equals(newName, forEachStatement.Identifier.ValueText, StringComparison.Ordinal))
            {
                return;
            }

            ISymbol symbol = semanticModel.GetDeclaredSymbol(forEachStatement, context.CancellationToken);

            context.RegisterRefactoring(
                $"Rename foreach variable to '{newName}'",
                cancellationToken => symbol.RenameAsync(newName, context.Document, cancellationToken));
        }