示例#1
0
        protected override async Task <SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document)
        {
            var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false);

            if (diagnostics.IsEmpty)
            {
                return(null);
            }

            DocumentEditor editor = await DocumentEditor.CreateAsync(document, fixAllContext.CancellationToken).ConfigureAwait(false);

            SyntaxNode root = editor.GetChangedRoot();

            ImmutableList <SyntaxNode> nodesToChange = ImmutableList.Create <SyntaxNode>();

            // Make sure all nodes we care about are tracked
            foreach (var diagnostic in diagnostics)
            {
                var location   = diagnostic.Location;
                var syntaxNode = root.FindNode(location.SourceSpan);
                if (syntaxNode != null)
                {
                    editor.TrackNode(syntaxNode);
                    nodesToChange = nodesToChange.Add(syntaxNode);
                }
            }

            foreach (var node in nodesToChange)
            {
                editor.ReplaceNode(node, node.WithLeadingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed));
            }

            return(editor.GetChangedRoot());
        }
        private static void Move(DocumentEditor editor, BasePropertyDeclarationSyntax property)
        {
            editor.TrackNode(property);
            _ = editor.ReplaceNode(
                (TypeDeclarationSyntax)property.Parent,
                syntax => WithMoved(syntax));

            SyntaxNode WithMoved(TypeDeclarationSyntax old)
            {
                switch (old)
                {
                case ClassDeclarationSyntax classDeclaration:
                    return(classDeclaration.WithMembers(SortPropertiesFix.WithMoved(old.Members, old.GetCurrentNode(property))));

                case StructDeclarationSyntax structDeclaration:
                    return(structDeclaration.WithMembers(SortPropertiesFix.WithMoved(old.Members, old.GetCurrentNode(property))));

                default:
                    return(old);
                }
            }
        }