public static Task <Document> AddCallToOnPropertyChangedInProp(Document document, Diagnostic diagnostic, SyntaxNode root)
        {
            var compilationUnit = root as CompilationUnitSyntax;
            var node            = compilationUnit.FindNode(diagnostic.Location.SourceSpan).FirstAncestorOrSelf <PropertyDeclarationSyntax>();

            var newRoot = root;


            if (node.AccessorList.Accessors[1].Body == null)
            {
                (newRoot, node) = ConvertToFullProperty(newRoot, node);
            }


            (newRoot, node) = AddCallToOnPropertyChanged(newRoot, node.AccessorList.Accessors[1]);

            var cls = node.FirstAncestorOrSelf <ClassDeclarationSyntax>();

            if (!OnPropertyChangedHelper.ImplementsOnPropertyChanged(cls))
            {
                newRoot = OnPropertyChangedHelper.CreateOnPropertyChanged(newRoot, cls);
            }

            return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
        }
Example #2
0
        public sealed override async 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 => OnPropertyChangedHelper.ImplementOnPropertyChanged(context.Document, diagnostic, root),
                    equivalenceKey: title),
                diagnostic);
        }
        private void SyntaxNodeAction(SyntaxNodeAnalysisContext node)
        {
            var classNode = node.Node as ClassDeclarationSyntax;

            if (classNode.BaseList == null || classNode.BaseList.Types == null)
            {
                return;
            }

            if (OnPropertyChangedHelper.ImplementsOnPropertyChanged(classNode))
            {
                return;
            }

            foreach (var tp in classNode.BaseList.Types.Select(x => x.Type).OfType <SimpleNameSyntax>())
            {
                if (tp.Identifier.Text == "INotifyPropertyChanged")
                {
                    var diag = Diagnostic.Create(Rule, classNode.Identifier.GetLocation());
                    node.ReportDiagnostic(diag);
                }
            }
        }