public void PreOverType(out bool handledCommand) { handledCommand = false; if (ClosingPoint == null) { return; } // Brace completion is not cancellable. var cancellationToken = CancellationToken.None; var snapshot = this.SubjectBuffer.CurrentSnapshot; var document = snapshot.GetOpenDocumentInCurrentContextWithChanges(); SnapshotPoint closingSnapshotPoint = ClosingPoint.GetPoint(snapshot); if (!HasForwardTyping && _session.AllowOverType(this, cancellationToken)) { SnapshotPoint?caretPos = this.GetCaretPosition(); Debug.Assert(caretPos.HasValue && caretPos.Value.Position < closingSnapshotPoint.Position); // ensure that we are within the session before clearing if (caretPos.HasValue && caretPos.Value.Position < closingSnapshotPoint.Position && closingSnapshotPoint.Position > 0) { using (ITextUndoTransaction undo = CreateUndoTransaction()) { _editorOperations.AddBeforeTextBufferChangePrimitive(); SnapshotSpan span = new SnapshotSpan(caretPos.Value, closingSnapshotPoint.Subtract(1)); using (ITextEdit edit = SubjectBuffer.CreateEdit()) { edit.Delete(span); if (edit.HasFailedChanges) { Debug.Fail("Unable to clear closing brace"); edit.Cancel(); undo.Cancel(); } else { handledCommand = true; edit.Apply(); MoveCaretToClosingPoint(); _editorOperations.AddAfterTextBufferChangePrimitive(); undo.Complete(); } } } } } }
public void PreOverType(out bool handledCommand) { handledCommand = false; // AllowOverType may make changes to the buffer such as for completing intellisense if (!HasForwardTyping && (_context == null || _context.AllowOverType(this))) { SnapshotPoint?caretPos = CaretPosition; SnapshotPoint closingSnapshotPoint = _closingPoint.GetPoint(SubjectBuffer.CurrentSnapshot); Debug.Assert(caretPos.HasValue && caretPos.Value.Position < closingSnapshotPoint.Position); // ensure that we are within the session before clearing if (caretPos.HasValue && caretPos.Value.Position < closingSnapshotPoint.Position && closingSnapshotPoint.Position > 0) { using (ITextUndoTransaction undo = CreateUndoTransaction()) { _editorOperations.AddBeforeTextBufferChangePrimitive(); SnapshotSpan span = new SnapshotSpan(caretPos.Value, closingSnapshotPoint.Subtract(1)); using (ITextEdit edit = _subjectBuffer.CreateEdit()) { edit.Delete(span); if (edit.HasFailedChanges) { Debug.Fail("Unable to clear closing brace"); edit.Cancel(); undo.Cancel(); } else { handledCommand = true; edit.Apply(); MoveCaretToClosingPoint(); _editorOperations.AddAfterTextBufferChangePrimitive(); undo.Complete(); } } } } } }
void PerformActionInUndo(Func <bool> action) { ITrackingPoint anchor = _textView.TextSnapshot.CreateTrackingPoint(_textView.Selection.AnchorPoint.Position, PointTrackingMode.Positive); ITrackingPoint active = _textView.TextSnapshot.CreateTrackingPoint(_textView.Selection.ActivePoint.Position, PointTrackingMode.Positive); bool empty = _textView.Selection.IsEmpty; TextSelectionMode mode = _textView.Selection.Mode; using (var undo = _undoHistory.CreateTransaction("Untabify")) { _operations.AddBeforeTextBufferChangePrimitive(); if (!action()) { undo.Cancel(); return; } ITextSnapshot after = _textView.TextSnapshot; _operations.SelectAndMoveCaret(new VirtualSnapshotPoint(anchor.GetPoint(after)), new VirtualSnapshotPoint(active.GetPoint(after)), mode, EnsureSpanVisibleOptions.ShowStart); _operations.AddAfterTextBufferChangePrimitive(); undo.Complete(); } }
private void DeleteSpan(NormalizedSnapshotSpanCollection applicabilitySpans) { using (ITextUndoTransaction undoTransaction = _undoHistory.CreateTransaction("HTML Cut")) { _editorOperations.AddBeforeTextBufferChangePrimitive(); bool successfulEdit = true; using (ITextEdit edit = _textView.TextBuffer.CreateEdit()) { foreach (SnapshotSpan span in applicabilitySpans) { successfulEdit &= edit.Delete(span); } if (successfulEdit) { edit.Apply(); } } _editorOperations.AddAfterTextBufferChangePrimitive(); if (successfulEdit) { undoTransaction.Complete(); } } }
public void Commit() { if (!_inTransaction) { throw new InvalidOperationException("The transaction is already complete"); } _editorOperations?.AddAfterTextBufferChangePrimitive(); _transaction?.Complete(); EndTransaction(); }
public void Complete() { if (!_active) { throw new InvalidOperationException(EditorFeaturesResources.The_transaction_is_already_complete); } _editorOperations.AddAfterTextBufferChangePrimitive(); _transaction?.Complete(); EndTransaction(); }
public void Complete() { if (!_active) { throw new InvalidOperationException(EditorFeaturesResources.TheTransactionIsAlreadyComplete); } _editorOperations.AddAfterTextBufferChangePrimitive(); if (_transaction != null) { _transaction.Complete(); } EndTransaction(); }
public void Complete() { if (!_active) { throw new InvalidOperationException("This transaction is already compelte"); } _editorOperations.AddAfterTextBufferChangePrimitive(); if (_transaction != null) { _transaction.Complete(); } EndTransaction(); }
private void TextBufferPostChanged(object sender, EventArgs e) { if (_createdTransaction != null) { #if false if (_initiatingOperations != null) { _initiatingOperations.AddAfterTextBufferChangePrimitive(); } _initiatingOperations = null; #endif _createdTransaction.Complete(); _createdTransaction.Dispose(); _createdTransaction = null; } }
public void Dispose() { if (_undoTransaction != null) { if (_discardChanges) { _undoTransaction.Cancel(); } else { _editorOperations.AddAfterTextBufferChangePrimitive(); _undoTransaction.Complete(); if (_undoAfterClose) { _undoManager.TextBufferUndoHistory.Undo(1); } } } }
/// <summary> /// Called by the editor when return is pressed while both braces are on the same line and no typing has occurred in the session. /// </summary> /// <param name="session">Current brace completion session.</param> public void OnReturn(IBraceCompletionSession session) { // Return in Repl window would just execute the current command if (session.SubjectBuffer.ContentType.TypeName.Equals(ReplConstants.ReplContentTypeName, StringComparison.OrdinalIgnoreCase)) { return; } var closingPointPosition = session.ClosingPoint.GetPosition(session.SubjectBuffer.CurrentSnapshot); Debug.Assert( condition: closingPointPosition > 0, message: "The closing point position should always be greater than zero", detailMessage: "The closing point position should always be greater than zero, " + "since there is also an opening point for this brace completion session"); // reshape code from // { // |} // // to // { // | // } // where | indicates caret position. using (var undo = _undoHistory.CreateTransaction("Insert new line.")) { _editorOperations.AddBeforeTextBufferChangePrimitive(); _editorOperations.MoveLineUp(false); _editorOperations.MoveToEndOfLine(false); _editorOperations.InsertNewLine(); _editorOperations.AddAfterTextBufferChangePrimitive(); undo.Complete(); } }
public void PreOverType(out bool handledCommand) { _threadingContext.ThrowIfNotOnUIThread(); handledCommand = false; if (ClosingPoint == null) { return; } // Brace completion is not cancellable. var cancellationToken = CancellationToken.None; var snapshot = this.SubjectBuffer.CurrentSnapshot; var closingSnapshotPoint = ClosingPoint.GetPoint(snapshot); if (!HasForwardTyping && AllowOverType()) { var caretPos = this.GetCaretPosition(); Debug.Assert(caretPos.HasValue && caretPos.Value.Position < closingSnapshotPoint.Position); // ensure that we are within the session before clearing if (caretPos.HasValue && caretPos.Value.Position < closingSnapshotPoint.Position && closingSnapshotPoint.Position > 0) { using var undo = CreateUndoTransaction(); _editorOperations.AddBeforeTextBufferChangePrimitive(); var span = new SnapshotSpan(caretPos.Value, closingSnapshotPoint.Subtract(1)); using var edit = SubjectBuffer.CreateEdit(); edit.Delete(span); if (edit.HasFailedChanges) { Debug.Fail("Unable to clear closing brace"); edit.Cancel(); undo.Cancel(); } else { handledCommand = true; edit.ApplyAndLogExceptions(); MoveCaretToClosingPoint(); _editorOperations.AddAfterTextBufferChangePrimitive(); undo.Complete(); } } } return; bool AllowOverType() { var context = GetBraceCompletionContext(); return(context != null && _service.AllowOverTypeAsync(context.Value, cancellationToken).WaitAndGetResult(cancellationToken)); } }