Esempio n. 1
0
        private static IFormattingResult?GetFormattingResult(SyntaxNode node, IEnumerable <TextSpan>?spans, Workspace workspace, OptionSet?options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken)
        {
            if (workspace == null)
            {
                throw new ArgumentNullException(nameof(workspace));
            }

            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            var languageFormatter = workspace.Services.GetLanguageServices(node.Language).GetService <ISyntaxFormattingService>();

            if (languageFormatter == null)
            {
                return(null);
            }

            options ??= workspace.Options;
            spans ??= SpecializedCollections.SingletonEnumerable(node.FullSpan);
            var formattingOptions = SyntaxFormattingOptions.Create(options, workspace.Services, node.Language);

            return(languageFormatter.GetFormattingResult(node, spans, formattingOptions, rules, cancellationToken));
        }
Esempio n. 2
0
        private void AnalyzeSyntaxTree(SyntaxTreeAnalysisContext context)
        {
            if (context.Options is not WorkspaceAnalyzerOptions workspaceAnalyzerOptions)
            {
                return;
            }

            var tree = context.Tree;
            var cancellationToken = context.CancellationToken;
            var optionSet         = context.Options.GetAnalyzerOptionSet(tree, cancellationToken);
            var options           = SyntaxFormattingOptions.Create(optionSet, workspaceAnalyzerOptions.Services, tree.Options.Language);

            FormattingAnalyzerHelper.AnalyzeSyntaxTree(context, workspaceAnalyzerOptions.Services, Descriptor, options);
        }
Esempio n. 3
0
        /// <summary>
        /// Formats the whitespace in areas of a document corresponding to multiple non-overlapping spans.
        /// </summary>
        /// <param name="document">The document to format.</param>
        /// <param name="spans">The spans of the document's text to format.</param>
        /// <param name="options">An optional set of formatting options. If these options are not supplied the current set of options from the document's workspace will be used.</param>
        /// <param name="cancellationToken">An optional cancellation token.</param>
        /// <returns>The formatted document.</returns>
        public static async Task <Document> FormatAsync(Document document, IEnumerable <TextSpan>?spans, OptionSet?options = null, CancellationToken cancellationToken = default)
        {
            var formattingService = document.GetLanguageService <IFormattingService>();

            if (formattingService == null)
            {
                return(document);
            }

            options ??= await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            var services          = document.Project.Solution.Workspace.Services;
            var formattingOptions = SyntaxFormattingOptions.Create(options, services, document.Project.Language);

            return(await formattingService.FormatAsync(document, spans, formattingOptions, cancellationToken).ConfigureAwait(false));
        }
Esempio n. 4
0
        internal static async Task <Document> FormatAsync(Document document, SyntaxAnnotation annotation, OptionSet?options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (annotation == null)
            {
                throw new ArgumentNullException(nameof(annotation));
            }

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

            var documentOptions = options ?? await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            var services          = document.Project.Solution.Workspace.Services;
            var formattingOptions = SyntaxFormattingOptions.Create(documentOptions, services, root.Language);

            return(document.WithSyntaxRoot(Format(root, annotation, services, formattingOptions, rules, cancellationToken)));
        }
        public Task <Document> FormatAsync(Document document, IEnumerable <TextSpan>?spans, OptionSet options, CancellationToken cancellationToken)
        {
            var formattingOptions = SyntaxFormattingOptions.Create(options, document.Project.Solution.Workspace.Services, document.Project.Language);

            return(Formatter.FormatAsync(document, spans, formattingOptions, rules: null, cancellationToken));
        }