protected override void FormatAndApply(Document document, int position, CancellationToken cancellationToken)
        {
            var root = document.GetSyntaxRootSynchronously(cancellationToken);

            var endToken = root.FindToken(position);

            if (endToken.IsMissing)
            {
                return;
            }

            var ranges = FormattingRangeHelper.FindAppropriateRange(endToken, useDefaultRange: false);

            if (ranges == null)
            {
                return;
            }

            var startToken = ranges.Value.Item1;

            if (startToken.IsMissing || startToken.Kind() == SyntaxKind.None)
            {
                return;
            }

            var options = document.GetOptionsAsync(cancellationToken).WaitAndGetResult(cancellationToken);

            var changes = Formatter.GetFormattedTextChanges(root, new TextSpan[] { TextSpan.FromBounds(startToken.SpanStart, endToken.Span.End) }, document.Project.Solution.Workspace, options,
                                                            rules: null, // use default
                                                            cancellationToken: cancellationToken);

            document.ApplyTextChanges(changes.ToArray(), cancellationToken);
        }
        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);
        }
Ejemplo n.º 3
0
        async void CompletionWindowManager_WindowClosed(object sender, EventArgs e)
        {
            var document = DocumentContext.AnalysisDocument;

            if (document == null)
            {
                return;
            }
            var caretPosition = Editor.CaretOffset;
            var token         = await CSharpEditorFormattingService.GetTokenBeforeTheCaretAsync(document, caretPosition, default(CancellationToken)).ConfigureAwait(false);

            if (token.IsMissing || !token.Parent.IsKind(SyntaxKind.ElseDirectiveTrivia))
            {
                return;
            }
            var tokenRange = FormattingRangeHelper.FindAppropriateRange(token);

            if (tokenRange == null || !tokenRange.HasValue || tokenRange.Value.Item1.Equals(tokenRange.Value.Item2))
            {
                return;
            }

            var value = tokenRange.Value;

            using (var undo = Editor.OpenUndoGroup()) {
                OnTheFlyFormatter.Format(Editor, DocumentContext, value.Item1.SpanStart, value.Item2.Span.End, optionSet: optionSet);
            }
        }
        private static async Task <ImmutableArray <TextChange> > FormatRangeAsync(
            Document document,
            OptionSet options,
            SyntaxToken endToken,
            IEnumerable <AbstractFormattingRule> formattingRules,
            CancellationToken cancellationToken)
        {
            if (!IsEndToken(endToken))
            {
                return(ImmutableArray <TextChange> .Empty);
            }

            var tokenRange = FormattingRangeHelper.FindAppropriateRange(endToken);

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

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

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

            var formatter = new CSharpSmartTokenFormatter(options, formattingRules, (CompilationUnitSyntax)root);

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

            return(changes.ToImmutableArray());
        }
        async void FormatOnReturn(CancellationToken cancellationToken = default(CancellationToken))
        {
            var document = DocumentContext.AnalysisDocument;

            if (document == null)
            {
                return;
            }
            var caretPosition = Editor.CaretOffset;
            var token         = await CSharpEditorFormattingService.GetTokenBeforeTheCaretAsync(document, caretPosition, cancellationToken).ConfigureAwait(false);

            if (token.IsMissing)
            {
                return;
            }

            string text = null;

            if (service.IsInvalidToken(token, ref text))
            {
                return;
            }
            // Check to see if the token is ')' and also the parent is a using statement. If not, bail
            if (CSharpEditorFormattingService.TokenShouldNotFormatOnReturn(token))
            {
                return;
            }
            var tokenRange = FormattingRangeHelper.FindAppropriateRange(token);

            if (tokenRange == null || !tokenRange.HasValue || tokenRange.Value.Item1.Equals(tokenRange.Value.Item2))
            {
                return;
            }
            var value = tokenRange.Value;

            using (var undo = Editor.OpenUndoGroup()) {
                OnTheFlyFormatter.Format(Editor, DocumentContext, value.Item1.SpanStart, value.Item2.Span.End, optionSet: optionSet);
            }
        }