示例#1
0
        private static async Task ComputeRefactoringAsync(RefactoringContext context, LocalDeclarationStatementSyntax localDeclaration, ConditionalExpressionSyntax conditionalExpression)
        {
            if (localDeclaration?.IsParentKind(SyntaxKind.Block, SyntaxKind.SwitchSection) == true)
            {
                TypeSyntax type = localDeclaration.Declaration.Type;

                if (type != null)
                {
                    bool success = true;

                    if (type.IsVar)
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken);

                        success = typeSymbol?.SupportsExplicitDeclaration() == true;
                    }

                    if (success)
                    {
                        context.RegisterRefactoring(
                            "Replace ?: with if-else",
                            cancellationToken => RefactorAsync(context.Document, localDeclaration, conditionalExpression, cancellationToken));
                    }
                }
            }
        }
示例#2
0
        public static void ComputeRefactoring(RefactoringContext context, ConditionalExpressionSyntax expression)
        {
            SyntaxNode parent = expression.Parent;

            if (parent != null && context.Span.IsBetweenSpans(expression))
            {
                SyntaxKind kind = parent.Kind();

                if (kind == SyntaxKind.ReturnStatement)
                {
                    context.RegisterRefactoring(
                        Title,
                        cancellationToken => RefactorAsync(context.Document, expression, (ReturnStatementSyntax)parent, cancellationToken));
                }
                else if (kind == SyntaxKind.YieldReturnStatement)
                {
                    context.RegisterRefactoring(
                        Title,
                        cancellationToken => RefactorAsync(context.Document, expression, (YieldStatementSyntax)parent, cancellationToken));
                }
                else if (kind == SyntaxKind.SimpleAssignmentExpression)
                {
                    parent = parent.Parent;

                    if (parent?.IsKind(SyntaxKind.ExpressionStatement) == true)
                    {
                        context.RegisterRefactoring(
                            Title,
                            cancellationToken => RefactorAsync(context.Document, expression, (ExpressionStatementSyntax)parent, cancellationToken));
                    }
                }
                else
                {
                    LocalDeclarationStatementSyntax localDeclaration = GetLocalDeclaration(expression);

                    if (localDeclaration?.IsParentKind(SyntaxKind.Block, SyntaxKind.SwitchSection) == true)
                    {
                        context.RegisterRefactoring(
                            Title,
                            cancellationToken => RefactorAsync(context.Document, expression, localDeclaration, cancellationToken));
                    }
                }
            }
        }