Пример #1
0
        private static ConditionalExpressionSyntax CreateMultilineConditionalExpression(ConditionalExpressionSyntax conditionalExpression)
        {
            SyntaxTriviaList triviaList = SyntaxUtility.GetIndentTrivia(conditionalExpression.Parent).Add(CSharpFactory.IndentTrivia());

            triviaList = triviaList.Insert(0, CSharpFactory.NewLineTrivia());

            ParenthesizedExpressionSyntax condition = null;

            if (conditionalExpression.Condition.IsKind(SyntaxKind.ParenthesizedExpression))
            {
                condition = (ParenthesizedExpressionSyntax)conditionalExpression.Condition;
            }
            else
            {
                condition = ParenthesizedExpression(conditionalExpression.Condition.WithoutTrailingTrivia())
                            .WithCloseParenToken(CreateTokenWithTrailingNewLine(SyntaxKind.CloseParenToken));
            }

            return(ConditionalExpression(
                       condition.WithoutTrailingTrivia(),
                       conditionalExpression.WhenTrue.WithoutTrailingTrivia(),
                       conditionalExpression.WhenFalse.WithoutTrailingTrivia())
                   .WithQuestionToken(CreateToken(SyntaxKind.QuestionToken, triviaList))
                   .WithColonToken(CreateToken(SyntaxKind.ColonToken, triviaList)));
        }
Пример #2
0
 private static SyntaxToken CreateTokenWithTrailingNewLine(SyntaxKind kind)
 {
     return(Token(
                TriviaList(),
                kind,
                TriviaList(CSharpFactory.NewLineTrivia())));
 }
        private static InitializerExpressionSyntax GetMultilineInitializer(InitializerExpressionSyntax initializer)
        {
            SyntaxNode parent = initializer.Parent;

            if (parent.IsKind(SyntaxKind.ObjectCreationExpression) &&
                !initializer.IsKind(SyntaxKind.CollectionInitializerExpression))
            {
                return(initializer
                       .WithExpressions(
                           SeparatedList(
                               initializer.Expressions.Select(expression => expression.WithLeadingTrivia(CSharpFactory.NewLineTrivia()))))
                       .WithFormatterAnnotation());
            }

            SyntaxTriviaList indent  = SyntaxUtility.GetIndentTrivia(initializer);
            SyntaxTriviaList indent2 = indent.Add(CSharpFactory.IndentTrivia());

            indent  = indent.Insert(0, CSharpFactory.NewLineTrivia());
            indent2 = indent2.Insert(0, CSharpFactory.NewLineTrivia());

            return(initializer
                   .WithExpressions(
                       SeparatedList(
                           initializer.Expressions.Select(expression => expression.WithLeadingTrivia(indent2))))
                   .WithOpenBraceToken(initializer.OpenBraceToken.WithLeadingTrivia(indent))
                   .WithCloseBraceToken(initializer.CloseBraceToken.WithLeadingTrivia(indent))
                   .WithFormatterAnnotation());
        }
        private static LambdaExpressionSyntax GetNewNode(
            LambdaExpressionSyntax lambda,
            ExpressionSyntax expression,
            ISymbol symbol)
        {
            BlockSyntax block = Block(GetStatement(expression, symbol));

            block = block
                    .WithCloseBraceToken(
                block.CloseBraceToken
                .WithLeadingTrivia(TriviaList(CSharpFactory.NewLineTrivia())));

            switch (lambda.Kind())
            {
            case SyntaxKind.SimpleLambdaExpression:
            {
                return(((SimpleLambdaExpressionSyntax)lambda)
                       .WithBody(block));
            }

            case SyntaxKind.ParenthesizedLambdaExpression:
            {
                return(((ParenthesizedLambdaExpressionSyntax)lambda)
                       .WithBody(block));
            }
            }

            return(lambda);
        }
Пример #5
0
        public static async Task <Document> RefactorAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken)
        {
            StatementSyntax newStatement = statement
                                           .WithLeadingTrivia(statement.GetLeadingTrivia().Insert(0, CSharpFactory.NewLineTrivia()))
                                           .WithFormatterAnnotation();

            if (statement.IsParentKind(SyntaxKind.Block))
            {
                var block = (BlockSyntax)statement.Parent;

                if (block.IsSingleLine(includeExteriorTrivia: false))
                {
                    SyntaxTriviaList triviaList = block.CloseBraceToken.LeadingTrivia
                                                  .Add(CSharpFactory.NewLineTrivia());

                    BlockSyntax newBlock = block
                                           .WithCloseBraceToken(block.CloseBraceToken.WithLeadingTrivia(triviaList))
                                           .WithStatements(block.Statements.Replace(statement, newStatement))
                                           .WithFormatterAnnotation();

                    return(await document.ReplaceNodeAsync(block, newBlock, cancellationToken).ConfigureAwait(false));
                }
                else
                {
                    return(await document.ReplaceNodeAsync(statement, newStatement, cancellationToken).ConfigureAwait(false));
                }
            }
            else
            {
                return(await document.ReplaceNodeAsync(statement, newStatement, cancellationToken).ConfigureAwait(false));
            }
        }
        private static Task <Document> RefactorAsync(
            Document document,
            SyntaxToken token,
            CancellationToken cancellationToken)
        {
            SyntaxToken newToken = token.AppendToTrailingTrivia(CSharpFactory.NewLineTrivia());

            return(document.ReplaceTokenAsync(token, newToken, cancellationToken));
        }
Пример #7
0
        public static async Task <Document> RefactorAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken)
        {
            StatementSyntax newNode = statement
                                      .AppendToTrailingTrivia(CSharpFactory.NewLineTrivia())
                                      .WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(statement, newNode, cancellationToken).ConfigureAwait(false));
        }
            public override SyntaxToken VisitToken(SyntaxToken token)
            {
                if (_separators.Contains(token))
                {
                    SyntaxTriviaList triviaList = token.TrailingTrivia;

                    if (!triviaList.Contains(SyntaxKind.EndOfLineTrivia))
                    {
                        return(token.WithTrailingTrivia(triviaList.TrimEnd().Add(CSharpFactory.NewLineTrivia())));
                    }
                }

                return(base.VisitToken(token));
            }
Пример #9
0
        private static AccessorListSyntax GetNewAccessorList(AccessorListSyntax accessorList)
        {
            if (accessorList.IsSingleLine(includeExteriorTrivia: false))
            {
                SyntaxTriviaList triviaList = accessorList.CloseBraceToken.LeadingTrivia
                                              .Add(CSharpFactory.NewLineTrivia());

                return(Remover.RemoveWhitespaceOrEndOfLine(accessorList)
                       .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(triviaList)));
            }
            else
            {
                return(AccessorSyntaxRewriter.VisitNode(accessorList));
            }
        }
Пример #10
0
        public static async Task <Document> RefactorAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxTriviaList trailingTrivia = statement.GetTrailingTrivia();

            int index = trailingTrivia.IndexOf(SyntaxKind.EndOfLineTrivia);

            SyntaxTriviaList newTrailingTrivia = trailingTrivia.Insert(index, CSharpFactory.NewLineTrivia());

            StatementSyntax newStatement = statement.WithTrailingTrivia(newTrailingTrivia);

            return(await document.ReplaceNodeAsync(statement, newStatement, cancellationToken).ConfigureAwait(false));
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            CompilationUnitSyntax compilationUnit,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            CompilationUnitSyntax newNode = compilationUnit
                                            .WithLeadingTrivia(
                compilationUnit
                .GetLeadingTrivia()
                .Insert(0, CSharpFactory.NewLineTrivia())
                .Insert(0, HeaderTrivia));

            root = root.ReplaceNode(compilationUnit, newNode);

            return(document.WithSyntaxRoot(root));
        }
        private static IEnumerable <StatementSyntax> GetNewNodes(StatementSyntax statement)
        {
            List <SyntaxTrivia> list = null;

            if (statement.Parent.IsKind(SyntaxKind.ElseClause))
            {
                list = new List <SyntaxTrivia>()
                {
                    CSharpFactory.NewLineTrivia()
                };
            }
            else
            {
                list = statement.Parent.GetLeadingTrivia()
                       .Reverse()
                       .SkipWhile(f => f.IsKind(SyntaxKind.WhitespaceTrivia))
                       .Reverse()
                       .ToList();
            }

            if (statement.IsKind(SyntaxKind.Block))
            {
                SyntaxList <StatementSyntax> .Enumerator en = ((BlockSyntax)statement).Statements.GetEnumerator();

                if (en.MoveNext())
                {
                    list.AddRange(en.Current.GetLeadingTrivia());

                    yield return(en.Current.WithLeadingTrivia(list));

                    while (en.MoveNext())
                    {
                        yield return(en.Current);
                    }
                }
            }
            else
            {
                list.AddRange(statement.GetLeadingTrivia());

                yield return(statement.WithLeadingTrivia(list));
            }
        }
        private static async Task <Document> FormatOnMultipleLinesAsync(
            Document document,
            BinaryExpressionSyntax condition,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            SyntaxTriviaList triviaList = SyntaxFactory.TriviaList(CSharpFactory.NewLineTrivia())
                                          .AddRange(SyntaxUtility.GetIndentTrivia(condition))
                                          .Add(CSharpFactory.IndentTrivia());

            var rewriter = new BinaryExpressioneSyntaxRewriter(triviaList);

            var newCondition = (ExpressionSyntax)rewriter.Visit(condition);

            root = root.ReplaceNode(condition, newCondition);

            return(document.WithSyntaxRoot(root));
        }
        private static async Task <Document> AddEmptyLineAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            SyntaxTriviaList trailingTrivia = statement.GetTrailingTrivia();

            int index = trailingTrivia.IndexOf(SyntaxKind.EndOfLineTrivia);

            SyntaxTriviaList newTrailingTrivia = trailingTrivia.Insert(index, CSharpFactory.NewLineTrivia());

            StatementSyntax newStatement = statement.WithTrailingTrivia(newTrailingTrivia);

            SyntaxNode newRoot = root.ReplaceNode(statement, newStatement);

            return(document.WithSyntaxRoot(newRoot));
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            AccessorDeclarationSyntax accessor,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            SyntaxToken closeBrace = accessor.Body.CloseBraceToken;

            AccessorDeclarationSyntax newAccessor = accessor
                                                    .WithBody(
                accessor.Body.WithCloseBraceToken(
                    closeBrace.WithLeadingTrivia(
                        closeBrace.LeadingTrivia.Add(CSharpFactory.NewLineTrivia()))))
                                                    .WithFormatterAnnotation();

            root = root.ReplaceNode(accessor, newAccessor);

            return(document.WithSyntaxRoot(root));
        }
        private static async Task <Document> FormatExpressionChainOnMultipleLinesAsync(
            Document document,
            ImmutableArray <MemberAccessExpressionSyntax> expressions,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            MemberAccessExpressionSyntax expression = expressions[0];

            SyntaxTriviaList triviaList = SyntaxUtility.GetIndentTrivia(expression).Add(CSharpFactory.IndentTrivia());

            triviaList = triviaList.Insert(0, CSharpFactory.NewLineTrivia());

            var rewriter = new ExpressionChainSyntaxRewriter(expressions, triviaList);

            SyntaxNode newNode = rewriter.Visit(expression)
                                 .WithFormatterAnnotation();

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

            return(document.WithSyntaxRoot(newRoot));
        }
Пример #17
0
        private static EventDeclarationSyntax ExpandEvent(EventFieldDeclarationSyntax eventDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(
                List(new AccessorDeclarationSyntax[]
            {
                AccessorDeclaration(SyntaxKind.AddAccessorDeclaration, Block()),
                AccessorDeclaration(SyntaxKind.RemoveAccessorDeclaration, Block()),
            }));

            accessorList = SyntaxRemover.RemoveWhitespaceOrEndOfLine(accessorList)
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLineTrivia()));

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

            return(EventDeclaration(
                       eventDeclaration.AttributeLists,
                       eventDeclaration.Modifiers,
                       eventDeclaration.Declaration.Type,
                       null,
                       declarator.Identifier,
                       accessorList));
        }
        private static PropertyDeclarationSyntax ExpandProperty(PropertyDeclarationSyntax propertyDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(List(CreateAccessors(propertyDeclaration)));

            accessorList = Remover.RemoveWhitespaceOrEndOfLine(accessorList)
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLineTrivia()));

            return(propertyDeclaration
                   .WithoutInitializer()
                   .WithoutSemicolonToken()
                   .WithAccessorList(accessorList));
        }
        public static Task <Document> RefactorAsync(
            Document document,
            MemberDeclarationSyntax memberDeclaration,
            CancellationToken cancellationToken)
        {
            MemberDeclarationSyntax newNode = memberDeclaration
                                              .WithTrailingTrivia(memberDeclaration.GetTrailingTrivia().Add(CSharpFactory.NewLineTrivia()));

            return(document.ReplaceNodeAsync(memberDeclaration, newNode, cancellationToken));
        }
Пример #20
0
        private static async Task <Document> AddEmptyLineAsync(
            Document document,
            MemberDeclarationSyntax declaration,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            MemberDeclarationSyntax newNode = declaration
                                              .WithTrailingTrivia(declaration.GetTrailingTrivia().Add(CSharpFactory.NewLineTrivia()));

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

            return(document.WithSyntaxRoot(newRoot));
        }
        private static MemberDeclarationSyntax GetNewDeclaration(MemberDeclarationSyntax declaration)
        {
            switch (declaration.Kind())
            {
            case SyntaxKind.ClassDeclaration:
            {
                var classDeclaration = (ClassDeclarationSyntax)declaration;

                return(classDeclaration
                       .WithOpenBraceToken(classDeclaration.OpenBraceToken.WithoutTrailingTrivia())
                       .WithCloseBraceToken(classDeclaration.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLineTrivia())));
            }

            case SyntaxKind.StructDeclaration:
            {
                var structDeclaration = (StructDeclarationSyntax)declaration;

                return(structDeclaration
                       .WithOpenBraceToken(structDeclaration.OpenBraceToken.WithoutTrailingTrivia())
                       .WithCloseBraceToken(structDeclaration.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLineTrivia())));
            }

            case SyntaxKind.InterfaceDeclaration:
            {
                var interfaceDeclaration = (InterfaceDeclarationSyntax)declaration;

                return(interfaceDeclaration
                       .WithOpenBraceToken(interfaceDeclaration.OpenBraceToken.WithoutTrailingTrivia())
                       .WithCloseBraceToken(interfaceDeclaration.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLineTrivia())));
            }
            }

            return(declaration);
        }
Пример #22
0
        private static SyntaxNode Refactor(MemberDeclarationSyntax member)
        {
            switch (member.Parent.Kind())
            {
            case SyntaxKind.CompilationUnit:
            {
                var parent = (CompilationUnitSyntax)member.Parent;
                int index  = parent.Members.IndexOf(member);

                return(parent.WithMembers(parent.Members.Insert(index + 1, member)));
            }

            case SyntaxKind.NamespaceDeclaration:
            {
                var parent = (NamespaceDeclarationSyntax)member.Parent;
                int index  = parent.Members.IndexOf(member);

                if (index == 0 &&
                    parent.OpenBraceToken.GetFullSpanEndLine() == member.GetFullSpanStartLine())
                {
                    member = member.WithLeadingTrivia(member.GetLeadingTrivia().Insert(0, CSharpFactory.NewLineTrivia()));
                }

                return(parent.WithMembers(parent.Members.Insert(index + 1, member)));
            }

            case SyntaxKind.ClassDeclaration:
            {
                var parent = (ClassDeclarationSyntax)member.Parent;
                int index  = parent.Members.IndexOf(member);

                if (index == 0 &&
                    parent.OpenBraceToken.GetFullSpanEndLine() == member.GetFullSpanStartLine())
                {
                    member = member.WithLeadingTrivia(member.GetLeadingTrivia().Insert(0, CSharpFactory.NewLineTrivia()));
                }

                return(parent.WithMembers(parent.Members.Insert(index + 1, member)));
            }

            case SyntaxKind.StructDeclaration:
            {
                var parent = (StructDeclarationSyntax)member.Parent;
                int index  = parent.Members.IndexOf(member);

                if (index == 0 &&
                    parent.OpenBraceToken.GetFullSpanEndLine() == member.GetFullSpanStartLine())
                {
                    member = member.WithLeadingTrivia(member.GetLeadingTrivia().Insert(0, CSharpFactory.NewLineTrivia()));
                }

                return(parent.WithMembers(parent.Members.Insert(index + 1, member)));
            }

            case SyntaxKind.InterfaceDeclaration:
            {
                var parent = (InterfaceDeclarationSyntax)member.Parent;
                int index  = parent.Members.IndexOf(member);

                if (index == 0 &&
                    parent.OpenBraceToken.GetFullSpanEndLine() == member.GetFullSpanStartLine())
                {
                    member = member.WithLeadingTrivia(member.GetLeadingTrivia().Insert(0, CSharpFactory.NewLineTrivia()));
                }

                return(parent.WithMembers(parent.Members.Insert(index + 1, member)));
            }
            }

            return(null);
        }
Пример #23
0
        private static async Task <Document> DuplicateStatementAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var block = (BlockSyntax)statement.Parent;

            int index = block.Statements.IndexOf(statement);

            if (index == 0 &&
                block.OpenBraceToken.GetFullSpanEndLine() == statement.GetFullSpanStartLine())
            {
                statement = statement.WithLeadingTrivia(statement.GetLeadingTrivia().Insert(0, CSharpFactory.NewLineTrivia()));
            }

            BlockSyntax newBlock = block.WithStatements(block.Statements.Insert(index + 1, statement));

            return(await document.ReplaceNodeAsync(block, newBlock, cancellationToken).ConfigureAwait(false));
        }
Пример #24
0
        private static PropertyDeclarationSyntax ExpandPropertyAndAddBackingField(PropertyDeclarationSyntax propertyDeclaration, string name)
        {
            AccessorDeclarationSyntax getter = propertyDeclaration.Getter();

            if (getter != null)
            {
                AccessorDeclarationSyntax newGetter = getter
                                                      .WithBody(Block(ReturnStatement(IdentifierName(name))))
                                                      .WithoutSemicolonToken();

                propertyDeclaration = propertyDeclaration
                                      .ReplaceNode(getter, newGetter)
                                      .WithoutInitializer()
                                      .WithoutSemicolonToken();
            }

            AccessorDeclarationSyntax setter = propertyDeclaration.Setter();

            if (setter != null)
            {
                AccessorDeclarationSyntax newSetter = setter
                                                      .WithBody(Block(
                                                                    ExpressionStatement(
                                                                        SimpleAssignmentExpression(
                                                                            IdentifierName(name),
                                                                            IdentifierName("value")))))
                                                      .WithoutSemicolonToken();

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

            AccessorListSyntax accessorList = SyntaxRemover.RemoveWhitespaceOrEndOfLine(propertyDeclaration.AccessorList)
                                              .WithCloseBraceToken(propertyDeclaration.AccessorList.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLineTrivia()));

            return(propertyDeclaration
                   .WithAccessorList(accessorList));
        }
        public static Task <Document> RefactorAsync(
            Document document,
            BlockSyntax block,
            CancellationToken cancellationToken)
        {
            BlockSyntax newBlock = block
                                   .WithOpenBraceToken(block.OpenBraceToken.WithoutTrailingTrivia())
                                   .WithCloseBraceToken(block.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLineTrivia()))
                                   .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(block, newBlock, cancellationToken));
        }
        private static async Task <Document> FormatBlockAsync(
            Document document,
            BlockSyntax block,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            BlockSyntax newBlock = block
                                   .WithOpenBraceToken(block.OpenBraceToken.WithoutTrailingTrivia())
                                   .WithCloseBraceToken(block.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLineTrivia()))
                                   .WithFormatterAnnotation();

            SyntaxNode newRoot = oldRoot.ReplaceNode(block, newBlock);

            return(document.WithSyntaxRoot(newRoot));
        }
Пример #27
0
        private static async Task <Document> AddEmptyLineAfterEmbeddedStatementAsync(
            Document document,
            StatementSyntax node,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            StatementSyntax newNode = node
                                      .WithTrailingTrivia(node.GetTrailingTrivia().Add(CSharpFactory.NewLineTrivia()))
                                      .WithFormatterAnnotation();

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

            return(document.WithSyntaxRoot(newRoot));
        }
Пример #28
0
        private static AccessorListSyntax CreateAccessorList(BlockSyntax block, bool singleline)
        {
            AccessorListSyntax accessorList =
                AccessorList(
                    SingletonList(
                        AccessorDeclaration(
                            SyntaxKind.GetAccessorDeclaration,
                            block)));

            if (singleline)
            {
                accessorList = SyntaxRemover.RemoveWhitespaceOrEndOfLine(accessorList)
                               .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLineTrivia()));
            }

            return(accessorList);
        }
Пример #29
0
 public static SyntaxToken WithTrailingNewLine(this SyntaxToken token)
 {
     return(token.WithTrailingTrivia(CSharpFactory.NewLineTrivia()));
 }