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

            if (syntaxFormattingService is null)
            {
                return(null);
            }

            options ??= CompilerAnalyzerConfigOptions.Empty;
            rules ??= GetDefaultFormattingRules(syntaxFormattingService);
            spans ??= SpecializedCollections.SingletonEnumerable(node.FullSpan);
            return(syntaxFormattingService.Format(
                       node,
                       spans,
                       shouldUseFormattingSpanCollapse: false,
                       options,
                       rules,
                       cancellationToken
                       ));
        }
Esempio n. 2
0
        internal static async Task <SyntaxTree> FormatAsync(
            SyntaxTree syntaxTree,
            ISyntaxFormattingService syntaxFormattingService,
            IEnumerable <TextSpan> spans,
            OptionSet options,
            IEnumerable <AbstractFormattingRule> rules,
            CancellationToken cancellationToken
            )
        {
            if (syntaxTree == null)
            {
                throw new ArgumentNullException(nameof(syntaxTree));
            }

            var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            var documentOptions = options ?? CompilerAnalyzerConfigOptions.Empty;

            return(syntaxTree.WithRootAndOptions(
                       Format(
                           root,
                           syntaxFormattingService,
                           spans,
                           documentOptions,
                           rules,
                           cancellationToken
                           ),
                       syntaxTree.Options
                       ));
        }
Esempio n. 3
0
        internal static IList <TextChange> GetFormattedTextChanges(SyntaxNode node, ISyntaxFormattingService syntaxFormattingService, IEnumerable <TextSpan> spans, OptionSet options, IEnumerable <AbstractFormattingRule> rules, CancellationToken cancellationToken)
        {
            var formattingResult = GetFormattingResult(node, syntaxFormattingService, spans, options, rules, cancellationToken);

            return(formattingResult == null
                ? SpecializedCollections.EmptyList <TextChange>()
                : formattingResult.GetTextChanges(cancellationToken));
        }
Esempio n. 4
0
        public static SyntaxNode Format(SyntaxNode node, ISyntaxFormattingService syntaxFormattingService, SyntaxAnnotation annotation, OptionSet options, IEnumerable <AbstractFormattingRule> rules, CancellationToken cancellationToken)
        {
            var spans = (annotation == SyntaxAnnotation.ElasticAnnotation)
                ? GetElasticSpans(node)
                : GetAnnotatedSpans(node, annotation);

            return(Format(node, syntaxFormattingService, spans, options, rules, cancellationToken: cancellationToken));
        }
Esempio n. 5
0
 /// <summary>
 /// Gets the formatting rules that would be applied if left unspecified.
 /// </summary>
 internal static IEnumerable <IFormattingRule> GetDefaultFormattingRules(ISyntaxFormattingService syntaxFormattingService)
 {
     if (syntaxFormattingService != null)
     {
         return(syntaxFormattingService.GetDefaultFormattingRules());
     }
     else
     {
         return(SpecializedCollections.EmptyEnumerable <IFormattingRule>());
     }
 }
        private static async Task <Document> CleanUpNewLinesAsync(
            Document document,
            TextSpan insertSpan,
            ISyntaxFormattingService languageFormatter,
            OptionSet optionSet,
            CancellationToken cancellationToken
            )
        {
            var root = await document
                       .GetRequiredSyntaxRootAsync(cancellationToken)
                       .ConfigureAwait(false);

            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var optionService =
                document.Project.Solution.Workspace.Services.GetRequiredService <IOptionService>();
            var shouldUseFormattingSpanCollapse = optionSet.GetOption(
                FormattingOptions.AllowDisjointSpanMerging
                );
            var options = optionSet.AsAnalyzerConfigOptions(optionService, root.Language);

            var textChanges = languageFormatter
                              .Format(
                root,
                new[] { insertSpan },
                shouldUseFormattingSpanCollapse,
                options,
                new[] { new CleanUpNewLinesFormatter(text) },
                cancellationToken
                )
                              .GetTextChanges(cancellationToken);

            // If there are no changes then, do less work.
            if (textChanges.Count == 0)
            {
                return(document);
            }

            // The last text change should include where the insert span ends
            Debug.Assert(textChanges.Last().Span.IntersectsWith(insertSpan.End));

            // If there are changes then, this was a case where there were no
            // previous imports statements. We need to retain the final extra
            // newline because that separates the imports section from the rest
            // of the code.
            textChanges.RemoveAt(textChanges.Count - 1);

            var newText = text.WithChanges(textChanges);

            return(document.WithText(newText));
        }
Esempio n. 7
0
 public static Task <SyntaxTree> FormatAsync(
     SyntaxTree syntaxTree,
     ISyntaxFormattingService syntaxFormattingService,
     TextSpan span,
     OptionSet options,
     CancellationToken cancellationToken
     ) =>
 FormatAsync(
     syntaxTree,
     syntaxFormattingService,
     SpecializedCollections.SingletonEnumerable(span),
     options,
     cancellationToken
     );
Esempio n. 8
0
 public static IList <TextChange> GetFormattedTextChanges(
     SyntaxNode node,
     ISyntaxFormattingService syntaxFormattingService,
     OptionSet options,
     CancellationToken cancellationToken
     ) =>
 GetFormattedTextChanges(
     node,
     syntaxFormattingService,
     SpecializedCollections.SingletonEnumerable(node.FullSpan),
     options,
     rules: null,
     cancellationToken: cancellationToken
     );
Esempio n. 9
0
 public static SyntaxNode Format(
     SyntaxNode node,
     ISyntaxFormattingService syntaxFormattingService,
     OptionSet options,
     CancellationToken cancellationToken
     ) =>
 Format(
     node,
     syntaxFormattingService,
     SpecializedCollections.SingletonEnumerable(node.FullSpan),
     options,
     rules: null,
     cancellationToken: cancellationToken
     );
Esempio n. 10
0
 public static Task <SyntaxTree> FormatAsync(
     SyntaxTree syntaxTree,
     ISyntaxFormattingService syntaxFormattingService,
     IEnumerable <TextSpan> spans,
     OptionSet options,
     CancellationToken cancellationToken
     ) =>
 FormatAsync(
     syntaxTree,
     syntaxFormattingService,
     spans,
     options,
     rules: null,
     cancellationToken
     );
Esempio n. 11
0
        internal static SyntaxNode Format(
            SyntaxNode node,
            ISyntaxFormattingService syntaxFormattingService,
            IEnumerable <TextSpan> spans,
            OptionSet options,
            IEnumerable <AbstractFormattingRule> rules,
            CancellationToken cancellationToken
            )
        {
            var formattingResult = GetFormattingResult(
                node,
                syntaxFormattingService,
                spans,
                options,
                rules,
                cancellationToken
                );

            return(formattingResult == null
              ? node
              : formattingResult.GetFormattedRoot(cancellationToken));
        }
Esempio n. 12
0
        private async Task <Document> CleanUpNewLinesAsync(Document document, TextSpan insertSpan, ISyntaxFormattingService languageFormatter, OptionSet options, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var textChanges = languageFormatter.Format(root, new[] { insertSpan }, options, new[] { new CleanUpNewLinesFormatter(text) }, cancellationToken).GetTextChanges();

            // If there are no changes then, do less work.
            if (textChanges.Count == 0)
            {
                return(document);
            }

            // The last text change should include where the insert span ends
            Debug.Assert(textChanges.Last().Span.IntersectsWith(insertSpan.End));

            // If there are changes then, this was a case where there were no
            // previous imports statements. We need to retain the final extra
            // newline because that separates the imports section from the rest
            // of the code.
            textChanges.RemoveAt(textChanges.Count - 1);

            var newText = text.WithChanges(textChanges);

            return(document.WithText(newText));
        }
Esempio n. 13
0
 /// <summary>
 /// Gets the formatting rules that would be applied if left unspecified.
 /// </summary>
 internal static IEnumerable <AbstractFormattingRule> GetDefaultFormattingRules(ISyntaxFormattingService syntaxFormattingService)
 => syntaxFormattingService.GetDefaultFormattingRules();
Esempio n. 14
0
 public static SyntaxNode Format(SyntaxNode node, TextSpan spanToFormat, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken)
 => Format(node, SpecializedCollections.SingletonEnumerable(spanToFormat), syntaxFormattingService, options, rules: null, cancellationToken: cancellationToken);
Esempio n. 15
0
 /// <summary>
 /// Formats the whitespace of a syntax tree.
 /// </summary>
 /// <param name="node">The root node of a syntax tree.</param>
 /// <param name="annotation">The descendant nodes of the root 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 workspace will be used.</param>
 /// <param name="cancellationToken">An optional cancellation token.</param>
 /// <returns>The formatted tree's root node.</returns>
 public static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken)
 => Format(node, GetAnnotatedSpans(node, annotation), syntaxFormattingService, options, rules, cancellationToken: cancellationToken);
Esempio n. 16
0
 internal static IFormattingResult GetFormattingResult(SyntaxNode node, IEnumerable <TextSpan> spans, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken)
 => syntaxFormattingService.GetFormattingResult(node, spans, options, rules, cancellationToken);
Esempio n. 17
0
 internal static IList <TextChange> GetFormattedTextChanges(SyntaxNode node, IEnumerable <TextSpan> spans, ISyntaxFormattingService syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken)
 => GetFormattingResult(node, spans, syntaxFormattingService, options, rules, cancellationToken).GetTextChanges(cancellationToken);