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 => FormatAccessorBraceOnMultipleLinesRefactoring.RefactorAsync(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 => FormatAccessorBraceOnSingleLineRefactoring.RefactorAsync(context.Document, accessor, 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));
            }
        }
Пример #3
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",
                                ct => SyntaxFormatter.ToMultiLineAsync(context.Document, accessor, ct),
                                RefactoringIdentifiers.FormatAccessorBraces);
                        }
                    }
                    else
                    {
                        SyntaxList <StatementSyntax> statements = body.Statements;

                        if (body.Statements.SingleOrDefault(shouldThrow: false)?.IsSingleLine() == true &&
                            accessor.DescendantTrivia(accessor.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                        {
                            context.RegisterRefactoring(
                                "Format braces on a single line",
                                ct => SyntaxFormatter.ToSingleLineAsync(context.Document, accessor, ct),
                                RefactoringIdentifiers.FormatAccessorBraces);
                        }
                    }
                }
            }

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

                if (accessor.Parent is AccessorListSyntax accessorList &&
                    accessorList.Accessors.SingleOrDefault(shouldThrow: false)?.Kind() == SyntaxKind.GetAccessorDeclaration &&
                    (accessorList.Parent is MemberDeclarationSyntax parent))
                {
                    node = parent;
                }

                context.RegisterRefactoring(
                    "Use expression-bodied member",
                    ct => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, node, ct),
                    RefactoringIdentifiers.UseExpressionBodiedMember);
            }
        }
Пример #4
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",
                                ct => SyntaxFormatter.ToMultiLineAsync(context.Document, accessor, ct),
                                RefactoringIdentifiers.FormatAccessorBraces);
                        }
                    }
                    else if (body.Statements.SingleOrDefault(shouldThrow: false)?.IsSingleLine() == true &&
                             accessor.DescendantTrivia(accessor.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        context.RegisterRefactoring(
                            "Format braces on a single line",
                            ct => SyntaxFormatter.ToSingleLineAsync(context.Document, accessor, ct),
                            RefactoringIdentifiers.FormatAccessorBraces);
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                context.SupportsCSharp6)
            {
                BlockSyntax body = accessor.Body;

                if (body != null &&
                    (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(accessor) ||
                     context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(body)) &&
                    !accessor.AttributeLists.Any() &&
                    ((accessor.IsKind(SyntaxKind.GetAccessorDeclaration))
                        ? UseExpressionBodiedMemberAnalysis.GetReturnExpression(body) != null
                        : UseExpressionBodiedMemberAnalysis.GetExpression(body) != null) &&
                    (accessor.Parent as AccessorListSyntax)?
                    .Accessors
                    .SingleOrDefault(shouldThrow: false)?
                    .Kind() != SyntaxKind.GetAccessorDeclaration)
                {
                    context.RegisterRefactoring(
                        UseExpressionBodiedMemberRefactoring.Title,
                        ct => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, accessor, ct),
                        RefactoringIdentifiers.UseExpressionBodiedMember);
                }
            }
        }
        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",
                                ct => SyntaxFormatter.ToMultiLineAsync(context.Document, accessor, ct),
                                RefactoringIdentifiers.FormatAccessorBraces);
                        }
                    }
                    else if (body.Statements.SingleOrDefault(shouldThrow: false)?.IsSingleLine() == true &&
                             accessor.DescendantTrivia(accessor.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        context.RegisterRefactoring(
                            "Format braces on a single line",
                            ct => SyntaxFormatter.ToSingleLineAsync(context.Document, accessor, ct),
                            RefactoringIdentifiers.FormatAccessorBraces);
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                context.SupportsCSharp6 &&
                UseExpressionBodiedMemberRefactoring.CanRefactor(accessor, context.Span))
            {
                context.RegisterRefactoring(
                    UseExpressionBodiedMemberRefactoring.Title,
                    ct => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, accessor, ct),
                    RefactoringIdentifiers.UseExpressionBodiedMember);
            }
        }
Пример #6
0
        private static void FadeOut(SyntaxNodeAnalysisContext context, AnonymousMethodExpressionSyntax anonymousMethod)
        {
            DiagnosticDescriptor descriptor = DiagnosticDescriptors.ReplaceAnonymousMethodWithLambdaExpressionFadeOut;

            context.FadeOutToken(descriptor, anonymousMethod.DelegateKeyword);

            BlockSyntax block = anonymousMethod.Block;

            if (block.Statements.Count == 1 && block.IsSingleLine())
            {
                StatementSyntax statement = block.Statements[0];

                if (statement.IsKind(SyntaxKind.ReturnStatement, SyntaxKind.ExpressionStatement))
                {
                    context.FadeOutBraces(descriptor, block);

                    if (statement.IsKind(SyntaxKind.ReturnStatement))
                    {
                        context.FadeOutToken(descriptor, ((ReturnStatementSyntax)statement).ReturnKeyword);
                    }
                }
            }
        }
Пример #7
0
        private static void FadeOut(SyntaxNodeAnalysisContext context, AnonymousMethodExpressionSyntax anonymousMethod)
        {
            DiagnosticHelpers.ReportToken(context, DiagnosticDescriptors.UseLambdaExpressionInsteadOfAnonymousMethodFadeOut, anonymousMethod.DelegateKeyword);

            BlockSyntax block = anonymousMethod.Block;

            SyntaxList <StatementSyntax> statements = block.Statements;

            if (statements.Count == 1 &&
                block.IsSingleLine())
            {
                StatementSyntax statement = statements[0];

                if (statement.IsKind(SyntaxKind.ReturnStatement, SyntaxKind.ExpressionStatement))
                {
                    CSharpDiagnosticHelpers.ReportBraces(context, DiagnosticDescriptors.UseLambdaExpressionInsteadOfAnonymousMethodFadeOut, block);

                    if (statement.IsKind(SyntaxKind.ReturnStatement))
                    {
                        DiagnosticHelpers.ReportToken(context, DiagnosticDescriptors.UseLambdaExpressionInsteadOfAnonymousMethodFadeOut, ((ReturnStatementSyntax)statement).ReturnKeyword);
                    }
                }
            }
        }