Esempio n. 1
0
        public static async Task <Document> RefactorAsync(
            Document document,
            PropertyDeclarationSyntax propertyDeclaration,
            bool prefixIdentifierWithUnderscore = true,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            string fieldName = StringUtility.ToCamelCase(
                propertyDeclaration.Identifier.ValueText,
                prefixWithUnderscore: prefixIdentifierWithUnderscore);

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            fieldName = NameGenerator.Default.EnsureUniqueMemberName(
                fieldName,
                semanticModel,
                propertyDeclaration.SpanStart,
                cancellationToken: cancellationToken);

            FieldDeclarationSyntax fieldDeclaration = CreateBackingField(propertyDeclaration, fieldName)
                                                      .WithFormatterAnnotation();

            PropertyDeclarationSyntax newPropertyDeclaration = ExpandPropertyAndAddBackingField(propertyDeclaration, fieldName);

            newPropertyDeclaration = ExpandPropertyRefactoring.ReplaceAbstractWithVirtual(newPropertyDeclaration);

            newPropertyDeclaration = newPropertyDeclaration
                                     .WithTriviaFrom(propertyDeclaration)
                                     .WithFormatterAnnotation();

            var parentMember = (MemberDeclarationSyntax)propertyDeclaration.Parent;
            SyntaxList <MemberDeclarationSyntax> members = parentMember.GetMembers();

            int propertyIndex = members.IndexOf(propertyDeclaration);

            if (IsReadOnlyAutoProperty(propertyDeclaration))
            {
                IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, cancellationToken);

                ImmutableArray <SyntaxNode> nodes = await SyntaxFinder.FindReferencesAsync(propertySymbol, document, cancellationToken : cancellationToken).ConfigureAwait(false);

                IdentifierNameSyntax newNode = IdentifierName(fieldName);

                MemberDeclarationSyntax newParentMember = parentMember.ReplaceNodes(nodes, (f, g) => newNode.WithTriviaFrom(f));

                members = newParentMember.GetMembers();
            }

            SyntaxList <MemberDeclarationSyntax> newMembers = members.ReplaceAt(propertyIndex, newPropertyDeclaration);

            newMembers = newMembers.InsertMember(fieldDeclaration, MemberDeclarationComparer.ByKind);

            return(await document.ReplaceNodeAsync(parentMember, parentMember.WithMembers(newMembers), cancellationToken).ConfigureAwait(false));
        }
Esempio n. 2
0
        private static Task <Document> RefactorAsync(
            Document document,
            MemberDeclarationSyntax declaration,
            List <MemberDeclarationSyntax> assignableMembers,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            MemberDeclarationSyntax parentMember = GetContainingMember(declaration);

            SyntaxList <MemberDeclarationSyntax> members = parentMember.GetMembers();

            SyntaxList <MemberDeclarationSyntax> newMembers = members.InsertMember(
                CreateConstructor(GetConstructorIdentifierText(parentMember), assignableMembers),
                MemberDeclarationComparer.ByKind);

            MemberDeclarationSyntax newNode = parentMember.WithMembers(newMembers)
                                              .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(parentMember, newNode, cancellationToken));
        }
Esempio n. 3
0
        public static Task <Document> RefactorAsync(
            Document document,
            EventFieldDeclarationSyntax eventFieldDeclaration,
            IEventSymbol eventSymbol,
            ITypeSymbol eventArgsSymbol,
            bool supportsCSharp6,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var containingMember = (MemberDeclarationSyntax)eventFieldDeclaration.Parent;

            SyntaxList <MemberDeclarationSyntax> members = containingMember.GetMembers();

            MethodDeclarationSyntax method = CreateOnEventMethod(eventSymbol, eventArgsSymbol, supportsCSharp6)
                                             .WithFormatterAnnotation();

            SyntaxList <MemberDeclarationSyntax> newMembers = members.InsertMember(method, MemberDeclarationComparer.ByKind);

            return(document.ReplaceNodeAsync(containingMember, containingMember.WithMembers(newMembers), cancellationToken));
        }
Esempio n. 4
0
        public static async Task <Document> RefactorAsync(
            Document document,
            LockStatementSyntax lockStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

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

            MemberDeclarationSyntax containingMember = lockStatement.FirstAncestorOrSelf <MemberDeclarationSyntax>();

            if (containingMember != null)
            {
                var containingDeclaration = (MemberDeclarationSyntax)containingMember
                                            .Ancestors()
                                            .FirstOrDefault(f => f.IsKind(
                                                                SyntaxKind.ClassDeclaration,
                                                                SyntaxKind.InterfaceDeclaration,
                                                                SyntaxKind.StructDeclaration));

                if (containingDeclaration != null)
                {
                    SyntaxList <MemberDeclarationSyntax> members = containingDeclaration.GetMembers();

                    int index = members.IndexOf(containingMember);

                    SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                    string name = NameGenerator.Default.EnsureUniqueLocalName(
                        LockObjectName,
                        semanticModel,
                        lockStatement.Expression.SpanStart,
                        cancellationToken: cancellationToken);

                    LockStatementSyntax newLockStatement = lockStatement
                                                           .WithExpression(IdentifierName(Identifier(name).WithRenameAnnotation()));

                    MemberDeclarationSyntax newContainingMember = containingMember
                                                                  .ReplaceNode(lockStatement, newLockStatement);

                    bool isStatic = containingMember.GetModifiers().Contains(SyntaxKind.StaticKeyword);

                    FieldDeclarationSyntax field = CreateFieldDeclaration(name, isStatic).WithFormatterAnnotation();

                    SyntaxList <MemberDeclarationSyntax> newMembers = members.ReplaceAt(index, newContainingMember);

                    newMembers = newMembers.InsertMember(field, MemberDeclarationComparer.ByKind);

                    MemberDeclarationSyntax newNode = containingDeclaration.WithMembers(newMembers);

                    return(await document.ReplaceNodeAsync(containingDeclaration, newNode, cancellationToken).ConfigureAwait(false));
                }
            }

            return(document);
        }