public static void ComputeRefactoring(RefactoringContext context, SelectedStatementsInfo info)
        {
            if (info.IsSingleSelected)
            {
                StatementSyntax[] statements = info.SelectedNodes().ToArray();

                StatementSyntax first = statements[0];

                if (first.IsKind(SyntaxKind.IfStatement))
                {
                    var ifStatement = (IfStatementSyntax)first;

                    if (IfElseAnalysis.IsTopmostIf(ifStatement) &&
                        CanBeReplacedWithSwitch(ifStatement))
                    {
                        string title = (IfElseAnalysis.IsIsolatedIf(ifStatement))
                            ? "Replace if with switch"
                            : "Replace if-else with switch";

                        context.RegisterRefactoring(
                            title,
                            cancellationToken =>
                        {
                            return(RefactorAsync(
                                       context.Document,
                                       ifStatement,
                                       cancellationToken));
                        });
                    }
                }
            }
        }
Пример #2
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SelectedStatementsInfo info)
        {
            if (info.SelectedCount > 1)
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (AreLocalDeclarations(info.SelectedNodes(), semanticModel, context.CancellationToken))
                {
                    context.RegisterRefactoring(
                        "Merge local declarations",
                        cancellationToken =>
                    {
                        return(RefactorAsync(
                                   context.Document,
                                   info.Container,
                                   info.SelectedNodes().Cast <LocalDeclarationStatementSyntax>().ToArray(),
                                   cancellationToken));
                    });
                }
            }
        }
Пример #3
0
        public static void ComputeRefactorings(RefactoringContext context, SelectedStatementsInfo info)
        {
            List <IfStatementSyntax> ifStatements = GetIfStatements(info.SelectedNodes());

            if (ifStatements?.Count > 1)
            {
                context.RegisterRefactoring(
                    "Merge if statements",
                    cancellationToken =>
                {
                    return(RefactorAsync(
                               context.Document,
                               info.Container,
                               ifStatements.ToImmutableArray(),
                               cancellationToken));
                });
            }
        }
Пример #4
0
        public async Task <Document> RefactorAsync(
            Document document,
            SelectedStatementsInfo info,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            StatementContainer container = info.Container;

            StatementSyntax[] statements = info.SelectedNodes().ToArray();

            int index = info.FirstSelectedNodeIndex;

            SyntaxTriviaList leadingTrivia  = statements[0].GetLeadingTrivia();
            SyntaxTriviaList trailingTrivia = statements[statements.Length - 1].GetTrailingTrivia();

            statements[0] = statements[0].WithLeadingTrivia();
            statements[statements.Length - 1] = statements[statements.Length - 1].WithTrailingTrivia();

            SyntaxList <StatementSyntax> newStatements = container.Statements;

            int cnt = statements.Length;

            while (cnt > 0)
            {
                newStatements = newStatements.RemoveAt(index);
                cnt--;
            }

            TStatement statement = CreateStatement(statements.ToImmutableArray());

            statement = statement
                        .WithLeadingTrivia(leadingTrivia)
                        .WithTrailingTrivia(trailingTrivia)
                        .WithFormatterAnnotation();

            newStatements = newStatements.Insert(index, statement);

            root = root.ReplaceNode(container.Node, container.NodeWithStatements(newStatements));

            return(document.WithSyntaxRoot(root));
        }
Пример #5
0
        public static void ComputeRefactorings(RefactoringContext context, SelectedStatementsInfo info)
        {
            using (IEnumerator <StatementSyntax> en = info.SelectedNodes().GetEnumerator())
            {
                if (en.MoveNext() &&
                    en.Current.IsKind(SyntaxKind.ExpressionStatement))
                {
                    var statement = (ExpressionStatementSyntax)en.Current;

                    if (statement.Expression?.IsKind(SyntaxKind.SimpleAssignmentExpression) == true &&
                        en.MoveNext() &&
                        en.Current.IsKind(SyntaxKind.ReturnStatement))
                    {
                        var returnStatement = (ReturnStatementSyntax)en.Current;

                        if (returnStatement.Expression != null &&
                            !en.MoveNext())
                        {
                            var assignment = (AssignmentExpressionSyntax)statement.Expression;

                            if (assignment.Left?.IsMissing == false &&
                                assignment.Right?.IsMissing == false &&
                                assignment.Left.IsEquivalentTo(returnStatement.Expression, topLevel: false))
                            {
                                context.RegisterRefactoring(
                                    "Merge statements",
                                    cancellationToken =>
                                {
                                    return(RefactorAsync(
                                               context.Document,
                                               statement,
                                               returnStatement,
                                               cancellationToken));
                                });
                            }
                        }
                    }
                }
            }
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            ObjectCreationExpressionSyntax objectCreation,
            SelectedStatementsInfo info,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            StatementContainer container = info.Container;

            ExpressionStatementSyntax[] expressionStatements = info
                                                               .SelectedNodes()
                                                               .Skip(1)
                                                               .Cast <ExpressionStatementSyntax>()
                                                               .ToArray();

            StatementSyntax firstNode = info.FirstSelectedNode;

            SyntaxList <StatementSyntax> newStatements = container.Statements.Replace(
                firstNode,
                firstNode.ReplaceNode(
                    objectCreation,
                    objectCreation.WithInitializer(CreateInitializer(objectCreation, expressionStatements))));

            int count = expressionStatements.Length;
            int index = info.FirstSelectedNodeIndex + 1;

            while (count > 0)
            {
                newStatements = newStatements.RemoveAt(index);
                count--;
            }

            SyntaxNode newRoot = root.ReplaceNode(
                container.Node,
                container.NodeWithStatements(newStatements));

            return(document.WithSyntaxRoot(newRoot));
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SelectedStatementsInfo info)
        {
            StatementSyntax firstStatement = info.FirstSelectedNode;
            SemanticModel   semanticModel  = null;
            ISymbol         symbol         = null;
            ObjectCreationExpressionSyntax objectCreation = null;

            if (info.AreManySelected)
            {
                SyntaxKind kind = firstStatement.Kind();

                if (kind == SyntaxKind.LocalDeclarationStatement)
                {
                    var localDeclaration = (LocalDeclarationStatementSyntax)firstStatement;

                    VariableDeclarationSyntax declaration = localDeclaration.Declaration;

                    if (declaration != null)
                    {
                        SeparatedSyntaxList <VariableDeclaratorSyntax> variables = declaration.Variables;

                        if (variables.Count == 1)
                        {
                            VariableDeclaratorSyntax variable = variables[0];

                            objectCreation = variable.Initializer?.Value as ObjectCreationExpressionSyntax;

                            if (objectCreation != null)
                            {
                                semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                                symbol = semanticModel.GetDeclaredSymbol(variable, context.CancellationToken);
                            }
                        }
                    }
                }
                else if (kind == SyntaxKind.ExpressionStatement)
                {
                    var expressionStatement = (ExpressionStatementSyntax)firstStatement;

                    var assignment = expressionStatement.Expression as AssignmentExpressionSyntax;

                    if (assignment != null)
                    {
                        objectCreation = assignment.Right as ObjectCreationExpressionSyntax;

                        if (objectCreation != null)
                        {
                            semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                            symbol = semanticModel.GetSymbolInfo(assignment.Left, context.CancellationToken).Symbol;
                        }
                    }
                }

                if (objectCreation != null &&
                    symbol?.IsErrorType() == false &&
                    info
                    .SelectedNodes()
                    .Skip(1)
                    .All(f => IsValidAssignmentStatement(f, symbol, semanticModel, context.CancellationToken)))
                {
                    context.RegisterRefactoring(
                        "Collapse to initializer",
                        cancellationToken =>
                    {
                        return(RefactorAsync(
                                   context.Document,
                                   objectCreation,
                                   info,
                                   cancellationToken));
                    });
                }
            }
        }