private static async Task RenameVariableAccordingToTypeNameAsync(
            RefactoringContext context,
            DeclarationExpressionSyntax declarationExpression)
        {
            TypeSyntax type = declarationExpression.Type;

            if (type != null)
            {
                VariableDesignationSyntax designation = declarationExpression.Designation;

                if (designation?.IsKind(SyntaxKind.SingleVariableDesignation) == true)
                {
                    var singleVariableDesignation = (SingleVariableDesignationSyntax)designation;

                    SyntaxToken identifier = singleVariableDesignation.Identifier;

                    if (identifier.Span.Contains(context.Span))
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        var localSymbol = semanticModel.GetDeclaredSymbol(singleVariableDesignation, context.CancellationToken) as ILocalSymbol;

                        if (localSymbol?.IsErrorType() == false)
                        {
                            ITypeSymbol typeSymbol = localSymbol.Type;

                            if (typeSymbol != null)
                            {
                                string oldName = identifier.ValueText;
                                string newName = Identifier.CreateName(typeSymbol, firstCharToLower: true);

                                if (!string.IsNullOrEmpty(newName) &&
                                    !string.Equals(oldName, newName, StringComparison.Ordinal))
                                {
                                    newName = Identifier.EnsureUniqueLocalName(newName, singleVariableDesignation.SpanStart, semanticModel, context.CancellationToken);

                                    context.RegisterRefactoring(
                                        $"Rename '{oldName}' to '{newName}'",
                                        cancellationToken => Renamer.RenameSymbolAsync(context.Document, localSymbol, newName, cancellationToken));
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private static async Task RenameVariableAccordingToTypeNameAsync(
            RefactoringContext context,
            DeclarationExpressionSyntax declarationExpression)
        {
            TypeSyntax type = declarationExpression.Type;

            if (type != null)
            {
                VariableDesignationSyntax designation = declarationExpression.Designation;

                if (designation?.IsKind(SyntaxKind.SingleVariableDesignation) == true)
                {
                    var singleVariableDesignation = (SingleVariableDesignationSyntax)designation;

                    SyntaxToken identifier = singleVariableDesignation.Identifier;

                    if (identifier.Span.Contains(context.Span))
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        var localSymbol = semanticModel.GetDeclaredSymbol(singleVariableDesignation, context.CancellationToken) as ILocalSymbol;

                        if (localSymbol?.IsErrorType() == false)
                        {
                            string oldName = identifier.ValueText;

                            string newName = NameGenerator.Default.CreateUniqueLocalName(
                                localSymbol.Type,
                                oldName,
                                semanticModel,
                                singleVariableDesignation.SpanStart,
                                cancellationToken: context.CancellationToken);

                            if (newName != null)
                            {
                                context.RegisterRefactoring(
                                    $"Rename '{oldName}' to '{newName}'",
                                    cancellationToken => Renamer.RenameSymbolAsync(context.Solution, localSymbol, newName, default(OptionSet), cancellationToken));
                            }
                        }
                    }
                }
            }
        }
        internal static async Task ComputeRefactoringAsync(RefactoringContext context, DeclarationPatternSyntax declarationPattern)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameIdentifierAccordingToTypeName))
            {
                VariableDesignationSyntax designation = declarationPattern.Designation;

                if (designation?.IsKind(SyntaxKind.SingleVariableDesignation) == true)
                {
                    var singleVariableDesignation = (SingleVariableDesignationSyntax)designation;

                    SyntaxToken identifier = singleVariableDesignation.Identifier;

                    if (identifier.Span.Contains(context.Span))
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

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

                        if (symbol?.IsLocal() == true)
                        {
                            var localSymbol = (ILocalSymbol)symbol;

                            string oldName = identifier.ValueText;

                            string newName = NameGenerator.Default.CreateUniqueLocalName(
                                localSymbol.Type,
                                oldName,
                                semanticModel,
                                singleVariableDesignation.SpanStart,
                                cancellationToken: context.CancellationToken);

                            if (newName != null)
                            {
                                context.RegisterRefactoring(
                                    $"Rename '{oldName}' to '{newName}'",
                                    cancellationToken => Renamer.RenameSymbolAsync(context.Solution, symbol, newName, default(OptionSet), cancellationToken));
                            }
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        public static TypeAnalysisFlags AnalyzeType(
            DeclarationExpressionSyntax declarationExpression,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (declarationExpression == null)
            {
                throw new ArgumentNullException(nameof(declarationExpression));
            }

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

            TypeSyntax type = declarationExpression.Type;

            if (type != null)
            {
                VariableDesignationSyntax designation = declarationExpression.Designation;

                if (designation?.IsKind(SyntaxKind.SingleVariableDesignation) == true)
                {
                    var symbol = semanticModel.GetDeclaredSymbol((SingleVariableDesignationSyntax)designation, cancellationToken) as ILocalSymbol;

                    if (symbol?.IsErrorType() == false)
                    {
                        ITypeSymbol typeSymbol = symbol.Type;

                        if (typeSymbol?.IsErrorType() == false)
                        {
                            var flags = TypeAnalysisFlags.None;

                            if (typeSymbol.IsDynamicType())
                            {
                                flags = TypeAnalysisFlags.Dynamic;
                            }
                            else
                            {
                                flags = TypeAnalysisFlags.ValidSymbol;

                                if (type.IsVar)
                                {
                                    flags |= TypeAnalysisFlags.Implicit;

                                    if (symbol.Type.SupportsExplicitDeclaration())
                                    {
                                        flags |= TypeAnalysisFlags.SupportsExplicit;
                                    }
                                }
                                else
                                {
                                    flags |= TypeAnalysisFlags.Explicit;
                                    flags |= TypeAnalysisFlags.SupportsImplicit;
                                }
                            }

                            return(flags);
                        }
                    }
                }
            }

            return(TypeAnalysisFlags.None);
        }