Example #1
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 = Identifier.EnsureUniqueMemberName(LockObjectName, lockStatement.Expression.SpanStart, semanticModel, 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 = Inserter.InsertMember(newMembers, field);

                    MemberDeclarationSyntax newNode = containingDeclaration.SetMembers(newMembers);

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

            return(document);
        }
Example #2
0
        private static async Task <Document> IntroduceFieldToLockOnAsync(
            Document document,
            LockStatementSyntax lockStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            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);

                    string name = LockObjectName;

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

                        name = SyntaxUtility.GetUniqueName(name, semanticModel, lockStatement.Expression.Span.Start);
                    }

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

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

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

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

                    SyntaxList <MemberDeclarationSyntax> newMembers = members
                                                                      .Replace(members[index], newContainingMember)
                                                                      .Insert(FindField(members, index) + 1, field);

                    MemberDeclarationSyntax newNode = containingDeclaration.SetMembers(newMembers);

                    SyntaxNode newRoot = root.ReplaceNode(containingDeclaration, newNode);

                    return(document.WithSyntaxRoot(newRoot));
                }
            }

            return(document);
        }
Example #3
0
        private static async Task <Document> IntroduceFieldToLockOnAsync(
            Document document,
            LockStatementSyntax lockStatement,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

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

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

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

                    int index = members.IndexOf(containingMember);

                    LockStatementSyntax newLockStatement = lockStatement
                                                           .WithExpression(IdentifierName(LockObjectName));

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

                    FieldDeclarationSyntax field = CreateField()
                                                   .WithAdditionalAnnotations(Formatter.Annotation);

                    SyntaxList <MemberDeclarationSyntax> newMembers = members
                                                                      .Replace(members[index], newContainingMember)
                                                                      .Insert(FindField(members, index) + 1, field);

                    MemberDeclarationSyntax newNode = containingDeclaration.SetMembers(newMembers);

                    SyntaxNode newRoot = oldRoot.ReplaceNode(containingDeclaration, newNode);

                    return(document.WithSyntaxRoot(newRoot));
                }
            }

            return(document);
        }