Exemplo n.º 1
0
        private static async Task TokenFormatWorkerAsync(TestWorkspace workspace, ITextBuffer buffer, int indentationLine, char ch)
        {
            Document document = buffer.CurrentSnapshot.GetRelatedDocumentsWithChanges().First();
            var      root     = (CompilationUnitSyntax)await document.GetSyntaxRootAsync();

            var line = root.GetText().Lines[indentationLine];

            var index = line.ToString().LastIndexOf(ch);

            Assert.InRange(index, 0, int.MaxValue);

            // get token
            var position = line.Start + index;
            var token    = root.FindToken(position);

            var formattingRuleProvider = workspace.Services.GetService <IHostDependentFormattingRuleFactoryService>();

            var rules = formattingRuleProvider.CreateRule(document, position).Concat(Formatter.GetDefaultFormattingRules(document));

            var documentOptions = await document.GetOptionsAsync();

            var formatter = new SmartTokenFormatter(documentOptions, rules, root);
            var changes   = await formatter.FormatTokenAsync(workspace, token, CancellationToken.None);

            ApplyChanges(buffer, changes);
        }
        private async Task <IList <TextChange> > FormatRangeAsync(
            Document document, SyntaxToken endToken, IEnumerable <IFormattingRule> formattingRules,
            CancellationToken cancellationToken)
        {
            if (!IsEndToken(endToken))
            {
                return(SpecializedCollections.EmptyList <TextChange>());
            }

            var tokenRange = FormattingRangeHelper.FindAppropriateRange(endToken);

            if (tokenRange == null || tokenRange.Value.Item1.Equals(tokenRange.Value.Item2))
            {
                return(SpecializedCollections.EmptyList <TextChange>());
            }

            if (IsInvalidTokenKind(tokenRange.Value.Item1) || IsInvalidTokenKind(tokenRange.Value.Item2))
            {
                return(SpecializedCollections.EmptyList <TextChange>());
            }

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

            var formatter = new SmartTokenFormatter(document.Project.Solution.Workspace.Options, formattingRules, (CompilationUnitSyntax)root);

            var changes = formatter.FormatRange(document.Project.Solution.Workspace, tokenRange.Value.Item1, tokenRange.Value.Item2, cancellationToken);

            return(changes);
        }