Example #1
0
 public Rewriter(MyWalker walker, ProjectItemDetailsType projectItemDetails, ICleanupOption options) : base(options)
 {
     this.walker             = walker;
     this.projectItemDetails = projectItemDetails;
     semanticModel           = projectItemDetails.SemanticModel;
     WorkingDocument         = projectItemDetails.ProjectItemDocument;
 }
Example #2
0
        public override SyntaxNode CleanUp(SyntaxNode initialSourceNode)
        {
            orginalDocument = ProjectItemDetails.ProjectItemDocument;
            WorkingDocument = ProjectItemDetails.ProjectItemDocument;

            var walker = new MyWalker(ProjectItemDetails, Options);

            walker.Visit(initialSourceNode);

            if (walker.VariablesToRemove.Any())
            {
                var rewriter = new Rewriter(walker, ProjectItemDetails, Options);
                initialSourceNode = rewriter.Visit(initialSourceNode);
                WorkingDocument   = WorkingDocument.WithSyntaxRoot(initialSourceNode);

                IEnumerable <SyntaxNode> annotations;

                do
                {
                    annotations = initialSourceNode.GetAnnotatedNodes(SELECTED_METHOD_ANNOTATION_RENAME);

                    if (annotations.Any() == false)
                    {
                        break;
                    }

                    var firstAnnotatedItem             = annotations.First();
                    var annotationOfFirstAnnotatedItem = firstAnnotatedItem.GetAnnotations(SELECTED_METHOD_ANNOTATION_RENAME).FirstOrDefault();

                    var renameResult = Renamer.RenameSymbol(WorkingDocument, initialSourceNode, null, firstAnnotatedItem, annotationOfFirstAnnotatedItem.Data);

                    WorkingDocument   = renameResult.Document;
                    initialSourceNode = WorkingDocument.GetSyntaxRootAsync().Result;

                    firstAnnotatedItem = initialSourceNode.GetAnnotatedNodes(annotationOfFirstAnnotatedItem).FirstOrDefault();
                    initialSourceNode  = initialSourceNode.ReplaceNode(firstAnnotatedItem, firstAnnotatedItem.WithoutAnnotations(SELECTED_METHOD_ANNOTATION_RENAME));

                    WorkingDocument   = WorkingDocument.WithSyntaxRoot(initialSourceNode);
                    initialSourceNode = WorkingDocument.GetSyntaxRootAsync().Result;
                }while (annotations.Any());

                initialSourceNode = initialSourceNode.RemoveNodes(initialSourceNode.GetAnnotatedNodes(SELECTED_METHOD_ANNOTATION_REMOVE), SyntaxRemoveOptions.KeepEndOfLine);
                var t = initialSourceNode.DescendantNodes().OfType <FieldDeclarationSyntax>().Where(x => x.Declaration.Variables.Count == 0);
                initialSourceNode = initialSourceNode.RemoveNodes(t, SyntaxRemoveOptions.KeepEndOfLine);

                WorkingDocument = WorkingDocument.WithSyntaxRoot(initialSourceNode);

                return(null);
            }

            return(initialSourceNode);
        }
Example #3
0
        public override async Task <SyntaxNode> CleanUpAsync(SyntaxNode initialSourceNode)
        {
            orginalDocument = ProjectItemDetails.ProjectItemDocument;
            WorkingDocument = ProjectItemDetails.ProjectItemDocument;

            var walker = new MyWalker(ProjectItemDetails, Options);

            walker.Visit(initialSourceNode);

            if (walker.VariablesToRemove.Any())
            {
                var rewriter = new Rewriter(walker, ProjectItemDetails, Options);
                initialSourceNode = rewriter.Visit(initialSourceNode);
                WorkingDocument   = WorkingDocument.WithSyntaxRoot(initialSourceNode);

                IEnumerable <SyntaxNode> annotations;

                do
                {
                    annotations = initialSourceNode.GetAnnotatedNodes(SELECTED_METHOD_ANNOTATION_RENAME);

                    if (annotations.Any() == false)
                    {
                        break;
                    }

                    var firstAnnotatedItem             = annotations.FirstOrDefault();
                    var annotationOfFirstAnnotatedItem = firstAnnotatedItem.GetAnnotations(SELECTED_METHOD_ANNOTATION_RENAME).FirstOrDefault();

                    var renameResult = await Renamer.RenameSymbolAsync(WorkingDocument, initialSourceNode, null, firstAnnotatedItem, annotationOfFirstAnnotatedItem.Data);

                    WorkingDocument   = renameResult.Document;
                    initialSourceNode = await WorkingDocument.GetSyntaxRootAsync();

                    firstAnnotatedItem = initialSourceNode.GetAnnotatedNodes(annotationOfFirstAnnotatedItem).FirstOrDefault();
                    initialSourceNode  = initialSourceNode.ReplaceNode(firstAnnotatedItem, firstAnnotatedItem.WithoutAnnotations(SELECTED_METHOD_ANNOTATION_RENAME));

                    WorkingDocument   = WorkingDocument.WithSyntaxRoot(initialSourceNode);
                    initialSourceNode = await WorkingDocument.GetSyntaxRootAsync();
                }while (annotations.Any());

                var lineSpan = initialSourceNode.GetAnnotatedNodes(SELECTED_METHOD_ANNOTATION_REMOVE)
                               .FirstOrDefault().GetFileLinePosSpan();

                initialSourceNode = initialSourceNode.RemoveNodes(initialSourceNode.GetAnnotatedNodes(SELECTED_METHOD_ANNOTATION_REMOVE), SyntaxRemoveOptions.KeepEndOfLine);
                var t = initialSourceNode.DescendantNodes().OfType <FieldDeclarationSyntax>().Where(x => x.Declaration.Variables.Count == 0);
                initialSourceNode = initialSourceNode.RemoveNodes(t, SyntaxRemoveOptions.KeepEndOfLine);

                WorkingDocument = WorkingDocument.WithSyntaxRoot(initialSourceNode);

                if (IsReportOnlyMode &&
                    !IsEquivalentToUNModified(initialSourceNode))
                {
                    CollectMessages(new ChangesReport(await orginalDocument.GetSyntaxRootAsync())
                    {
                        LineNumber = lineSpan.StartLinePosition.Line,
                        Column     = lineSpan.StartLinePosition.Character,
                        Message    = "ConvertPropertiesToAutoProperties",
                        Generator  = nameof(ConvertPropertiesToAutoProperties)
                    });

                    return(initialSourceNode);
                }

                return(null);
            }

            return(initialSourceNode);
        }