public TokenStream(TreeData treeData, SyntaxFormattingOptions options, TextSpan spanToFormat, AbstractTriviaDataFactory factory) { using (Logger.LogBlock(FunctionId.Formatting_TokenStreamConstruction, CancellationToken.None)) { // initialize basic info _factory = factory; _treeData = treeData; _options = options; // use some heuristics to get initial size of list rather than blindly start from default size == 4 var sizeOfList = spanToFormat.Length / MagicTextLengthToTokensRatio; _tokens = new SegmentedList <SyntaxToken>(sizeOfList); _tokens.AddRange(_treeData.GetApplicableTokens(spanToFormat)); Debug.Assert(this.TokenCount > 0); // initialize trivia related info _cachedOriginalTriviaInfo = new SegmentedArray <TriviaData>(this.TokenCount - 1); // Func Cache _getTriviaData = this.GetTriviaData; _getOriginalTriviaData = this.GetOriginalTriviaData; } DebugCheckTokenOrder(); }
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)); }
public FormattedWhitespace(SyntaxFormattingOptions options, int lineBreaks, int indentation, string language) : base(options, language) { this.LineBreaks = Math.Max(0, lineBreaks); this.Spaces = Math.Max(0, indentation); _newString = CreateString(this.Options.GetOption(FormattingOptions2.NewLine)); }
public Whitespace(SyntaxFormattingOptions options, int lineBreaks, int indentation, bool elastic, string language) : base(options, language) { _elastic = elastic; // space and line breaks can be negative during formatting. but at the end, should be normalized // to >= 0 this.LineBreaks = lineBreaks; this.Spaces = indentation; }
private static async Task <Document> FixOneAsync(CodeFixContext context, Diagnostic diagnostic, CancellationToken cancellationToken) { var options = await SyntaxFormattingOptions.FromDocumentAsync(context.Document, cancellationToken).ConfigureAwait(false); var tree = await context.Document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); var formattedTree = await FormattingCodeFixHelper.FixOneAsync(tree, context.Document.Project.Solution.Workspace.Services, options, diagnostic, cancellationToken).ConfigureAwait(false); return(context.Document.WithSyntaxRoot(await formattedTree.GetRootAsync(cancellationToken).ConfigureAwait(false))); }
protected AbstractTriviaDataFactory(TreeData treeInfo, SyntaxFormattingOptions options) { Contract.ThrowIfNull(treeInfo); TreeInfo = treeInfo; Options = options; _spaces = new Whitespace[SpaceCacheSize]; for (var i = 0; i < SpaceCacheSize; i++) { _spaces[i] = new Whitespace(Options, space: i, elastic: false, language: treeInfo.Root.Language); } }
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); }
public AbstractFormatEngine( TreeData treeData, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule> formattingRules, SyntaxToken startToken, SyntaxToken endToken) : this( treeData, options, new ChainedFormattingRules(formattingRules, options), startToken, endToken) { }
/// <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)); }
public AbstractComplexTrivia(SyntaxFormattingOptions options, TreeData treeInfo, SyntaxToken token1, SyntaxToken token2) : base(options, token1.Language) { Contract.ThrowIfNull(treeInfo); _token1 = token1; _token2 = token2; _treatAsElastic = CommonFormattingHelpers.HasAnyWhitespaceElasticTrivia(token1, token2); this.TreeInfo = treeInfo; this.OriginalString = this.TreeInfo.GetTextBetween(token1, token2); ExtractLineAndSpace(this.OriginalString, out var lineBreaks, out var spaces); this.LineBreaks = lineBreaks; this.Spaces = spaces; }
protected AbstractTriviaDataFactory(TreeData treeInfo, SyntaxFormattingOptions options) { Contract.ThrowIfNull(treeInfo); this.TreeInfo = treeInfo; this.Options = options; UseTabs = options.GetOption(FormattingOptions2.UseTabs); TabSize = options.GetOption(FormattingOptions2.TabSize); IndentationSize = options.GetOption(FormattingOptions2.IndentationSize); _spaces = new Whitespace[SpaceCacheSize]; for (var i = 0; i < SpaceCacheSize; i++) { _spaces[i] = new Whitespace(this.Options, space: i, elastic: false, language: treeInfo.Root.Language); } }
internal AbstractFormatEngine( TreeData treeData, SyntaxFormattingOptions options, ChainedFormattingRules formattingRules, SyntaxToken startToken, SyntaxToken endToken) { Contract.ThrowIfTrue(treeData.Root.IsInvalidTokenRange(startToken, endToken)); this.Options = options; this.TreeData = treeData; _formattingRules = formattingRules; _startToken = startToken; _endToken = endToken; // get span and common root this.SpanToFormat = GetSpanToFormat(); _commonRoot = startToken.GetCommonRoot(endToken) ?? throw ExceptionUtilities.Unreachable; }
public static async Task <SyntaxFormattingOptions> GetInferredFormattingOptionsAsync(this IIndentationManagerService indentationManager, Document document, bool explicitFormat, CancellationToken cancellationToken) { var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false); var snapshot = text.FindCorrespondingEditorTextSnapshot(); var options = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false); if (snapshot == null) { return(options); } indentationManager.GetIndentation(snapshot.TextBuffer, explicitFormat, out var convertTabsToSpaces, out var tabSize, out var indentSize); return(options.With(new LineFormattingOptions( UseTabs: !convertTabsToSpaces, IndentationSize: indentSize, TabSize: tabSize, NewLine: options.NewLine))); }
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))); }
internal static async Task <Document> FormatAsync(Document document, IEnumerable <TextSpan>?spans, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken) { var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var services = document.Project.Solution.Workspace.Services; return(document.WithSyntaxRoot(Format(root, spans, services, options, rules, cancellationToken))); }
internal static IList <TextChange> GetFormattedTextChanges(SyntaxNode node, IEnumerable <TextSpan>?spans, HostWorkspaceServices services, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken = default) { var formatter = services.GetRequiredLanguageService <ISyntaxFormattingService>(node.Language); return(formatter.GetFormattingResult(node, spans, options, rules, cancellationToken).GetTextChanges(cancellationToken)); }
internal static IList <TextChange> GetFormattedTextChanges(SyntaxNode node, IEnumerable <TextSpan>?spans, HostWorkspaceServices services, SyntaxFormattingOptions options, CancellationToken cancellationToken = default) => GetFormattedTextChanges(node, spans, services, options, rules: null, cancellationToken);
internal static IList <TextChange> GetFormattedTextChanges(SyntaxNode node, TextSpan span, HostWorkspaceServices services, SyntaxFormattingOptions options, CancellationToken cancellationToken = default) => GetFormattedTextChanges(node, SpecializedCollections.SingletonEnumerable(span), services, options, rules: null, cancellationToken);
public TriviaDataWithList(SyntaxFormattingOptions options, string language) : base(options, language) { }
internal static SyntaxNode Format(SyntaxNode node, IEnumerable <TextSpan>?spans, HostWorkspaceServices services, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken) => GetFormattingResult(node, spans, services, options, rules, cancellationToken).GetFormattedRoot(cancellationToken);
internal static IList <TextChange> GetFormattedTextChanges(SyntaxNode node, IEnumerable <TextSpan> spans, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken) => GetFormattingResult(node, spans, syntaxFormattingService, options, rules, cancellationToken).GetTextChanges(cancellationToken);
internal static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, HostWorkspaceServices services, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken) => Format(node, GetAnnotatedSpans(node, annotation), services, options, rules, cancellationToken);
internal static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, HostWorkspaceServices services, SyntaxFormattingOptions options, CancellationToken cancellationToken) => Format(node, annotation, services, options, rules: null, cancellationToken);
public IFormattingResult GetFormattingResult(SyntaxNode node, IEnumerable <TextSpan>?spans, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken) { IReadOnlyList <TextSpan> spansToFormat; if (spans == null) { spansToFormat = node.FullSpan.IsEmpty ? SpecializedCollections.EmptyReadOnlyList <TextSpan>() : SpecializedCollections.SingletonReadOnlyList(node.FullSpan); } else { spansToFormat = new NormalizedTextSpanCollection(spans.Where(s_notEmpty)); } if (spansToFormat.Count == 0) { return(CreateAggregatedFormattingResult(node, SpecializedCollections.EmptyList <AbstractFormattingResult>())); } rules ??= GetDefaultFormattingRules(); List <AbstractFormattingResult>?results = null; foreach (var(startToken, endToken) in node.ConvertToTokenPairs(spansToFormat)) { if (node.IsInvalidTokenRange(startToken, endToken)) { continue; } results ??= new List <AbstractFormattingResult>(); results.Add(Format(node, options, rules, startToken, endToken, cancellationToken)); } // quick simple case check if (results == null) { return(CreateAggregatedFormattingResult(node, SpecializedCollections.EmptyList <AbstractFormattingResult>())); } if (results.Count == 1) { return(results[0]); } // more expensive case return(CreateAggregatedFormattingResult(node, results)); }
protected abstract AbstractFormattingResult Format(SyntaxNode node, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule> rules, SyntaxToken startToken, SyntaxToken endToken, CancellationToken cancellationToken);
public static SyntaxNode Format(SyntaxNode node, TextSpan spanToFormat, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken) => Format(node, SpecializedCollections.SingletonEnumerable(spanToFormat), syntaxFormattingService, options, rules: null, cancellationToken: cancellationToken);
internal static IFormattingResult GetFormattingResult(SyntaxNode node, IEnumerable <TextSpan> spans, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken) => syntaxFormattingService.GetFormattingResult(node, spans, options, rules, cancellationToken);
internal static SyntaxNode Format(SyntaxNode node, TextSpan span, HostWorkspaceServices services, SyntaxFormattingOptions options, CancellationToken cancellationToken) => Format(node, SpecializedCollections.SingletonEnumerable(span), services, options, rules: null, cancellationToken: cancellationToken);
/// <summary> /// Determines the changes necessary to format the whitespace of a syntax tree. /// </summary> /// <param name="node">The root node of a syntax tree 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 changes necessary to format the tree.</returns> public static IList <TextChange> GetFormattedTextChanges(SyntaxNode node, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, CancellationToken cancellationToken) => GetFormattedTextChanges(node, SpecializedCollections.SingletonEnumerable(node.FullSpan), syntaxFormattingService, options, rules: null, cancellationToken: cancellationToken);
/// <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, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken) => Format(node, GetAnnotatedSpans(node, annotation), syntaxFormattingService, options, rules, cancellationToken: cancellationToken);