Esempio n. 1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

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

            Debug.Assert(parameter != null, $"{nameof(parameter)} is null");

            if (parameter == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AddParameterToDocumentationComment:
                {
                    var refactoring = new AddParameterToDocumentationCommentRefactoring();

                    CodeAction codeAction = CodeAction.Create(
                        "Add parameter to documentation comment",
                        cancellationToken => refactoring.RefactorAsync(context.Document, parameter, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.OverridingMemberCannotChangeParamsModifier:
                {
                    CodeAction codeAction = CodeAction.Create(
                        (parameter.IsParams())
                                    ? "Remove 'params' modifier"
                                    : "Add 'params' modifier",
                        cancellationToken => OverridingMemberCannotChangeParamsModifierRefactoring.RefactorAsync(context.Document, parameter, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

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

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out ParameterSyntax parameter))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AddParameterToDocumentationComment:
                {
                    var refactoring = new AddParameterToDocumentationCommentRefactoring();

                    CodeAction codeAction = CodeAction.Create(
                        "Add parameter to documentation comment",
                        cancellationToken => refactoring.RefactorAsync(context.Document, parameter, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.OverridingMemberCannotChangeParamsModifier:
                {
                    CodeAction codeAction = CodeAction.Create(
                        (parameter.IsParams())
                                    ? "Remove 'params' modifier"
                                    : "Add 'params' modifier",
                        cancellationToken => OverridingMemberCannotChangeParamsModifierRefactoring.RefactorAsync(context.Document, parameter, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }