public virtual NormalizedSnapshotSpanCollection UncommentSpans([NotNull] NormalizedSnapshotSpanCollection spans) { Requires.NotNull(spans, nameof(spans)); List <SnapshotSpan> result = new List <SnapshotSpan>(); if (spans.Count == 0) { return(new NormalizedSnapshotSpanCollection()); } var undoHistory = TextUndoHistoryRegistry.RegisterHistory(TextView); using (var transaction = undoHistory.CreateTransaction("Uncomment Selection")) { ITextSnapshot snapshot = spans[0].Snapshot; using (var edit = snapshot.TextBuffer.CreateEdit()) { foreach (var span in spans) { var selection = UncommentSpan(span, edit); result.Add(selection); } edit.Apply(); } if (snapshot != TextView.TextSnapshot) { transaction.Complete(); } } if (result.Count > 1) { result.RemoveAll(span => span.IsEmpty); } var target = TextView.TextBuffer.CurrentSnapshot; for (int i = 0; i < result.Count; i++) { result[i] = result[i].TranslateTo(target, SpanTrackingMode.EdgeInclusive); } return(new NormalizedSnapshotSpanCollection(result)); }
public void Execute(ITextView view, IEmacsCommandMetadata metadata, bool evaluateUniversalArgument = true) { try { changes = new StringBuilder(); view.TextBuffer.Changed += OnTextBufferChanged; EmacsCommand command = CreateCommand(metadata); EmacsCommand emacsCommand = null; IEmacsCommandMetadata metadata1 = null; var context = new EmacsCommandContext(this, TextStructureNavigatorSelectorService, EditorOperationsFactoryService.GetEditorOperations(view), view, CommandRouterProvider.GetCommandRouter(view)); if (ClipboardRing.Count == 0 || ClipboardRing.Last() != Clipboard.GetText()) { ClipboardRing.Add(Clipboard.GetText()); } if (command == null) { return; } ITextUndoHistory history = TextUndoHistoryRegistry.GetHistory(context.TextBuffer); using (ITextUndoTransaction transaction = CreateTransaction(metadata, history)) { int num = 1; bool flag = false; if (evaluateUniversalArgument) { flag = GetUniversalArgumentOrDefault(0) < 0; if (flag) { metadata1 = GetCommandMetadata(metadata.InverseCommand); emacsCommand = CreateCommand(metadata1); } if (metadata.CanBeRepeated) { num = Math.Abs(GetUniversalArgumentOrDefault(1)); } } for (; num > 0; --num) { if (flag) { if (emacsCommand != null) { emacsCommand.Execute(context); } else { command.ExecuteInverse(context); } } else { command.Execute(context); } } if (transaction != null) { transaction.Complete(); } if (context.Clipboard.Length > 0) { ClipboardRing.Add(context.Clipboard.ToString()); Clipboard.SetText(context.Clipboard.ToString()); } else if (changes.Length > 0 && metadata.CopyDeletedTextToTheClipboard) { ClipboardRing.Add(changes.ToString()); Clipboard.SetText(changes.ToString()); } LastExecutedCommand = flag ? metadata1 : metadata; } } catch (NoOperationException ex) { } finally { view.TextBuffer.Changed -= OnTextBufferChanged; } }