private static async Task ComputeRefactoringsAsync(RefactoringContext context, ExpressionSyntax expression)
        {
            if (!context.Span.IsEmptyAndContainedInSpan(expression))
            {
                return;
            }

            expression = CSharpUtility.GetTopmostExpressionInCallChain(expression);

            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            if (!IsFormattable(expression, semanticModel, context.CancellationToken))
            {
                return;
            }

            if (expression.IsSingleLine(includeExteriorTrivia: false))
            {
                context.RegisterRefactoring(
                    "Format expression chain on multiple lines",
                    ct => SyntaxFormatter.ToMultiLineAsync(context.Document, expression, semanticModel, ct),
                    RefactoringIdentifiers.FormatExpressionChain);
            }
            else if (expression.DescendantTrivia(expression.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                context.RegisterRefactoring(
                    "Format expression chain on a single line",
                    ct => SyntaxFormatter.ToSingleLineAsync(context.Document, expression, ct),
                    RefactoringIdentifiers.FormatExpressionChain);
            }
        }
示例#2
0
        private static ExpressionSyntax ProcessExpression(ExpressionSyntax expression)
        {
            if (expression
                .DescendantTrivia(expression.Span)
                .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                expression = SyntaxRemover.RemoveWhitespaceOrEndOfLine(expression)
                             .WithFormatterAnnotation();
            }

            return(expression);
        }
        private static ExpressionSyntax ProcessExpression(ExpressionSyntax expression)
        {
            if (expression
                .DescendantTrivia(expression.Span)
                .All(f => f.IsWhitespaceOrEndOfLine()))
            {
                expression = WhitespaceOrEndOfLineRemover.RemoveFrom(expression)
                             .WithAdditionalAnnotations(Formatter.Annotation);
            }

            return(expression);
        }
        private static async Task ComputeRefactoringsAsync(RefactoringContext context, ExpressionSyntax expression)
        {
            if (!context.Span.IsEmptyAndContainedInSpan(expression))
            {
                return;
            }

            while (true)
            {
                SyntaxNode parent = expression.Parent;

                if (parent.IsKind(
                        SyntaxKind.ConditionalAccessExpression,
                        SyntaxKind.SimpleMemberAccessExpression,
                        SyntaxKind.ElementAccessExpression,
                        SyntaxKind.MemberBindingExpression,
                        SyntaxKind.InvocationExpression))
                {
                    expression = (ExpressionSyntax)parent;
                }
                else
                {
                    break;
                }
            }

            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            if (!IsFormattable(expression, semanticModel, context.CancellationToken))
            {
                return;
            }

            if (expression.IsSingleLine(includeExteriorTrivia: false))
            {
                context.RegisterRefactoring(
                    "Format expression chain on multiple lines",
                    ct => SyntaxFormatter.ToMultiLineAsync(context.Document, expression, semanticModel, ct),
                    RefactoringIdentifiers.FormatExpressionChain);
            }
            else if (expression.DescendantTrivia(expression.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                context.RegisterRefactoring(
                    "Format expression chain on a single line",
                    ct => SyntaxFormatter.ToSingleLineAsync(context.Document, expression, ct),
                    RefactoringIdentifiers.FormatExpressionChain);
            }
        }