Beispiel #1
0
        /// <inheritdoc/>
        protected override async Task RegisterCodeFixesAsync(DocumentEditorCodeFixContext context)
        {
            var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken)
                             .ConfigureAwait(false);

            var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken)
                                .ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                if (syntaxRoot.TryFindNode(diagnostic, out ArgumentSyntax? argument) &&
                    argument.Expression is LiteralExpressionSyntax literal &&
                    semanticModel.LookupSymbols(argument.SpanStart, name: literal.Token.ValueText).TryFirst(out var member))
                {
                    context.RegisterCodeFix(
                        "Use nameof",
                        async(editor, cancellationToken) =>
                    {
                        var replacement = await editor.SymbolAccessAsync(member, literal, cancellationToken)
                                          .ConfigureAwait(false);
                        _ = editor.ReplaceNode(
                            literal,
                            x => InpcFactory.Nameof(replacement).WithTriviaFrom(x));
                    },
                        nameof(UseNameofFix),
                        diagnostic);
                }
            }
        }
        internal static async Task <ExpressionStatementSyntax> OnPropertyChangedInvocationStatementAsync(this DocumentEditor editor, IMethodSymbol invoker, string propertyName, CancellationToken cancellationToken)
        {
            var qualifyMethodAccess = await editor.QualifyMethodAccessAsync(cancellationToken)
                                      .ConfigureAwait(false);

            var qualifyPropertyAccess = await editor.QualifyPropertyAccessAsync(cancellationToken)
                                        .ConfigureAwait(false);

            return(InpcFactory.OnPropertyChangedInvocationStatement(
                       InpcFactory.SymbolAccess(invoker.Name, qualifyMethodAccess),
                       InpcFactory.Nameof(InpcFactory.SymbolAccess(propertyName, qualifyPropertyAccess))));
        }