Example #1
0
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var usingStatement = (UsingStatementSyntax)context.Node;

            if (RemoveBracesFromUsingStatementRefactoring.CanRefactor(usingStatement))
            {
                var block = (BlockSyntax)usingStatement.Statement;

                context.ReportDiagnostic(
                    DiagnosticDescriptors.SimplifyNestedUsingStatement,
                    block.GetLocation());

                context.FadeOutBraces(DiagnosticDescriptors.SimplifyNestedUsingStatementFadeOut, block);
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            UsingStatementSyntax usingStatement = root
                                                  .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                  .FirstAncestorOrSelf <UsingStatementSyntax>();

            if (usingStatement == null)
            {
                return;
            }

            bool fMultiple = usingStatement.Statement
                             .DescendantNodes()
                             .Any(f => f.IsKind(SyntaxKind.UsingStatement) && UsingStatementAnalysis.ContainsEmbeddableUsingStatement((UsingStatementSyntax)f));

            string title = "Remove braces from 'using' statement";

            if (fMultiple)
            {
                title += "s";
            }

            CodeAction codeAction = CodeAction.Create(
                title,
                cancellationToken =>
            {
                return(RemoveBracesFromUsingStatementRefactoring.RefactorAsync(
                           context.Document,
                           usingStatement,
                           cancellationToken));
            },
                DiagnosticIdentifiers.SimplifyNestedUsingStatement + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }