コード例 #1
0
        private async static Task <Document> MakeStringInterpolationAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var invocationExpression   = root.FindToken(diagnostic.Location.SourceSpan.Start).Parent.AncestorsAndSelf().OfType <InvocationExpressionSyntax>().First();
            var newStringInterpolation = StringFormatCodeFixProvider.CreateNewStringInterpolation(invocationExpression);
            var newArgumentList        = SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList <ArgumentSyntax>().Add(SyntaxFactory.Argument(newStringInterpolation)));
            var newRoot     = root.ReplaceNode(invocationExpression.ArgumentList, newArgumentList);
            var newDocument = document.WithSyntaxRoot(newRoot);

            return(newDocument);
        }
コード例 #2
0
        private async static Task <SyntaxNode> GetFixedDocumentAsync(FixAllContext fixAllContext, Document document)
        {
            var diagnostics = await fixAllContext.GetDocumentDiagnosticsAsync(document).ConfigureAwait(false);

            var root = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);

            var nodes   = diagnostics.Select(d => root.FindNode(d.Location.SourceSpan, getInnermostNodeForTie: true).FirstAncestorOrSelfOfType <InvocationExpressionSyntax>()).Where(n => !n.IsMissing).ToList();
            var newRoot = root.ReplaceNodes(nodes, (original, rewritten) => rewritten.WithAdditionalAnnotations(stringFormatAnnotation));

            while (true)
            {
                var annotatedNodes = newRoot.GetAnnotatedNodes(stringFormatAnnotation);
                var node           = annotatedNodes.FirstOrDefault();
                if (node == null)
                {
                    break;
                }
                newRoot = StringFormatCodeFixProvider.CreateNewStringInterpolation(newRoot, (InvocationExpressionSyntax)node);
            }
            return(newRoot);
        }