Beispiel #1
0
        private static AccessorListSyntax GetNewAccessorList(AccessorListSyntax accessorList)
        {
            if (accessorList.IsSingleLine(includeExteriorTrivia: false))
            {
                SyntaxTriviaList triviaList = accessorList
                                              .CloseBraceToken
                                              .LeadingTrivia
                                              .Add(NewLine());

                return(accessorList
                       .RemoveWhitespace()
                       .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(triviaList)));
            }
            else
            {
                return(accessorList.ReplaceNodes(accessorList.Accessors, (f, g) =>
                {
                    if (FormatAccessorListAnalyzer.ShouldBeFormatted(f))
                    {
                        return f.RemoveWhitespace(f.Span);
                    }
                    else
                    {
                        return g;
                    }
                }));
            }
        }
        public static PropertyDeclarationSyntax CreateAutoProperty(PropertyDeclarationSyntax propertyDeclaration, EqualsValueClauseSyntax initializer)
        {
            AccessorListSyntax accessorList = CreateAccessorList(propertyDeclaration);

            if (accessorList
                .DescendantTrivia()
                .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                accessorList = accessorList.RemoveWhitespace();
            }

            PropertyDeclarationSyntax newProperty = propertyDeclaration
                                                    .WithIdentifier(propertyDeclaration.Identifier.WithTrailingTrivia(Space))
                                                    .WithExpressionBody(null)
                                                    .WithAccessorList(accessorList);

            if (initializer != null)
            {
                newProperty = newProperty
                              .WithInitializer(initializer)
                              .WithSemicolonToken(SemicolonToken());
            }
            else
            {
                newProperty = newProperty.WithSemicolonToken(default(SyntaxToken));
            }

            return(newProperty
                   .WithTriviaFrom(propertyDeclaration)
                   .WithFormatterAnnotation());
        }
Beispiel #3
0
        private static AccessorListSyntax CreateAccessorList(BlockSyntax block, bool singleline)
        {
            AccessorListSyntax accessorList = AccessorList(GetAccessorDeclaration(block));

            if (singleline)
            {
                accessorList = accessorList.RemoveWhitespace();
            }

            return(accessorList);
        }
        public static AccessorListSyntax ExpandAccessorList(AccessorListSyntax accessorList)
        {
            IEnumerable <AccessorDeclarationSyntax> accessors = accessorList
                                                                .Accessors
                                                                .Select(f => f.WithBody(Block()).WithSemicolonToken(default(SyntaxToken)));

            AccessorListSyntax newAccessorList = AccessorList(List(accessors));

            return(newAccessorList
                   .RemoveWhitespace()
                   .WithCloseBraceToken(newAccessorList.CloseBraceToken.WithLeadingTrivia(NewLine())));
        }
Beispiel #5
0
        public static Task <Document> RefactorAsync(
            Document document,
            PropertyDeclarationSyntax propertyDeclaration,
            CancellationToken cancellationToken = default)
        {
            AccessorListSyntax accessorList = AccessorList(List(ExpandProperty()));

            accessorList = accessorList
                           .RemoveWhitespace()
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(NewLine()));

            PropertyDeclarationSyntax newPropertyDeclaration = propertyDeclaration
                                                               .WithInitializer(null)
                                                               .WithSemicolonToken(default(SyntaxToken))
                                                               .WithAccessorList(accessorList);

            newPropertyDeclaration = newPropertyDeclaration
                                     .WithModifiers(newPropertyDeclaration.Modifiers.Replace(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword))
                                     .WithTriviaFrom(propertyDeclaration)
                                     .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(propertyDeclaration, newPropertyDeclaration, cancellationToken));

            IEnumerable <AccessorDeclarationSyntax> ExpandProperty()
            {
                foreach (AccessorDeclarationSyntax accessor in propertyDeclaration.AccessorList.Accessors)
                {
                    if (accessor.IsKind(SyntaxKind.GetAccessorDeclaration))
                    {
                        ExpressionSyntax value = propertyDeclaration.Initializer?.Value;

                        if (value != null)
                        {
                            yield return(accessor
                                         .WithBody(Block(ReturnStatement(value)))
                                         .WithSemicolonToken(default(SyntaxToken)));

                            continue;
                        }
                    }

                    BlockSyntax body = Block(
                        OpenBraceToken(),
                        List <StatementSyntax>(),
                        CloseBraceToken().WithNavigationAnnotation());

                    yield return(accessor
                                 .WithBody(body)
                                 .WithSemicolonToken(default(SyntaxToken)));
                }
            }
        }
        public static PropertyDeclarationSyntax CreateAutoProperty(PropertyDeclarationSyntax property, EqualsValueClauseSyntax initializer)
        {
            AccessorListSyntax accessorList = property.AccessorList;

            if (accessorList != null)
            {
                SyntaxList <AccessorDeclarationSyntax> newAccessors = accessorList
                                                                      .Accessors
                                                                      .Select(accessor =>
                {
                    accessor = accessor.Update(
                        attributeLists: accessor.AttributeLists,
                        modifiers: accessor.Modifiers,
                        keyword: accessor.Keyword,
                        body: default(BlockSyntax),
                        expressionBody: default(ArrowExpressionClauseSyntax),
                        semicolonToken: SemicolonToken());

                    return(accessor.WithTriviaFrom(accessor));
                })
                                                                      .ToSyntaxList();

                accessorList = accessorList.WithAccessors(newAccessors);
            }
            else
            {
                accessorList = AccessorList(AutoGetAccessorDeclaration())
                               .WithTriviaFrom(property.ExpressionBody);
            }

            if (accessorList
                .DescendantTrivia()
                .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                accessorList = accessorList.RemoveWhitespace();
            }

            PropertyDeclarationSyntax newProperty = property.Update(
                attributeLists: property.AttributeLists,
                modifiers: property.Modifiers,
                type: property.Type,
                explicitInterfaceSpecifier: property.ExplicitInterfaceSpecifier,
                identifier: property.Identifier.WithTrailingTrivia(Space),
                accessorList: accessorList,
                expressionBody: default(ArrowExpressionClauseSyntax),
                initializer: initializer,
                semicolonToken: (initializer != null) ? SemicolonToken() : default(SyntaxToken));

            return(newProperty
                   .WithTriviaFrom(property)
                   .WithFormatterAnnotation());
        }
        private static PropertyDeclarationSyntax ExpandProperty(PropertyDeclarationSyntax propertyDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(List(CreateAccessors(propertyDeclaration)));

            accessorList = accessorList
                           .RemoveWhitespace()
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(NewLine()));

            return(propertyDeclaration
                   .WithInitializer(null)
                   .WithSemicolonToken(default(SyntaxToken))
                   .WithAccessorList(accessorList));
        }
Beispiel #8
0
        private static AccessorListSyntax CreateAccessorList(ExpressionSyntax expression, SyntaxToken semicolon)
        {
            BlockSyntax block = CreateBlock(expression, semicolon);

            AccessorListSyntax accessorList = AccessorList(GetAccessorDeclaration(block));

            if (expression.IsSingleLine())
            {
                accessorList = accessorList
                               .RemoveWhitespace()
                               .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(NewLine()));
            }

            return(accessorList);
        }
Beispiel #9
0
        private static EventDeclarationSyntax ExpandEvent(EventFieldDeclarationSyntax eventDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(
                AddAccessorDeclaration(Block(OpenBraceToken(), List <StatementSyntax>(), CloseBraceToken().WithNavigationAnnotation())),
                RemoveAccessorDeclaration(Block()));

            accessorList = accessorList
                           .RemoveWhitespace()
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(NewLine()));

            VariableDeclaratorSyntax declarator = eventDeclaration.Declaration.Variables[0];

            return(EventDeclaration(
                       eventDeclaration.AttributeLists,
                       eventDeclaration.Modifiers,
                       eventDeclaration.Declaration.Type,
                       default(ExplicitInterfaceSpecifierSyntax),
                       declarator.Identifier,
                       accessorList));
        }