diff --git a/src/EditorFeatures/Core/Implementation/CommentSelection/CommentUncommentSelectionCommandHandler.cs b/src/EditorFeatures/Core/Implementation/CommentSelection/CommentUncommentSelectionCommandHandler.cs index adf3ef6e7c3949cd337a519a44648b8d363c4475..889bca842bf567776de850af75bd37426c5632ee 100644 --- a/src/EditorFeatures/Core/Implementation/CommentSelection/CommentUncommentSelectionCommandHandler.cs +++ b/src/EditorFeatures/Core/Implementation/CommentSelection/CommentUncommentSelectionCommandHandler.cs @@ -16,6 +16,7 @@ using Microsoft.CodeAnalysis.Text.Shared.Extensions; using Microsoft.VisualStudio.Text; using Microsoft.VisualStudio.Text.Editor; +using Microsoft.VisualStudio.Text.Operations; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Editor.Implementation.CommentSelection @@ -26,14 +27,22 @@ internal class CommentUncommentSelectionCommandHandler : ICommandHandler { private readonly IWaitIndicator _waitIndicator; + private readonly ITextUndoHistoryRegistry _undoHistoryRegistry; + private readonly IEditorOperationsFactoryService _editorOperationsFactoryService; [ImportingConstructor] internal CommentUncommentSelectionCommandHandler( - IWaitIndicator waitIndicator) + IWaitIndicator waitIndicator, + ITextUndoHistoryRegistry undoHistoryRegistry, + IEditorOperationsFactoryService editorOperationsFactoryService) { Contract.ThrowIfNull(waitIndicator); + Contract.ThrowIfNull(undoHistoryRegistry); + Contract.ThrowIfNull(editorOperationsFactoryService); _waitIndicator = waitIndicator; + _undoHistoryRegistry = undoHistoryRegistry; + _editorOperationsFactoryService = editorOperationsFactoryService; } private static CommandState GetCommandState(ITextBuffer buffer, Func nextHandler) @@ -103,11 +112,19 @@ internal void ExecuteCommand(ITextView textView, ITextBuffer subjectBuffer, Oper CollectEdits(service, textView.Selection.GetSnapshotSpansOnBuffer(subjectBuffer), textChanges, trackingSpans, operation); - document.Project.Solution.Workspace.ApplyTextChanges(document.Id, textChanges, waitContext.CancellationToken); + using (var transaction = new CaretPreservingEditTransaction(title, textView, _undoHistoryRegistry, _editorOperationsFactoryService)) + { + document.Project.Solution.Workspace.ApplyTextChanges(document.Id, textChanges, waitContext.CancellationToken); + transaction.Complete(); + } if (operation == Operation.Uncomment) { - Format(service, subjectBuffer.CurrentSnapshot, trackingSpans, waitContext.CancellationToken); + using (var transaction = new CaretPreservingEditTransaction(title, textView, _undoHistoryRegistry, _editorOperationsFactoryService)) + { + Format(service, subjectBuffer.CurrentSnapshot, trackingSpans, waitContext.CancellationToken); + transaction.Complete(); + } } if (trackingSpans.Any()) @@ -118,6 +135,16 @@ internal void ExecuteCommand(ITextView textView, ITextBuffer subjectBuffer, Oper }); } + private static IEnumerable GetUpdatedSpans(IEnumerable ranges) + { + int delta = 0; + foreach (var range in ranges) + { + yield return new TextSpan(range.Span.Start + delta, range.NewLength); + delta += (range.NewLength - range.Span.Length); + } + } + private void Format(ICommentUncommentService service, ITextSnapshot snapshot, IEnumerable changes, CancellationToken cancellationToken) { var document = snapshot.GetOpenDocumentInCurrentContextWithChanges();