private static Task <Solution> ApplyFixAsync(CancellationToken cancellationToken, CodeFixContext context, Diagnostic diagnostic, SemanticModel semanticModel, SyntaxNode syntaxRoot, SyntaxToken token)
        {
            var parameter = syntaxRoot.FindNode(diagnostic.Location.SourceSpan)
                            .FirstAncestorOrSelf <ParameterSyntax>();
            var constructorDeclarationSyntax = parameter.FirstAncestorOrSelf <ConstructorDeclarationSyntax>();

            using (var pooled = GU0003CtorParameterNamesShouldMatch.ConstructorAssignmentsWalker.Create(constructorDeclarationSyntax, semanticModel, context.CancellationToken))
            {
                foreach (var kvp in pooled.Item.ParameterNameMap)
                {
                    if (kvp.Key == parameter)
                    {
                        return(RenameHelper.RenameSymbolAsync(
                                   context.Document,
                                   syntaxRoot,
                                   token,
                                   kvp.Value,
                                   cancellationToken));
                    }
                }
            }

            // should never get here.
            return(Task.FromResult(context.Document.Project.Solution));
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken)
                             .ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);

                if (diagnostic.Properties.TryGetValue("Name", out var name))
                {
                    context.RegisterCodeFix(
                        CodeAction.Create(
                            "Rename parameter",
                            cancellationToken => RenameHelper.RenameSymbolAsync(
                                context.Document,
                                syntaxRoot,
                                token,
                                name,
                                cancellationToken),
                            nameof(NameArgumentsCodeFixProvider)),
                        diagnostic);
                }
            }
        }