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 )); }
private async Task <Document> CleanUpNewLinesAsync(Document document, TextSpan insertSpan, ISyntaxFormattingService languageFormatter, OptionSet optionSet, CancellationToken cancellationToken) { var root = await document.GetSyntaxRootAsync(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(); // 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)); }