public static Task <Document> RefactorAsync(
     Document document,
     InitializerExpressionSyntax initializer,
     CancellationToken cancellationToken)
 {
     return(DocumentFormatter.ToSingleLineAsync(document, initializer, cancellationToken));
 }
Exemple #2
0
        public static void ComputeRefactorings(RefactoringContext context, AccessorDeclarationSyntax accessor)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatAccessorBraces))
            {
                BlockSyntax body = accessor.Body;

                if (body?.Span.Contains(context.Span) == true &&
                    !body.OpenBraceToken.IsMissing &&
                    !body.CloseBraceToken.IsMissing)
                {
                    if (body.IsSingleLine())
                    {
                        if (accessor.Parent?.IsMultiLine() == true)
                        {
                            context.RegisterRefactoring(
                                "Format braces on separate lines",
                                cancellationToken => DocumentFormatter.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 => DocumentFormatter.ToSingleLineAsync(context.Document, accessor, cancellationToken));
                        }
                    }
                }
            }

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

                if (accessor.Parent is AccessorListSyntax accessorList)
                {
                    SyntaxList <AccessorDeclarationSyntax> accessors = accessorList.Accessors;

                    if (accessors.Count == 1 &&
                        accessors.First().IsKind(SyntaxKind.GetAccessorDeclaration) &&
                        (accessorList.Parent is MemberDeclarationSyntax parent))
                    {
                        node = parent;
                    }
                }

                context.RegisterRefactoring(
                    "Use expression-bodied member",
                    cancellationToken => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, node, cancellationToken));
            }
        }
Exemple #3
0
        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(DocumentFormatter.ToMultiLineAsync(
                                       context.Document,
                                       conditionalExpression,
                                       cancellationToken));
                        });
                    }
                    else
                    {
                        context.RegisterRefactoring(
                            "Format ?: on a single line",
                            cancellationToken =>
                        {
                            return(DocumentFormatter.ToSingleLineAsync(
                                       context.Document,
                                       conditionalExpression,
                                       cancellationToken));
                        });
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceConditionalExpressionWithIfElse))
                {
                    await ReplaceConditionalExpressionWithIfElseRefactoring.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));
                });
            }
        }
Exemple #4
0
        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 => DocumentFormatter.ToMultiLineAsync(
                                context.Document,
                                initializer,
                                cancellationToken));
                    }
                    else if (expressions.All(expression => expression.IsSingleLine()))
                    {
                        context.RegisterRefactoring(
                            "Format initializer on a single line",
                            cancellationToken => DocumentFormatter.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 AddOrRemoveParameterNameRefactoring.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 arguments on separate lines",
                            cancellationToken =>
                        {
                            return(DocumentFormatter.ToMultiLineAsync(
                                       context.Document,
                                       argumentList,
                                       cancellationToken));
                        });
                    }
                }
                else
                {
                    context.RegisterRefactoring(
                        "Format arguments on a single line",
                        cancellationToken =>
                    {
                        return(DocumentFormatter.ToSingleLineAsync(
                                   context.Document,
                                   argumentList,
                                   cancellationToken));
                    });
                }
            }
        }
Exemple #6
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(DocumentFormatter.ToMultiLineAsync(
                                       context.Document,
                                       argumentList,
                                       cancellationToken));
                        });
                    }
                }
                else
                {
                    context.RegisterRefactoring(
                        "Format arguments on a single line",
                        cancellationToken =>
                    {
                        return(DocumentFormatter.ToSingleLineAsync(
                                   context.Document,
                                   argumentList,
                                   cancellationToken));
                    });
                }
            }
        }
Exemple #7
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 parameters on separate lines",
                                cancellationToken => DocumentFormatter.ToMultiLineAsync(context.Document, parameterList, cancellationToken));
                        }
                    }
                    else
                    {
                        context.RegisterRefactoring(
                            "Format parameters on a single line",
                            cancellationToken => DocumentFormatter.ToSingleLineAsync(context.Document, parameterList, cancellationToken));
                    }
                }
            }
        }
Exemple #8
0
        public static void ComputeRefactorings(RefactoringContext context, BinaryExpressionSyntax binaryExpression)
        {
            binaryExpression = GetBinaryExpression(binaryExpression, context.Span);

            if (binaryExpression != null &&
                IsFormattableKind(binaryExpression.Kind()))
            {
                if (binaryExpression.IsSingleLine())
                {
                    context.RegisterRefactoring(
                        "Format binary expression on multiple lines",
                        cancellationToken => DocumentFormatter.ToMultiLineAsync(context.Document, binaryExpression, cancellationToken));
                }
                else
                {
                    context.RegisterRefactoring(
                        "Format binary expression on a single line",
                        cancellationToken => DocumentFormatter.ToSingleLineAsync(context.Document, binaryExpression, 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(DocumentFormatter.ToMultiLineAsync(
                                       context.Document,
                                       expressions.ToArray(),
                                       cancellationToken));
                        });
                    }
                    else
                    {
                        context.RegisterRefactoring(
                            "Format expression chain on a single line",
                            cancellationToken =>
                        {
                            return(DocumentFormatter.ToSingleLineAsync(
                                       context.Document,
                                       expressions[0],
                                       cancellationToken));
                        });
                    }
                }
            }
        }