예제 #1
0
        private static async Task <Document> FormatAccessorListAsync(
            Document document,
            AccessorListSyntax accessorList,
            CancellationToken cancellationToken)
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken);

            if (accessorList.Accessors.All(f => f.Body == null))
            {
                var propertyDeclaration = (PropertyDeclarationSyntax)accessorList.Parent;

                TextSpan span = TextSpan.FromBounds(
                    propertyDeclaration.Identifier.Span.End,
                    accessorList.CloseBraceToken.Span.Start);

                PropertyDeclarationSyntax newPropertyDeclaration =
                    WhitespaceOrEndOfLineRemover.RemoveFrom(propertyDeclaration, span);

                newPropertyDeclaration = newPropertyDeclaration
                                         .WithAdditionalAnnotations(Formatter.Annotation);

                root = root.ReplaceNode(propertyDeclaration, newPropertyDeclaration);
            }
            else
            {
                AccessorListSyntax newAccessorList = GetNewAccessorList(accessorList)
                                                     .WithAdditionalAnnotations(Formatter.Annotation);

                root = root.ReplaceNode(accessorList, newAccessorList);
            }

            return(document.WithSyntaxRoot(root));
        }
예제 #2
0
        private static PropertyDeclarationSyntax CreateAutoProperty(PropertyDeclarationSyntax property, EqualsValueClauseSyntax initializer)
        {
            AccessorListSyntax accessorList = CreateAccessorList(property);

            accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList);

            if (initializer != null)
            {
                accessorList = accessorList
                               .WithCloseBraceToken(accessorList.CloseBraceToken.WithoutTrailingTrivia());
            }

            PropertyDeclarationSyntax newProperty = property
                                                    .WithIdentifier(property.Identifier.WithTrailingSpace())
                                                    .WithExpressionBody(null)
                                                    .WithAccessorList(accessorList);

            if (initializer != null)
            {
                newProperty = newProperty
                              .WithInitializer(initializer)
                              .WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
            }
            else
            {
                newProperty = newProperty
                              .WithSemicolonToken(Token(SyntaxKind.None));
            }

            return(newProperty
                   .WithTriviaFrom(property)
                   .WithAdditionalAnnotations(Formatter.Annotation));
        }
예제 #3
0
        public static TNode RemoveWhitespaceOrEndOfLine <TNode>(TNode node, TextSpan span) where TNode : SyntaxNode
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            var remover = new WhitespaceOrEndOfLineRemover(span);

            return((TNode)remover.Visit(node));
        }
        private static ExpressionSyntax ProcessExpression(ExpressionSyntax expression)
        {
            if (expression
                .DescendantTrivia(expression.Span)
                .All(f => f.IsWhitespaceOrEndOfLine()))
            {
                expression = WhitespaceOrEndOfLineRemover.RemoveFrom(expression)
                             .WithAdditionalAnnotations(Formatter.Annotation);
            }

            return(expression);
        }
        private static async Task <Document> FormatAllParametersOnSingleLineAsync(
            Document document,
            ParameterListSyntax parameterList,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            ParameterListSyntax newParameterList = WhitespaceOrEndOfLineRemover.RemoveFrom(parameterList)
                                                   .WithAdditionalAnnotations(Formatter.Annotation);

            SyntaxNode newRoot = oldRoot.ReplaceNode(parameterList, newParameterList);

            return(document.WithSyntaxRoot(newRoot));
        }
예제 #6
0
            public override SyntaxNode VisitAccessorDeclaration(AccessorDeclarationSyntax node)
            {
                if (node == null)
                {
                    throw new ArgumentNullException(nameof(node));
                }

                if (AccessorListDiagnosticAnalyzer.ShouldBeFormatted(node))
                {
                    return(WhitespaceOrEndOfLineRemover.RemoveFrom(node, node.Span));
                }

                return(base.VisitAccessorDeclaration(node));
            }
        private static async Task <Document> FormatExpressionChainOnSingleLineAsync(
            Document document,
            MemberAccessExpressionSyntax expression,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            SyntaxNode newNode = WhitespaceOrEndOfLineRemover.RemoveFrom(expression)
                                 .WithAdditionalAnnotations(Formatter.Annotation);

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

            return(document.WithSyntaxRoot(newRoot));
        }
예제 #8
0
        private static AccessorListSyntax GetNewAccessorList(AccessorListSyntax accessorList)
        {
            if (accessorList.IsSingleline(includeExteriorTrivia: false))
            {
                SyntaxTriviaList triviaList = accessorList.CloseBraceToken.LeadingTrivia
                                              .Add(SyntaxHelper.NewLine);

                return(WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList)
                       .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(triviaList)));
            }
            else
            {
                return(AccessorSyntaxRewriter.VisitNode(accessorList));
            }
        }
예제 #9
0
        private static AccessorListSyntax CreateAccessorList(BlockSyntax block, bool singleline)
        {
            AccessorListSyntax accessorList =
                AccessorList(
                    SingletonList(
                        AccessorDeclaration(
                            SyntaxKind.GetAccessorDeclaration,
                            block)));

            if (singleline)
            {
                accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList);
            }

            return(accessorList);
        }
예제 #10
0
        private static PropertyDeclarationSyntax ExpandProperty(PropertyDeclarationSyntax propertyDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(
                List(propertyDeclaration
                     .AccessorList
                     .Accessors.Select(accessor => accessor
                                       .WithBody(Block())
                                       .WithSemicolonToken(Token(SyntaxKind.None)))));

            accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList)
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(SyntaxHelper.NewLine));

            return(propertyDeclaration
                   .WithInitializer(null)
                   .WithAccessorList(accessorList));
        }
        private static AccessorListSyntax CreateAccessorList(BlockSyntax block, bool singleline)
        {
            AccessorListSyntax accessorList =
                AccessorList(
                    SingletonList(
                        AccessorDeclaration(
                            SyntaxKind.GetAccessorDeclaration,
                            block)));

            if (singleline)
            {
                accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList)
                               .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(SyntaxHelper.NewLine));
            }

            return(accessorList);
        }
예제 #12
0
        private static PropertyDeclarationSyntax ExpandPropertyAndAddBackingField(PropertyDeclarationSyntax propertyDeclaration, string name)
        {
            AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

            if (getter != null)
            {
                AccessorDeclarationSyntax newGetter = getter
                                                      .WithBody(
                    Block(
                        SingletonList <StatementSyntax>(
                            ReturnStatement(IdentifierName(name)))))
                                                      .WithSemicolonToken(Token(SyntaxKind.None));

                propertyDeclaration = propertyDeclaration
                                      .WithInitializer(null)
                                      .WithAccessorList(
                    propertyDeclaration.AccessorList.ReplaceNode(getter, newGetter));
            }

            AccessorDeclarationSyntax setter = propertyDeclaration.Setter();

            if (setter != null)
            {
                AccessorDeclarationSyntax newSetter = setter
                                                      .WithBody(
                    Block(
                        SingletonList <StatementSyntax>(
                            ExpressionStatement(
                                AssignmentExpression(
                                    SyntaxKind.SimpleAssignmentExpression,
                                    IdentifierName(name),
                                    IdentifierName("value"))))))
                                                      .WithSemicolonToken(Token(SyntaxKind.None));

                propertyDeclaration = propertyDeclaration
                                      .WithAccessorList(
                    propertyDeclaration.AccessorList.ReplaceNode(setter, newSetter));
            }

            AccessorListSyntax accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(propertyDeclaration.AccessorList)
                                              .WithCloseBraceToken(propertyDeclaration.AccessorList.CloseBraceToken.WithLeadingTrivia(SyntaxHelper.NewLine));

            return(propertyDeclaration
                   .WithAccessorList(accessorList));
        }
        private static EventDeclarationSyntax Expand(EventFieldDeclarationSyntax eventDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(
                List(new AccessorDeclarationSyntax[]
            {
                AccessorDeclaration(SyntaxKind.AddAccessorDeclaration, Block()),
                AccessorDeclaration(SyntaxKind.RemoveAccessorDeclaration, Block()),
            }));

            accessorList = WhitespaceOrEndOfLineRemover.RemoveFrom(accessorList)
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(SyntaxHelper.NewLine));

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

            return(EventDeclaration(
                       eventDeclaration.AttributeLists,
                       eventDeclaration.Modifiers,
                       eventDeclaration.Declaration.Type,
                       null,
                       declarator.Identifier,
                       accessorList));
        }
예제 #14
0
 public static TNode RemoveWhitespaceOrEndOfLine <TNode>(TNode node, TextSpan span) where TNode : SyntaxNode
 {
     return(WhitespaceOrEndOfLineRemover.RemoveWhitespaceOrEndOfLine(node, span));
 }