AbstractCommentSelectionBase.cs 9.4 KB
Newer Older
1 2 3
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Collections.Generic;
4 5 6
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
D
David Barbet 已提交
7
using System.Threading.Tasks;
8 9 10 11 12 13 14 15 16 17 18 19 20 21
using Microsoft.CodeAnalysis.CommentSelection;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Operations;
using Roslyn.Utilities;
using VSCommanding = Microsoft.VisualStudio.Commanding;

namespace Microsoft.CodeAnalysis.Editor.Implementation.CommentSelection
{
D
David Barbet 已提交
22 23 24 25 26 27 28 29 30 31
    internal enum Operation
    {
        /// <summary>
        /// The operation is a comment action.
        /// </summary>
        Comment,

        /// <summary>
        /// The operation is an uncomment action.
        /// </summary>
D
David Barbet 已提交
32
        Uncomment
D
David Barbet 已提交
33
    }
34

D
David Barbet 已提交
35
    internal abstract class AbstractCommentSelectionBase<TCommand>
36
    {
D
David Barbet 已提交
37 38
        private readonly ITextUndoHistoryRegistry _undoHistoryRegistry;
        private readonly IEditorOperationsFactoryService _editorOperationsFactoryService;
39

D
David Barbet 已提交
40
        internal AbstractCommentSelectionBase(
41 42 43 44 45 46 47 48 49 50
            ITextUndoHistoryRegistry undoHistoryRegistry,
            IEditorOperationsFactoryService editorOperationsFactoryService)
        {
            Contract.ThrowIfNull(undoHistoryRegistry);
            Contract.ThrowIfNull(editorOperationsFactoryService);

            _undoHistoryRegistry = undoHistoryRegistry;
            _editorOperationsFactoryService = editorOperationsFactoryService;
        }

D
David Barbet 已提交
51 52
        public abstract string DisplayName { get; }

D
David Barbet 已提交
53
        protected abstract string GetTitle(TCommand command);
D
David Barbet 已提交
54

D
David Barbet 已提交
55
        protected abstract string GetMessage(TCommand command);
D
David Barbet 已提交
56

D
David Barbet 已提交
57
        // Internal as tests currently rely on this method.
D
David Barbet 已提交
58 59
        internal abstract Task<CommentSelectionResult> CollectEdits(
            Document document, ICommentSelectionService service, NormalizedSnapshotSpanCollection selectedSpans,
D
David Barbet 已提交
60
            TCommand command, CancellationToken cancellationToken);
D
David Barbet 已提交
61

62 63
        protected static VSCommanding.CommandState GetCommandState(ITextBuffer buffer)
        {
D
David Barbet 已提交
64 65 66
            return buffer.CanApplyChangeDocumentToWorkspace()
                ? VSCommanding.CommandState.Available
                : VSCommanding.CommandState.Unspecified;
67 68
        }

69 70 71 72 73 74 75 76 77 78
        protected static void InsertText(List<TextChange> textChanges, int position, string text)
        {
            textChanges.Add(new TextChange(new TextSpan(position, 0), text));
        }

        protected static void DeleteText(List<TextChange> textChanges, TextSpan span)
        {
            textChanges.Add(new TextChange(span, string.Empty));
        }

D
David Barbet 已提交
79
        internal bool ExecuteCommand(ITextView textView, ITextBuffer subjectBuffer, TCommand command, CommandExecutionContext context)
80
        {
D
David Barbet 已提交
81 82
            var title = GetTitle(command);
            var message = GetMessage(command);
83

D
David Barbet 已提交
84
            using (context.OperationContext.AddScope(allowCancellation: true, message))
85
            {
D
David Barbet 已提交
86 87 88 89 90 91 92
                var cancellationToken = context.OperationContext.UserCancellationToken;

                var selectedSpans = textView.Selection.GetSnapshotSpansOnBuffer(subjectBuffer);
                if (selectedSpans.IsEmpty())
                {
                    return true;
                }
93 94 95 96 97 98 99 100 101 102 103 104 105

                var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();
                if (document == null)
                {
                    return true;
                }

                var service = GetService(document);
                if (service == null)
                {
                    return true;
                }

D
David Barbet 已提交
106
                var edits = CollectEdits(document, service, selectedSpans, command, cancellationToken).WaitAndGetResult(cancellationToken);
107

D
David Barbet 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
                ApplyEdits(document, textView, subjectBuffer, service, title, edits);
            }

            return true;
        }

        /// <summary>
        /// Applies the requested edits and sets the selection.
        /// This operation is not cancellable.
        /// </summary>
        private void ApplyEdits(Document document, ITextView textView, ITextBuffer subjectBuffer,
            ICommentSelectionService service, string title, CommentSelectionResult edits)
        {
            // Create tracking spans to track the text changes.
            var currentSnapshot = subjectBuffer.CurrentSnapshot;
            var trackingSpans = edits.TrackingSpans
                .Select(textSpan => (originalSpan: textSpan, trackingSpan: CreateTrackingSpan(edits.ResultOperation, currentSnapshot, textSpan.TrackingTextSpan)))
                .ToImmutableList();

            // Apply the text changes.
            using (var transaction = new CaretPreservingEditTransaction(title, textView, _undoHistoryRegistry, _editorOperationsFactoryService))
            {
                document.Project.Solution.Workspace.ApplyTextChanges(document.Id, edits.TextChanges.Distinct(), CancellationToken.None);
                transaction.Complete();
            }
133

D
David Barbet 已提交
134 135 136 137 138
            // Convert the tracking spans into snapshot spans for formatting and selection.
            var trackingSnapshotSpans = trackingSpans.Select(s => CreateSnapshotSpan(subjectBuffer.CurrentSnapshot, s.trackingSpan, s.originalSpan));

            if (trackingSnapshotSpans.Any())
            {
D
David Barbet 已提交
139
                if (edits.ResultOperation == Operation.Uncomment)
140
                {
D
David Barbet 已提交
141 142 143
                    // Format the document only during uncomment operations.  Use second transaction so it can be undone.
                    using (var transaction = new CaretPreservingEditTransaction(title, textView, _undoHistoryRegistry, _editorOperationsFactoryService))
                    {
D
David Barbet 已提交
144
                        Format(service, subjectBuffer.CurrentSnapshot, trackingSnapshotSpans, CancellationToken.None);
D
David Barbet 已提交
145 146
                        transaction.Complete();
                    }
147 148
                }

D
David Barbet 已提交
149
                // Set the selection after the edits have been applied.
D
David Barbet 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
                var spansToSelect = trackingSnapshotSpans.Select(s => new Selection(s));
                textView.GetMultiSelectionBroker().SetSelectionRange(spansToSelect, spansToSelect.Last());
            }
        }

        /// <summary>
        /// Creates a tracking span for the operation.
        /// Internal for tests.
        /// </summary>
        internal static ITrackingSpan CreateTrackingSpan(Operation operation, ITextSnapshot snapshot, TextSpan textSpan)
        {
            var spanTrackingMode = operation == Operation.Comment
                ? SpanTrackingMode.EdgeInclusive
                : SpanTrackingMode.EdgeExclusive;
            return snapshot.CreateTrackingSpan(Span.FromBounds(textSpan.Start, textSpan.End), spanTrackingMode);
        }

        /// <summary>
        /// Retrieves the snapshot span from a post edited tracking span.
        /// Additionally applies any extra modifications to the tracking span post edit.
        /// Internal for tests.
        /// </summary>
        private static SnapshotSpan CreateSnapshotSpan(ITextSnapshot snapshot, ITrackingSpan trackingSpan, CommentTrackingSpan originalSpan)
        {
            var snapshotSpan = trackingSpan.GetSpan(snapshot);
            if (originalSpan.HasPostApplyChanges())
            {
                var updatedStart = snapshotSpan.Start.Position + originalSpan.AmountToAddToStart;
                var updatedEnd = snapshotSpan.End.Position + originalSpan.AmountToAddToEnd;
                if (updatedStart >= snapshotSpan.Start.Position && updatedEnd <= snapshotSpan.End.Position)
180
                {
D
David Barbet 已提交
181
                    snapshotSpan = new SnapshotSpan(snapshot, Span.FromBounds(updatedStart, updatedEnd));
182 183 184
                }
            }

D
David Barbet 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198
            return snapshotSpan;
        }

        private static void Format(ICommentSelectionService service, ITextSnapshot snapshot, IEnumerable<SnapshotSpan> changes, CancellationToken cancellationToken)
        {
            var document = snapshot.GetOpenDocumentInCurrentContextWithChanges();
            if (document == null)
            {
                return;
            }

            var textSpans = changes.Select(change => change.Span.ToTextSpan()).ToImmutableArray();
            var newDocument = service.FormatAsync(document, textSpans, cancellationToken).WaitAndGetResult(cancellationToken);
            newDocument.Project.Solution.Workspace.ApplyDocumentChanges(newDocument, cancellationToken);
199 200
        }

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        private static ICommentSelectionService GetService(Document document)
        {
            // First, try to get the new service for comment selection.
            var service = document.GetLanguageService<ICommentSelectionService>();
            if (service != null)
            {
                return service;
            }

            // If we couldn't find one, fallback to the legacy service.
#pragma warning disable CS0618 // Type or member is obsolete
            var legacyService = document.GetLanguageService<ICommentUncommentService>();
#pragma warning restore CS0618 // Type or member is obsolete
            if (legacyService != null)
            {
                return new CommentSelectionServiceProxy(legacyService);
            }

            return null;
        }
221 222
    }
}