Esempio n. 1
0
        public async Task <Document> CleanupAsync(Document document, ImmutableArray <TextSpan> spans, CodeCleanupOptions options, ImmutableArray <ICodeCleanupProvider> providers, CancellationToken cancellationToken)
        {
            using (Logger.LogBlock(FunctionId.CodeCleanup_CleanupAsync, cancellationToken))
            {
                // If there is no span to format...
                if (!spans.Any())
                {
                    // ... then return the Document unchanged
                    return(document);
                }

                var codeCleaners = providers.IsDefault ? GetDefaultProviders() : providers;

                var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

                var normalizedSpan = spans.ToNormalizedSpans();
                if (CleanupWholeNode(root.FullSpan, normalizedSpan))
                {
                    // We are cleaning up the whole document, so there is no need to do expansive span tracking between cleaners.
                    return(await IterateAllCodeCleanupProvidersAsync(document, document, options, r => ImmutableArray.Create(r.FullSpan), codeCleaners, cancellationToken).ConfigureAwait(false));
                }

                // We need to track spans between cleaners. Annotate the tree with the provided spans.
                var(newNode, annotations) = AnnotateNodeForTextSpans(root, normalizedSpan, cancellationToken);

                // If it urns out we don't need to annotate anything since all spans are merged to one span that covers the whole node...
                if (newNode == null)
                {
                    // ... then we are cleaning up the whole document, so there is no need to do expansive span tracking between cleaners.
                    return(await IterateAllCodeCleanupProvidersAsync(document, document, options, n => ImmutableArray.Create(n.FullSpan), codeCleaners, cancellationToken).ConfigureAwait(false));
                }

                // Replace the initial node and document with the annotated node.
                var annotatedRoot     = newNode;
                var annotatedDocument = document.WithSyntaxRoot(annotatedRoot);

                // Run the actual cleanup.
                return(await IterateAllCodeCleanupProvidersAsync(
                           document, annotatedDocument, options,
                           r => GetTextSpansFromAnnotation(r, annotations, cancellationToken),
                           codeCleaners, cancellationToken).ConfigureAwait(false));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Clean up the provided spans in the document.
        /// Optionally you can provide your own options and code cleaners. Otherwise, the default will be used.
        /// </summary>
        public static async Task <Document> CleanupAsync(Document document, ImmutableArray <TextSpan> spans, CodeCleanupOptions options, ImmutableArray <ICodeCleanupProvider> providers = default, CancellationToken cancellationToken = default)
        {
            var cleanupService = document.GetRequiredLanguageService <ICodeCleanerService>();

            return(await cleanupService.CleanupAsync(document, spans, options, providers, cancellationToken).ConfigureAwait(false));
        }
Esempio n. 3
0
        /// <summary>
        /// Cleans up the document marked with the provided annotation.
        /// Optionally you can provide your own options and code cleaners. Otherwise, the default will be used.
        /// </summary>
        public static async Task <Document> CleanupAsync(Document document, SyntaxAnnotation annotation, CodeCleanupOptions options, ImmutableArray <ICodeCleanupProvider> providers = default, CancellationToken cancellationToken = default)
        {
            var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            return(await CleanupAsync(document, root.GetAnnotatedNodesAndTokens(annotation).Select(n => n.Span).ToImmutableArray(), options, providers, cancellationToken : cancellationToken).ConfigureAwait(false));
        }
Esempio n. 4
0
 /// <summary>
 /// Clean up the provided span in the document.
 /// Optionally you can provide your own options and code cleaners. Otherwise, the default will be used.
 /// </summary>
 public static Task <Document> CleanupAsync(Document document, TextSpan span, CodeCleanupOptions options, ImmutableArray <ICodeCleanupProvider> providers = default, CancellationToken cancellationToken = default)
 => CleanupAsync(document, ImmutableArray.Create(span), options, providers, cancellationToken);
Esempio n. 5
0
        /// <summary>
        /// Cleans up the whole document.
        /// Optionally you can provide your own options and code cleaners. Otherwise, the default will be used.
        /// </summary>
        public static async Task <Document> CleanupAsync(Document document, CodeCleanupOptions options, ImmutableArray <ICodeCleanupProvider> providers = default, CancellationToken cancellationToken = default)
        {
            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            return(await CleanupAsync(document, new TextSpan(0, text.Length), options, providers, cancellationToken : cancellationToken).ConfigureAwait(false));
        }