public static async Task ComputeRefactoringsAsync(RefactoringContext context, ParameterSyntax parameter)
        {
            if (!context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.AddIdentifierToParameter,
                    RefactoringIdentifiers.RenameParameterAccordingToTypeName))
            {
                return;
            }

            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            IParameterSymbol parameterSymbol = semanticModel.GetDeclaredSymbol(parameter, context.CancellationToken);

            if (parameterSymbol?.Type == null)
            {
                return;
            }

            if (parameter.Identifier.IsMissing)
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddIdentifierToParameter))
                {
                    TextSpan span = (parameter.Type != null)
                        ? TextSpan.FromBounds(parameter.Type.Span.End, parameter.Span.End)
                        : parameter.Span;

                    if (span.Contains(context.Span))
                    {
                        string name = NameGenerator.CreateName(parameterSymbol.Type, firstCharToLower: true);

                        if (!string.IsNullOrEmpty(name))
                        {
                            context.RegisterRefactoring(
                                $"Add identifier '{name}'",
                                cancellationToken => AddParameterNameToParameterAsync(context.Document, parameter, name, cancellationToken),
                                RefactoringIdentifiers.AddIdentifierToParameter);
                        }
                    }
                }
            }
            else if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameParameterAccordingToTypeName) &&
                     parameter.Identifier.Span.Contains(context.Span))
            {
                string oldName = parameter.Identifier.ValueText;

                string newName = NameGenerator.Default.CreateUniqueParameterName(
                    oldName,
                    parameterSymbol,
                    semanticModel,
                    cancellationToken: context.CancellationToken);

                if (newName != null)
                {
                    context.RegisterRefactoring(
                        $"Rename '{oldName}' to '{newName}'",
                        cancellationToken => Renamer.RenameSymbolAsync(context.Solution, parameterSymbol, newName, default(OptionSet), cancellationToken),
                        RefactoringIdentifiers.RenameParameterAccordingToTypeName);
                }
            }
        }
        private static Task <Document> RefactorAsync(
            Document document,
            TypeDeclarationSyntax typeDeclaration,
            INamedTypeSymbol typeSymbol,
            ITypeSymbol elementSymbol,
            CancellationToken cancellationToken)
        {
            TypeSyntax type = typeSymbol.ToTypeSyntax().WithSimplifierAnnotation();

            TypeSyntax elementType = elementSymbol.ToTypeSyntax().WithSimplifierAnnotation();

            string identifier = NameGenerator.CreateName(typeSymbol, firstCharToLower: true) ?? DefaultNames.Variable;

            string identifierWithUnderscore = "_" + identifier;

            MethodDeclarationSyntax getEnumeratorDeclaration = MethodDeclaration(
                Modifiers.Public(),
                IdentifierName("Enumerator"),
                Identifier("GetEnumerator"),
                ParameterList(),
                Block(
                    ReturnStatement(
                        ObjectCreationExpression(
                            IdentifierName("Enumerator"), ArgumentList(Argument(ThisExpression()))))));

            StructDeclarationSyntax enumeratorDeclaration = StructDeclaration(
                Modifiers.Public(),
                Identifier("Enumerator").WithRenameAnnotation(),
                CreateEnumeratorMembers(type, elementType, identifier, identifierWithUnderscore).ToSyntaxList());

            ClassDeclarationSyntax enumeratorImplDeclaration = ClassDeclaration(
                attributeLists: default,
Exemplo n.º 3
0
        private static async Task RenamePropertyAccordingToTypeName(RefactoringContext context, PropertyDeclarationSyntax propertyDeclaration)
        {
            TypeSyntax type = propertyDeclaration.Type;

            if (type == null)
            {
                return;
            }

            SyntaxToken identifier = propertyDeclaration.Identifier;

            if (!context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(identifier))
            {
                return;
            }

            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken);

            if (typeSymbol?.IsErrorType() != false)
            {
                return;
            }

            string newName = NameGenerator.CreateName(typeSymbol);

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

            string oldName = identifier.ValueText;

            newName = StringUtility.FirstCharToUpper(newName);

            if (string.Equals(oldName, newName, StringComparison.Ordinal))
            {
                return;
            }

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

            if (!await MemberNameGenerator.IsUniqueMemberNameAsync(
                    newName,
                    symbol,
                    context.Solution,
                    cancellationToken: context.CancellationToken)
                .ConfigureAwait(false))
            {
                return;
            }

            context.RegisterRefactoring(
                $"Rename '{oldName}' to '{newName}'",
                cancellationToken => Renamer.RenameSymbolAsync(context.Solution, symbol, newName, default(OptionSet), cancellationToken),
                RefactoringIdentifiers.RenamePropertyAccordingToTypeName);
        }
        internal static Task <Document> RefactorAsync(
            Document document,
            ExpressionStatementSyntax expressionStatement,
            ITypeSymbol typeSymbol,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            MemberDeclarationSyntax containingMember = expressionStatement.FirstAncestor <MemberDeclarationSyntax>();

            var containingType = (TypeDeclarationSyntax)containingMember.Parent;

            string name = NameGenerator.CreateName(typeSymbol, firstCharToLower: true) ?? DefaultNames.Variable;

            if (RefactoringSettings.Current.PrefixFieldIdentifierWithUnderscore)
            {
                name = "_" + name;
            }

            name = NameGenerator.Default.EnsureUniqueLocalName(
                name,
                semanticModel,
                expressionStatement.SpanStart,
                cancellationToken: cancellationToken);

            name = NameGenerator.Default.EnsureUniqueMemberName(
                name,
                semanticModel,
                containingType.OpenBraceToken.Span.End,
                cancellationToken: cancellationToken);

            ExpressionSyntax expression = expressionStatement.Expression;

            ExpressionStatementSyntax newExpressionStatement = ExpressionStatement(
                SimpleAssignmentExpression(
                    IdentifierName(Identifier(name).WithRenameAnnotation()),
                    expression.WithoutTrivia()).WithTriviaFrom(expression));

            newExpressionStatement = newExpressionStatement
                                     .WithTriviaFrom(expressionStatement)
                                     .WithFormatterAnnotation();

            FieldDeclarationSyntax fieldDeclaration = FieldDeclaration(
                (containingMember.GetModifiers().Contains(SyntaxKind.StaticKeyword)) ? Modifiers.PrivateStatic() : Modifiers.Private(),
                typeSymbol.ToMinimalTypeSyntax(semanticModel, containingType.OpenBraceToken.Span.End),
                name);

            fieldDeclaration = fieldDeclaration.WithFormatterAnnotation();

            TypeDeclarationSyntax newNode = containingType
                                            .ReplaceNode(expressionStatement, newExpressionStatement)
                                            .InsertMember(fieldDeclaration, MemberDeclarationComparer.ByKind);

            return(document.ReplaceNodeAsync(containingType, newNode, cancellationToken));
        }
        private static async Task RenameMethodAccoringToTypeNameAsync(
            RefactoringContext context,
            MethodDeclarationSyntax methodDeclaration)
        {
            TypeSyntax returnType = methodDeclaration.ReturnType;

            if (returnType?.IsVoid() != false)
                return;

            SyntaxToken identifier = methodDeclaration.Identifier;

            if (!context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(identifier))
                return;

            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            IMethodSymbol methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration, context.CancellationToken);

            ITypeSymbol typeSymbol = GetType(returnType, semanticModel, context.CancellationToken);

            if (typeSymbol == null)
                return;

            string newName = NameGenerator.CreateName(typeSymbol);

            if (string.IsNullOrEmpty(newName))
                return;

            newName = "Get" + newName;

            if (methodSymbol.IsAsync)
                newName += "Async";

            string oldName = identifier.ValueText;

            if (string.Equals(oldName, newName, StringComparison.Ordinal))
                return;

            if (!await MemberNameGenerator.IsUniqueMemberNameAsync(
                newName,
                methodSymbol,
                context.Solution,
                cancellationToken: context.CancellationToken).ConfigureAwait(false))
            {
                return;
            }

            context.RegisterRefactoring(
                $"Rename '{oldName}' to '{newName}'",
                cancellationToken => Renamer.RenameSymbolAsync(context.Solution, methodSymbol, newName, default(OptionSet), cancellationToken),
                RefactoringIdentifiers.RenameMethodAccordingToTypeName);
        }
        private async Task <Document> _RefactorToParallelForAsync(Document document, ForStatementSyntax statement, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            root = root.ReplaceNode(statement, _CreateParallelFor(statement));

            if (root is CompilationUnitSyntax compilation)
            {
                root = UsingGenerator.AddUsingIfMissing(compilation, NameGenerator.CreateName("System", "Threading", "Tasks"));
            }

            return(document.WithSyntaxRoot(root));
        }
Exemplo n.º 7
0
        private static async Task <Document> RefactorAsync(
            Document document,
            ExpressionStatementSyntax expressionStatement,
            ITypeSymbol typeSymbol,
            bool addAwait,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (addAwait)
            {
                typeSymbol = ((INamedTypeSymbol)typeSymbol).TypeArguments[0];
            }

            string name = NameGenerator.CreateName(typeSymbol, firstCharToLower: true) ?? DefaultNames.Variable;

            name = NameGenerator.Default.EnsureUniqueLocalName(
                name,
                semanticModel,
                expressionStatement.SpanStart,
                cancellationToken: cancellationToken);

            ExpressionSyntax value = expressionStatement.Expression;

            if (addAwait)
            {
                value = AwaitExpression(value);
            }

            LocalDeclarationStatementSyntax newNode = LocalDeclarationStatement(
                VarType(),
                Identifier(name).WithRenameAnnotation(),
                value);

            newNode = newNode
                      .WithTriviaFrom(expressionStatement)
                      .WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(expressionStatement, newNode, cancellationToken).ConfigureAwait(false));
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, PropertyDeclarationSyntax propertyDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplacePropertyWithMethod) &&
                propertyDeclaration.HeaderSpan().Contains(context.Span))
            {
                ReplacePropertyWithMethodRefactoring.ComputeRefactoring(context, propertyDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemovePropertyInitializer) &&
                RemovePropertyInitializerRefactoring.CanRefactor(context, propertyDeclaration))
            {
                context.RegisterRefactoring(
                    "Remove property initializer",
                    cancellationToken => RemovePropertyInitializerRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken));
            }

            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ExpandProperty,
                    RefactoringIdentifiers.ExpandPropertyAndAddBackingField) &&
                propertyDeclaration.Span.Contains(context.Span) &&
                ExpandPropertyRefactoring.CanRefactor(propertyDeclaration))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandProperty))
                {
                    context.RegisterRefactoring(
                        "Expand property",
                        cancellationToken => ExpandPropertyRefactoring.RefactorAsync(context.Document, propertyDeclaration, cancellationToken));
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandPropertyAndAddBackingField))
                {
                    context.RegisterRefactoring(
                        "Expand property and add backing field",
                        cancellationToken => ExpandPropertyAndAddBackingFieldRefactoring.RefactorAsync(context.Document, propertyDeclaration, context.Settings.PrefixFieldIdentifierWithUnderscore, cancellationToken));
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.NotifyPropertyChanged) &&
                await NotifyPropertyChangedRefactoring.CanRefactorAsync(context, propertyDeclaration).ConfigureAwait(false))
            {
                context.RegisterRefactoring(
                    "Notify property changed",
                    cancellationToken =>
                {
                    return(NotifyPropertyChangedRefactoring.RefactorAsync(
                               context.Document,
                               propertyDeclaration,
                               context.SupportsCSharp6,
                               cancellationToken));
                });
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) &&
                propertyDeclaration.HeaderSpan().Contains(context.Span))
            {
                MakePropertyAbstractRefactoring.ComputeRefactoring(context, propertyDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberVirtual) &&
                propertyDeclaration.HeaderSpan().Contains(context.Span))
            {
                MakePropertyVirtualRefactoring.ComputeRefactoring(context, propertyDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                propertyDeclaration.HeaderSpan().Contains(context.Span))
            {
                await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, propertyDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenamePropertyAccordingToTypeName))
            {
                TypeSyntax type = propertyDeclaration.Type;

                if (type != null)
                {
                    SyntaxToken identifier = propertyDeclaration.Identifier;

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

                        ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken);

                        if (typeSymbol?.IsErrorType() == false)
                        {
                            string newName = NameGenerator.CreateName(typeSymbol);

                            if (!string.IsNullOrEmpty(newName))
                            {
                                string oldName = identifier.ValueText;

                                newName = StringUtility.FirstCharToUpper(newName);

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

                                    if (await WorkspaceNameGenerator.IsUniqueMemberNameAsync(
                                            newName,
                                            symbol,
                                            context.Solution,
                                            cancellationToken: context.CancellationToken).ConfigureAwait(false))
                                    {
                                        context.RegisterRefactoring(
                                            $"Rename '{oldName}' to '{newName}'",
                                            cancellationToken => Renamer.RenameSymbolAsync(context.Solution, symbol, newName, default(OptionSet), cancellationToken));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddMemberToInterface) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(propertyDeclaration.Identifier))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                AddMemberToInterfaceRefactoring.ComputeRefactoring(context, propertyDeclaration, semanticModel);
            }
        }
Exemplo n.º 9
0
 public static string GenerateName(bool gender, int length)
 {
     nameGen = new NameGenerator();
     return nameGen.CreateName(gender, length);
 }
Exemplo n.º 10
0
 public void CreateNameWithTwoComponents()
 {
     Assert.AreEqual("System.Threading", NameGenerator.CreateName("System", "Threading").ToString());
 }
Exemplo n.º 11
0
 public void CreateNameWithFourComponents()
 {
     Assert.AreEqual("Microsoft.CodeAnalysis.CSharp.Syntax", NameGenerator.CreateName("Microsoft", "CodeAnalysis", "CSharp", "Syntax").ToString());
 }
Exemplo n.º 12
0
 public void CreateNameWithThreeComponents()
 {
     Assert.AreEqual("Microsoft.VisualStudio.TestTools", NameGenerator.CreateName("Microsoft", "VisualStudio", "TestTools").ToString());
 }