private void SyntaxNodeAction(SyntaxNodeAnalysisContext tree) { var prop = tree.Node as PropertyDeclarationSyntax; if (!prop.FirstAncestorOrSelf <ClassDeclarationSyntax>().ImplementsType("INotifyPropertyChanged")) { return; } var accessors = prop.AccessorList.Accessors; if (accessors.Count < 2) { return; } if (OnPropertyChangedCallHelper.CallsOnPropertyChanged(accessors[1])) { return; } var diag = Diagnostic.Create(Rule, prop.Identifier.GetLocation()); tree.ReportDiagnostic(diag); }
public async override Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); // TODO: Replace the following code with your own analysis, generating a CodeAction for each fix to suggest var diagnostic = context.Diagnostics.First(); var diagnosticSpan = diagnostic.Location.SourceSpan; // Find the type declaration identified by the diagnostic. var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType <TypeDeclarationSyntax>().First(); // Register a code action that will invoke the fix. context.RegisterCodeFix( CodeAction.Create( title: title, createChangedDocument: c => OnPropertyChangedCallHelper.AddCallToOnPropertyChangedInProp(context.Document, diagnostic, root), equivalenceKey: title), diagnostic); }