bool TryFormat(IEditorFormattingService formattingService, char typedChar, int position, bool formatOnReturn, CancellationToken cancellationToken)
        {
            var document = DocumentContext.AnalysisDocument;
            IList <TextChange> changes;

            if (formatOnReturn)
            {
                if (!formattingService.SupportsFormatOnReturn)
                {
                    return(false);
                }
                changes = formattingService.GetFormattingChangesOnReturnAsync(document, position, cancellationToken).WaitAndGetResult(cancellationToken);
            }
            else
            {
                if (!formattingService.SupportsFormattingOnTypedCharacter(document, typedChar))
                {
                    return(false);
                }
                changes = formattingService.GetFormattingChangesAsync(document, typedChar, position, cancellationToken).WaitAndGetResult(cancellationToken);
            }

            if (changes == null || changes.Count == 0)
            {
                return(false);
            }

            Editor.ApplyTextChanges(changes);
            return(true);
        }
コード例 #2
0
        bool TryFormat(IEditorFormattingService formattingService, char typedChar, int position, bool formatOnReturn, CancellationToken cancellationToken)
        {
            var document = DocumentContext.AnalysisDocument;
            IEnumerable <TextChange> changes;

            if (formatOnReturn)
            {
                if (!formattingService.SupportsFormatOnReturn)
                {
                    return(false);
                }
                changes = formattingService.GetFormattingChangesOnReturnAsync(document, position, cancellationToken).WaitAndGetResult(cancellationToken);
            }
            else
            {
                if (!formattingService.SupportsFormattingOnTypedCharacter(document, typedChar))
                {
                    return(false);
                }
                changes = formattingService.GetFormattingChangesAsync(document, typedChar, position, cancellationToken).WaitAndGetResult(cancellationToken);
                var line = Editor.GetLineByOffset(position);
                if (typedChar == '#')
                {
                    changes = changes.Where(c => c.Span.Start >= line.Offset);
                }
            }

            if (changes == null)
            {
                return(false);
            }
            Editor.ApplyTextChanges(changes);
            return(true);
        }
コード例 #3
0
 protected virtual Task <IList <TextChange> > GetFormattingChangesAsync(
     IEditorFormattingService formattingService,
     Document document,
     TextSpan?textSpan,
     DocumentOptionSet documentOptions,
     CancellationToken cancellationToken)
 => formattingService.GetFormattingChangesAsync(document, textSpan, documentOptions, cancellationToken);
コード例 #4
0
 protected virtual Task <IList <TextChange>?> GetFormattingChangesAsync(
     IEditorFormattingService formattingService,
     Document document,
     char typedChar,
     int position,
     DocumentOptionSet documentOptions,
     CancellationToken cancellationToken)
 => formattingService.GetFormattingChangesAsync(document, typedChar, position, documentOptions, cancellationToken);
コード例 #5
0
        private bool TryFormat(
            ITextView textView, Document document, IEditorFormattingService formattingService, char typedChar, int position, bool formatOnReturn, CancellationToken cancellationToken)
        {
            var changes = formatOnReturn
                ? formattingService.GetFormattingChangesOnReturnAsync(document, position, cancellationToken).WaitAndGetResult(cancellationToken)
                : formattingService.GetFormattingChangesAsync(document, typedChar, position, cancellationToken).WaitAndGetResult(cancellationToken);

            if (changes == null || changes.Count == 0)
            {
                return false;
            }

            using (var transaction = CreateEditTransaction(textView, EditorFeaturesResources.Automatic_Formatting))
            {
                transaction.MergePolicy = AutomaticCodeChangeMergePolicy.Instance;
                document.Project.Solution.Workspace.ApplyTextChanges(document.Id, changes, cancellationToken);
                transaction.Complete();
            }

            return true;
        }
コード例 #6
0
        private bool TryFormat(
            ITextView textView, Document document, IEditorFormattingService formattingService, char typedChar, int position, bool formatOnReturn, CancellationToken cancellationToken)
        {
            var changes = formatOnReturn
                ? formattingService.GetFormattingChangesOnReturnAsync(document, position, cancellationToken).WaitAndGetResult(cancellationToken)
                : formattingService.GetFormattingChangesAsync(document, typedChar, position, cancellationToken).WaitAndGetResult(cancellationToken);

            if (changes == null || changes.Count == 0)
            {
                return(false);
            }

            using (var transaction = CreateEditTransaction(textView, EditorFeaturesResources.Automatic_Formatting))
            {
                transaction.MergePolicy = AutomaticCodeChangeMergePolicy.Instance;
                document.Project.Solution.Workspace.ApplyTextChanges(document.Id, changes, cancellationToken);
                transaction.Complete();
            }

            return(true);
        }
コード例 #7
0
 protected virtual Task <IList <TextChange> > GetFormattingChangesAsync(IEditorFormattingService formattingService, Document document, TextSpan?textSpan, CancellationToken cancellationToken)
 {
     return(formattingService.GetFormattingChangesAsync(document, textSpan, cancellationToken));
 }
コード例 #8
0
 public Task <IList <TextChange> > GetFormattingChangesAsync(Document document, TextSpan?textSpan, CancellationToken cancellationToken)
 => _underlyingObject.GetFormattingChangesAsync(document, textSpan, cancellationToken);