예제 #1
0
        private static Task <Document> DuplicateStatementAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            StatementContainer container;

            if (StatementContainer.TryCreate(statement, out container))
            {
                int index = container.Statements.IndexOf(statement);

                if (index == 0 &&
                    container.IsBlock &&
                    container.Block.OpenBraceToken.GetFullSpanEndLine() == statement.GetFullSpanStartLine())
                {
                    statement = statement.PrependToLeadingTrivia(CSharpFactory.NewLine());
                }

                SyntaxList <StatementSyntax> newStatements = container.Statements.Insert(index + 1, statement);

                SyntaxNode newNode = container.NodeWithStatements(newStatements);

                return(document.ReplaceNodeAsync(container.Node, newNode, cancellationToken));
            }
            else
            {
                SyntaxList <StatementSyntax> statements = SyntaxFactory.List(new StatementSyntax[] { statement, statement });

                BlockSyntax block = SyntaxFactory.Block(statements);

                return(document.ReplaceNodeAsync(statement, block, cancellationToken));
            }
        }
        private static Task <Document> DuplicateStatementAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            StatementsInfo statementsInfo = SyntaxInfo.StatementsInfo(statement);

            if (statementsInfo.Success)
            {
                int index = statementsInfo.Statements.IndexOf(statement);

                if (index == 0 &&
                    statementsInfo.IsInBlock &&
                    statementsInfo.Block.OpenBraceToken.GetFullSpanEndLine() == statement.GetFullSpanStartLine())
                {
                    statement = statement.PrependToLeadingTrivia(CSharpFactory.NewLine());
                }

                SyntaxList <StatementSyntax> newStatements = statementsInfo.Statements.Insert(index + 1, statement.WithNavigationAnnotation());

                StatementsInfo newInfo = statementsInfo.WithStatements(newStatements);

                return(document.ReplaceNodeAsync(statementsInfo.Node, newInfo.Node, cancellationToken));
            }
            else
            {
                SyntaxList <StatementSyntax> statements = SyntaxFactory.List(new StatementSyntax[] { statement, statement.WithNavigationAnnotation() });

                BlockSyntax block = SyntaxFactory.Block(statements);

                return(document.ReplaceNodeAsync(statement, block, cancellationToken));
            }
        }
예제 #3
0
        public static async Task <Document> RefactorAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken)
        {
            StatementSyntax newStatement = statement
                                           .WithLeadingTrivia(statement.GetLeadingTrivia().Insert(0, CSharpFactory.NewLine()))
                                           .WithFormatterAnnotation();

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

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

                    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));
            }
        }
예제 #4
0
        private static LambdaExpressionSyntax GetNewNode(
            LambdaExpressionSyntax lambda,
            ExpressionSyntax expression,
            ISymbol symbol)
        {
            BlockSyntax block = Block(GetStatement(expression, symbol));

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

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

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

            return(lambda);
        }
예제 #5
0
        private static Task <Document> RefactorAsync(
            Document document,
            SyntaxToken token,
            CancellationToken cancellationToken)
        {
            SyntaxToken newToken = token.AppendToTrailingTrivia(CSharpFactory.NewLine());

            return(document.ReplaceTokenAsync(token, newToken, cancellationToken));
        }
예제 #6
0
            public override SyntaxToken VisitToken(SyntaxToken token)
            {
                if (_separators.Contains(token) &&
                    !token.TrailingTrivia.Contains(SyntaxKind.EndOfLineTrivia))
                {
                    return(token.TrimTrailingTrivia().AppendToTrailingTrivia(CSharpFactory.NewLine()));
                }

                return(base.VisitToken(token));
            }
예제 #7
0
        private static Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken)
        {
            return(document.ReplaceNodeAsync(ifStatement, GetNewStatements(), cancellationToken));

            IEnumerable <StatementSyntax> GetNewStatements()
            {
                ElseClauseSyntax parentElse = null;

                foreach (IfStatementOrElseClause ifOrElse in ifStatement.AsCascade())
                {
                    if (ifOrElse.IsIf)
                    {
                        IfStatementSyntax newIfStatement = ifOrElse.AsIf();

                        ElseClauseSyntax elseClause = newIfStatement.Else;

                        newIfStatement = newIfStatement.WithElse(null);

                        if (parentElse != null)
                        {
                            newIfStatement = newIfStatement.PrependToLeadingTrivia(parentElse.GetLeadingTrivia());
                        }

                        if (elseClause != null)
                        {
                            newIfStatement = newIfStatement.AppendToTrailingTrivia(CSharpFactory.NewLine());
                        }

                        yield return(newIfStatement.WithFormatterAnnotation());

                        parentElse = ifStatement.Else;
                    }
                    else
                    {
                        StatementSyntax statement = ifOrElse.Statement;

                        if (statement is BlockSyntax block)
                        {
                            foreach (StatementSyntax newStatement in block.Statements.Select(f => f.WithFormatterAnnotation()))
                            {
                                yield return(newStatement);
                            }
                        }
                        else
                        {
                            yield return(statement);
                        }
                    }
                }
            }
        }
        public static Task <Document> RefactorAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken)
        {
            StatementSyntax newNode = statement
                                      .AppendToTrailingTrivia(CSharpFactory.NewLine())
                                      .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(statement, newNode, cancellationToken));
        }
        private static Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken)
        {
            var statements = new List <StatementSyntax>();

            IfStatementSyntax topIfStatement = ifStatement;

            ElseClauseSyntax elseClause = null;

            while (true)
            {
                IfStatementSyntax newIfStatement = ifStatement.WithElse(null);

                if (elseClause != null)
                {
                    newIfStatement = newIfStatement.PrependToLeadingTrivia(elseClause.GetLeadingTrivia());
                }

                newIfStatement = newIfStatement
                                 .AppendToTrailingTrivia(CSharpFactory.NewLine())
                                 .WithFormatterAnnotation();

                statements.Add(newIfStatement);

                elseClause = ifStatement.Else;

                StatementSyntax statement = elseClause.Statement;

                SyntaxKind kind = statement.Kind();

                if (kind != SyntaxKind.IfStatement)
                {
                    if (kind == SyntaxKind.Block)
                    {
                        statements.AddRange(((BlockSyntax)statement).Statements.Select(f => f.WithFormatterAnnotation()));
                    }
                    else
                    {
                        statements.Add(statement);
                    }

                    break;
                }

                ifStatement = ((IfStatementSyntax)statement);
            }

            return(document.ReplaceNodeAsync(topIfStatement, statements, cancellationToken));
        }
            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.NewLine())));
                    }
                }

                return(base.VisitToken(token));
            }
예제 #11
0
        private static Task <Document> AddEmptyLineBeforeWhileInDoStatementAsync(
            Document document,
            StatementSyntax statement,
            CancellationToken cancellationToken = default)
        {
            SyntaxTriviaList trailingTrivia = statement.GetTrailingTrivia();

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

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

            StatementSyntax newStatement = statement.WithTrailingTrivia(newTrailingTrivia);

            return(document.ReplaceNodeAsync(statement, newStatement, cancellationToken));
        }
예제 #12
0
        private static Task <Document> RemoveBracesAsync(
            Document document,
            BlockSyntax block,
            CancellationToken cancellationToken)
        {
            var switchSection = (SwitchSectionSyntax)block.Parent;

            SyntaxList <StatementSyntax> statements = block.Statements;

            SyntaxTriviaList leadingTrivia = block.OpenBraceToken.LeadingTrivia;

            leadingTrivia = AddTriviaIfNecessary(leadingTrivia, block.OpenBraceToken.TrailingTrivia);
            leadingTrivia = AddTriviaIfNecessary(leadingTrivia, statements[0].GetLeadingTrivia());

            SyntaxTriviaList trailingTrivia = statements.Last().GetTrailingTrivia();

            trailingTrivia = AddTriviaIfNecessary(trailingTrivia, block.CloseBraceToken.LeadingTrivia);
            trailingTrivia = AddTriviaIfNecessary(trailingTrivia, block.CloseBraceToken.TrailingTrivia);

            trailingTrivia = trailingTrivia.TrimEnd().Add(CSharpFactory.NewLine());

            var switchStatement = (SwitchStatementSyntax)switchSection.Parent;

            if (!switchStatement.Sections.IsLast(switchSection))
            {
                trailingTrivia = trailingTrivia.Add(CSharpFactory.NewLine());
            }

            SyntaxList <StatementSyntax> newStatements = statements.ReplaceAt(0, statements[0].WithLeadingTrivia(leadingTrivia));

            newStatements = newStatements.ReplaceAt(newStatements.Count - 1, newStatements.Last().WithTrailingTrivia(trailingTrivia));

            SwitchSectionSyntax newSwitchSection = switchSection
                                                   .WithStatements(newStatements)
                                                   .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(switchSection, newSwitchSection, cancellationToken));

            SyntaxTriviaList AddTriviaIfNecessary(SyntaxTriviaList trivia, SyntaxTriviaList triviaToAdd)
            {
                if (triviaToAdd.Any(f => f.IsKind(SyntaxKind.SingleLineCommentTrivia)))
                {
                    trivia = trivia.AddRange(triviaToAdd);
                }

                return(trivia);
            }
        }
예제 #13
0
        private static Task <Document> RefactorAsync(
            Document document,
            MemberDeclarationListSelection selectedMembers,
            CancellationToken cancellationToken)
        {
            SyntaxList <MemberDeclarationSyntax> members = selectedMembers.UnderlyingList;

            IEnumerable <MemberDeclarationSyntax> newMembers = members.ModifyRange(
                selectedMembers.FirstIndex,
                selectedMembers.Count - 1,
                member => member.AppendToTrailingTrivia(CSharpFactory.NewLine()));

            MemberDeclarationListInfo membersInfo = SyntaxInfo.MemberDeclarationListInfo(selectedMembers);

            return(document.ReplaceMembersAsync(membersInfo, newMembers, cancellationToken));
        }
예제 #14
0
        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.NewLine())
                .Insert(0, HeaderTrivia));

            root = root.ReplaceNode(compilationUnit, newNode);

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

            if (statement.IsParentKind(SyntaxKind.ElseClause))
            {
                list = new List <SyntaxTrivia>()
                {
                    CSharpFactory.NewLine()
                };
            }
            else
            {
                list = statement.Parent.GetLeadingTrivia()
                       .Reverse()
                       .SkipWhile(f => f.IsWhitespaceTrivia())
                       .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));
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            Diagnostic diagnostic = context.Diagnostics[0];

            SyntaxToken token = root.FindToken(context.Span.Start);

            if (token.IsKind(SyntaxKind.CommaToken))
            {
                CodeAction codeAction = CodeAction.Create(
                    Title,
                    cancellationToken =>
                {
                    SyntaxToken newToken = token.AppendToTrailingTrivia(CSharpFactory.NewLine());

                    return(context.Document.ReplaceTokenAsync(token, newToken, cancellationToken));
                },
                    base.GetEquivalenceKey(diagnostic));

                context.RegisterCodeFix(codeAction, diagnostic);
            }
            else
            {
                if (!TryFindFirstAncestorOrSelf(root, context.Span, out MemberDeclarationSyntax memberDeclaration))
                {
                    return;
                }

                CodeAction codeAction = CodeAction.Create(
                    Title,
                    cancellationToken =>
                {
                    MemberDeclarationSyntax newMemberDeclaration = memberDeclaration.AppendToTrailingTrivia(CSharpFactory.NewLine());

                    return(context.Document.ReplaceNodeAsync(memberDeclaration, newMemberDeclaration, cancellationToken));
                },
                    base.GetEquivalenceKey(diagnostic));

                context.RegisterCodeFix(codeAction, diagnostic);
            }
        }
        private static Task <Document> CopySwitchSectionAsync(
            Document document,
            SwitchSectionSyntax switchSection,
            bool insertNewLine,
            CancellationToken cancellationToken)
        {
            var switchStatement = (SwitchStatementSyntax)switchSection.Parent;

            SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

            int index = sections.IndexOf(switchSection);

            SwitchLabelSyntax label = switchSection.Labels[0];

            SyntaxToken firstToken = label.GetFirstToken();

            SyntaxToken newFirstToken = firstToken.WithNavigationAnnotation();

            if (insertNewLine &&
                !firstToken.LeadingTrivia.Contains(SyntaxKind.EndOfLineTrivia))
            {
                newFirstToken = newFirstToken.PrependToLeadingTrivia(CSharpFactory.NewLine());
            }

            SwitchSectionSyntax newSection = switchSection
                                             .WithLabels(switchSection.Labels.ReplaceAt(0, label.ReplaceToken(firstToken, newFirstToken)))
                                             .WithFormatterAnnotation();

            if (index == sections.Count - 1)
            {
                newSection = newSection.TrimTrailingTrivia();
            }

            SyntaxList <SwitchSectionSyntax> newSections = sections.Insert(index + 1, newSection);

            SwitchStatementSyntax newSwitchStatement = switchStatement.WithSections(newSections);

            return(document.ReplaceNodeAsync(switchStatement, newSwitchStatement, cancellationToken));
        }
        public static Task <Document> RefactorAsync(
            Document document,
            BlockSyntax block,
            CancellationToken cancellationToken)
        {
            BlockSyntax newBlock = block
                                   .WithOpenBraceToken(block.OpenBraceToken.WithoutTrailingTrivia())
                                   .WithCloseBraceToken(block.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLine()))
                                   .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(block, newBlock, cancellationToken));
        }
예제 #19
0
        private static 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.NewLine()));
            }

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

            return(document.ReplaceNodeAsync(block, newBlock, cancellationToken));
        }
        private static PropertyDeclarationSyntax ExpandProperty(PropertyDeclarationSyntax propertyDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(List(CreateAccessors(propertyDeclaration)));

            accessorList = accessorList
                           .RemoveWhitespaceOrEndOfLineTrivia()
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLine()));

            return(propertyDeclaration
                   .WithInitializer(null)
                   .WithoutSemicolonToken()
                   .WithAccessorList(accessorList));
        }
        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.NewLine())));
            }

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

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

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

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

            return(declaration);
        }
예제 #22
0
        public static Task <Document> RefactorAsync(
            Document document,
            MemberDeclarationSyntax memberDeclaration,
            CancellationToken cancellationToken)
        {
            MemberDeclarationSyntax newNode = memberDeclaration
                                              .WithTrailingTrivia(memberDeclaration.GetTrailingTrivia().Add(CSharpFactory.NewLine()));

            return(document.ReplaceNodeAsync(memberDeclaration, newNode, cancellationToken));
        }
예제 #23
0
        private static Task <Document> FormatSingleLineBlockAsync(
            Document document,
            BlockSyntax block,
            CancellationToken cancellationToken)
        {
            SyntaxToken closeBrace = block.CloseBraceToken;

            BlockSyntax newBlock = block
                                   .WithCloseBraceToken(closeBrace.WithLeadingTrivia(closeBrace.LeadingTrivia.Add(CSharpFactory.NewLine())))
                                   .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(block, newBlock, cancellationToken));
        }
예제 #24
0
        public static SyntaxTrivia GetEndOfLine(SyntaxToken token)
        {
            SyntaxTrivia trivia = FindEndOfLine(token);

            return((trivia.IsEndOfLineTrivia()) ? trivia : CSharpFactory.NewLine());
        }
예제 #25
0
        private static MemberDeclarationSyntax GetNewDeclaration(MemberDeclarationSyntax declaration)
        {
            switch (declaration)
            {
            case ClassDeclarationSyntax classDeclaration:
            {
                return(classDeclaration
                       .WithOpenBraceToken(classDeclaration.OpenBraceToken.WithoutTrailingTrivia())
                       .WithCloseBraceToken(classDeclaration.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLine())));
            }

            case StructDeclarationSyntax structDeclaration:
            {
                return(structDeclaration
                       .WithOpenBraceToken(structDeclaration.OpenBraceToken.WithoutTrailingTrivia())
                       .WithCloseBraceToken(structDeclaration.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLine())));
            }

            case InterfaceDeclarationSyntax interfaceDeclaration:
            {
                return(interfaceDeclaration
                       .WithOpenBraceToken(interfaceDeclaration.OpenBraceToken.WithoutTrailingTrivia())
                       .WithCloseBraceToken(interfaceDeclaration.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLine())));
            }
            }

            return(declaration);
        }