提交 52d1c60b 编写于 作者: M Matt Warren

Use undo transaction so razor docs don't have redundant undos

上级 f0aba114
......@@ -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<UncommentSelectionCommandArgs>
{
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<CommandState> 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<TextSpan> GetUpdatedSpans(IEnumerable<TextChangeRange> 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<ITrackingSpan> changes, CancellationToken cancellationToken)
{
var document = snapshot.GetOpenDocumentInCurrentContextWithChanges();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册