Exemplo n.º 1
0
        public static void ComputeRefactorings(RefactoringContext context, BinaryExpressionSyntax binaryExpression)
        {
            binaryExpression = GetBinaryExpression(binaryExpression, context.Span);

            if (binaryExpression != null &&
                IsFormattableKind(binaryExpression.Kind()))
            {
                string title = "Format binary expression";

                if (binaryExpression.IsSingleLine())
                {
                    title += " on multiple lines";

                    context.RegisterRefactoring(
                        title,
                        cancellationToken => CSharpFormatter.ToMultiLineAsync(context.Document, binaryExpression, cancellationToken));
                }
                else
                {
                    title += " on a single line";

                    context.RegisterRefactoring(
                        title,
                        cancellationToken => CSharpFormatter.ToSingleLineAsync(context.Document, binaryExpression, cancellationToken));
                }
            }
        }
        public static void ComputeRefactorings(RefactoringContext context, AccessorDeclarationSyntax accessor)
        {
            BlockSyntax body = accessor.Body;

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatAccessorBraces) &&
                body?.Span.Contains(context.Span) == true &&
                !body.OpenBraceToken.IsMissing &&
                !body.CloseBraceToken.IsMissing)
            {
                if (body.IsSingleLine())
                {
                    context.RegisterRefactoring(
                        "Format braces on multiple lines",
                        cancellationToken => CSharpFormatter.ToMultiLineAsync(context.Document, accessor, cancellationToken));
                }
                else
                {
                    SyntaxList <StatementSyntax> statements = body.Statements;

                    if (statements.Count == 1 &&
                        statements[0].IsSingleLine())
                    {
                        context.RegisterRefactoring(
                            "Format braces on a single line",
                            cancellationToken => CSharpFormatter.ToSingleLineAsync(context.Document, accessor, cancellationToken));
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(accessor) &&
                context.SupportsCSharp6 &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(accessor))
            {
                SyntaxNode node = accessor;

                var accessorList = accessor.Parent as AccessorListSyntax;

                if (accessorList != null)
                {
                    SyntaxList <AccessorDeclarationSyntax> accessors = accessorList.Accessors;

                    if (accessors.Count == 1 &&
                        accessors.First().IsKind(SyntaxKind.GetAccessorDeclaration))
                    {
                        var parent = accessorList.Parent as MemberDeclarationSyntax;

                        if (parent != null)
                        {
                            node = parent;
                        }
                    }
                }

                context.RegisterRefactoring(
                    "Use expression-bodied member",
                    cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, node, cancellationToken));
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ConditionalExpressionSyntax conditionalExpression)
        {
            if (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(conditionalExpression))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatConditionalExpression))
                {
                    if (conditionalExpression.IsSingleLine())
                    {
                        context.RegisterRefactoring(
                            "Format ?: on separate lines",
                            cancellationToken =>
                        {
                            return(CSharpFormatter.ToMultiLineAsync(
                                       context.Document,
                                       conditionalExpression,
                                       cancellationToken));
                        });
                    }
                    else
                    {
                        context.RegisterRefactoring(
                            "Format ?: on a single line",
                            cancellationToken =>
                        {
                            return(CSharpFormatter.ToSingleLineAsync(
                                       context.Document,
                                       conditionalExpression,
                                       cancellationToken));
                        });
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseIfElseInsteadOfConditionalExpression))
                {
                    await UseIfElseInsteadOfConditionalExpressionRefactoring.ComputeRefactoringAsync(context, conditionalExpression).ConfigureAwait(false);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapExpressionsInConditionalExpression) &&
                (context.Span.IsBetweenSpans(conditionalExpression) ||
                 context.Span.IsEmptyAndContainedInSpan(conditionalExpression.QuestionToken) ||
                 context.Span.IsEmptyAndContainedInSpan(conditionalExpression.ColonToken)) &&
                SwapExpressionsInConditionalExpressionRefactoring.CanRefactor(conditionalExpression))
            {
                context.RegisterRefactoring(
                    "Swap expressions in ?:",
                    cancellationToken =>
                {
                    return(SwapExpressionsInConditionalExpressionRefactoring.RefactorAsync(
                               context.Document,
                               conditionalExpression,
                               cancellationToken));
                });
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, InitializerExpressionSyntax initializer)
        {
            if (initializer.IsKind(SyntaxKind.ComplexElementInitializerExpression) &&
                initializer.IsParentKind(SyntaxKind.CollectionInitializerExpression))
            {
                initializer = (InitializerExpressionSyntax)initializer.Parent;
            }

            if (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(initializer) ||
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(initializer.Expressions))
            {
                SeparatedSyntaxList <ExpressionSyntax> expressions = initializer.Expressions;

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatInitializer) &&
                    expressions.Any() &&
                    !initializer.IsKind(SyntaxKind.ComplexElementInitializerExpression) &&
                    initializer.IsParentKind(
                        SyntaxKind.ArrayCreationExpression,
                        SyntaxKind.ImplicitArrayCreationExpression,
                        SyntaxKind.ObjectCreationExpression,
                        SyntaxKind.CollectionInitializerExpression))
                {
                    if (initializer.IsSingleLine(includeExteriorTrivia: false))
                    {
                        context.RegisterRefactoring(
                            "Format initializer on multiple lines",
                            cancellationToken => CSharpFormatter.ToMultiLineAsync(
                                context.Document,
                                initializer,
                                cancellationToken));
                    }
                    else if (expressions.All(expression => expression.IsSingleLine()))
                    {
                        context.RegisterRefactoring(
                            "Format initializer on a single line",
                            cancellationToken => CSharpFormatter.ToSingleLineAsync(
                                context.Document,
                                initializer,
                                cancellationToken));
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ExpandInitializer))
                {
                    await ExpandInitializerRefactoring.ComputeRefactoringsAsync(context, initializer).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseCSharp6DictionaryInitializer) &&
                    context.SupportsCSharp6)
                {
                    await UseCSharp6DictionaryInitializerRefactoring.ComputeRefactoringAsync(context, initializer).ConfigureAwait(false);
                }
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ArgumentListSyntax argumentList)
        {
            SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

            if (arguments.Count == 0)
            {
                return;
            }

            await ArgumentParameterNameRefactoring.ComputeRefactoringsAsync(context, argumentList).ConfigureAwait(false);

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateArgument))
            {
                var refactoring = new DuplicateArgumentRefactoring(argumentList);
                refactoring.ComputeRefactoring(context);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatArgumentList) &&
                (context.Span.IsEmpty || context.Span.IsBetweenSpans(argumentList)))
            {
                if (argumentList.IsSingleLine())
                {
                    if (arguments.Count > 1)
                    {
                        context.RegisterRefactoring(
                            "Format each argument on a separate line",
                            cancellationToken =>
                        {
                            return(CSharpFormatter.ToMultiLineAsync(
                                       context.Document,
                                       argumentList,
                                       cancellationToken));
                        });
                    }
                }
                else
                {
                    string title = (arguments.Count == 1)
                            ? "Format argument on a single line"
                            : "Format all arguments on a single line";

                    context.RegisterRefactoring(
                        title,
                        cancellationToken =>
                    {
                        return(CSharpFormatter.ToSingleLineAsync(
                                   context.Document,
                                   argumentList,
                                   cancellationToken));
                    });
                }
            }
        }
Exemplo n.º 6
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ParameterListSyntax parameterList)
        {
            SeparatedSyntaxList <ParameterSyntax> parameters = parameterList.Parameters;

            if (parameters.Any())
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateParameter))
                {
                    var refactoring = new DuplicateParameterRefactoring(parameterList);
                    refactoring.ComputeRefactoring(context);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CheckParameterForNull))
                {
                    await CheckParameterForNullRefactoring.ComputeRefactoringAsync(context, parameterList).ConfigureAwait(false);
                }

                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.IntroduceAndInitializeField,
                        RefactoringIdentifiers.IntroduceAndInitializeProperty))
                {
                    IntroduceAndInitializeRefactoring.ComputeRefactoring(context, parameterList);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatParameterList) &&
                    (context.Span.IsEmpty || context.Span.IsBetweenSpans(parameterList)))
                {
                    if (parameterList.IsSingleLine())
                    {
                        if (parameters.Count > 1)
                        {
                            context.RegisterRefactoring(
                                "Format each parameter on a separate line",
                                cancellationToken => CSharpFormatter.ToMultiLineAsync(context.Document, parameterList, cancellationToken));
                        }
                    }
                    else
                    {
                        string title = (parameters.Count == 1)
                            ? "Format parameter on a single line"
                            : "Format all parameters on a single line";

                        context.RegisterRefactoring(
                            title,
                            cancellationToken => CSharpFormatter.ToSingleLineAsync(context.Document, parameterList, cancellationToken));
                    }
                }
            }
        }
Exemplo n.º 7
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, AttributeArgumentListSyntax argumentList)
        {
            if (!argumentList.Arguments.Any())
            {
                return;
            }

            await AttributeArgumentParameterNameRefactoring.ComputeRefactoringsAsync(context, argumentList).ConfigureAwait(false);

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateArgument))
            {
                DuplicateAttributeArgumentRefactoring.ComputeRefactoring(context, argumentList);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatArgumentList) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(argumentList))
            {
                if (argumentList.IsSingleLine())
                {
                    if (argumentList.Arguments.Count > 1)
                    {
                        context.RegisterRefactoring(
                            "Format arguments on separate lines",
                            cancellationToken =>
                        {
                            return(CSharpFormatter.ToMultiLineAsync(
                                       context.Document,
                                       argumentList,
                                       cancellationToken));
                        });
                    }
                }
                else
                {
                    context.RegisterRefactoring(
                        "Format arguments on a single line",
                        cancellationToken =>
                    {
                        return(CSharpFormatter.ToSingleLineAsync(
                                   context.Document,
                                   argumentList,
                                   cancellationToken));
                    });
                }
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, MemberAccessExpressionSyntax memberAccessExpression)
        {
            if (context.Span.IsEmpty &&
                memberAccessExpression.Span.Contains(context.Span) &&
                memberAccessExpression.IsKind(SyntaxKind.SimpleMemberAccessExpression))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                List <MemberAccessExpressionSyntax> expressions = GetChain(memberAccessExpression, semanticModel, context.CancellationToken);

                if (expressions.Count > 1)
                {
                    if (expressions[0].IsSingleLine(includeExteriorTrivia: false))
                    {
                        context.RegisterRefactoring(
                            "Format expression chain on multiple lines",
                            cancellationToken =>
                        {
                            return(CSharpFormatter.ToMultiLineAsync(
                                       context.Document,
                                       expressions.ToArray(),
                                       cancellationToken));
                        });
                    }
                    else
                    {
                        context.RegisterRefactoring(
                            "Format expression chain on a single line",
                            cancellationToken =>
                        {
                            return(CSharpFormatter.ToSingleLineAsync(
                                       context.Document,
                                       expressions[0],
                                       cancellationToken));
                        });
                    }
                }
            }
        }