Exemplo n.º 1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().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) && SimplifyNestedUsingStatementRefactoring.ContainsEmbeddableUsingStatement((UsingStatementSyntax)f));

            string title = "Remove braces from using statement";

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

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

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
Exemplo n.º 2
0
        public override void Initialize(AnalysisContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            base.Initialize(context);

            context.RegisterSyntaxNodeAction(
                f => SimplifyNestedUsingStatementRefactoring.Analyze(f, (UsingStatementSyntax)f.Node),
                SyntaxKind.UsingStatement);
        }
Exemplo n.º 3
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out UsingStatementSyntax usingStatement))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Remove braces",
                cancellationToken => SimplifyNestedUsingStatementRefactoring.RefactorAsync(context.Document, usingStatement, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.SimplifyNestedUsingStatement));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out UsingStatementSyntax usingStatement))
            {
                return;
            }

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

            CodeAction codeAction = CodeAction.Create(
                "Remove braces",
                cancellationToken => SimplifyNestedUsingStatementRefactoring.RefactorAsync(context.Document, usingStatement, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.SimplifyNestedUsingStatement));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
        {
            var usingStatement = (UsingStatementSyntax)context.Node;

            SimplifyNestedUsingStatementRefactoring.Analyze(context, usingStatement);
        }