Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
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));
        }
            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));
            }
Exemplo n.º 4
0
            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;
            }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 7
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);
        }
 public AbstractFormatEngine(
     TreeData treeData,
     SyntaxFormattingOptions options,
     IEnumerable <AbstractFormattingRule> formattingRules,
     SyntaxToken startToken,
     SyntaxToken endToken)
     : this(
         treeData,
         options,
         new ChainedFormattingRules(formattingRules, options),
         startToken,
         endToken)
 {
 }
Exemplo n.º 9
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));
        }
            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;
            }
Exemplo n.º 11
0
        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);
            }
        }
Exemplo n.º 12
0
        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;
        }
Exemplo n.º 13
0
        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)));
        }
Exemplo n.º 14
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)));
        }
Exemplo n.º 15
0
        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)));
        }
Exemplo n.º 16
0
        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));
        }
Exemplo n.º 17
0
 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);
Exemplo n.º 18
0
 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);
Exemplo n.º 19
0
 public TriviaDataWithList(SyntaxFormattingOptions options, string language)
     : base(options, language)
 {
 }
Exemplo n.º 20
0
 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);
Exemplo n.º 21
0
 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);
Exemplo n.º 22
0
 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);
Exemplo n.º 23
0
 internal static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, HostWorkspaceServices services, SyntaxFormattingOptions options, CancellationToken cancellationToken)
 => Format(node, annotation, services, options, rules: null, cancellationToken);
Exemplo n.º 24
0
        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));
        }
Exemplo n.º 25
0
 protected abstract AbstractFormattingResult Format(SyntaxNode node, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule> rules, SyntaxToken startToken, SyntaxToken endToken, CancellationToken cancellationToken);
Exemplo n.º 26
0
 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);
Exemplo n.º 27
0
 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);
Exemplo n.º 28
0
 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);
Exemplo n.º 29
0
 /// <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);
Exemplo n.º 30
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, ISyntaxFormatting syntaxFormattingService, SyntaxFormattingOptions options, IEnumerable <AbstractFormattingRule>?rules, CancellationToken cancellationToken)
 => Format(node, GetAnnotatedSpans(node, annotation), syntaxFormattingService, options, rules, cancellationToken: cancellationToken);