예제 #1
0
            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 Cancel()
        {
            if (!_inTransaction)
            {
                throw new InvalidOperationException("The transaction is already complete");
            }

            _transaction?.Cancel();
            EndTransaction();
        }
예제 #3
0
        public void Cancel()
        {
            if (!_active)
            {
                throw new InvalidOperationException(EditorFeaturesResources.The_transaction_is_already_complete);
            }

            _transaction?.Cancel();

            EndTransaction();
        }
        public void Cancel()
        {
            if (!_active)
            {
                throw new InvalidOperationException(EditorFeaturesResources.TheTransactionIsAlreadyComplete);
            }

            if (_transaction != null)
            {
                _transaction.Cancel();
            }

            EndTransaction();
        }
        public void Cancel()
        {
            if (!_active)
            {
                throw new InvalidOperationException("This transaction is already compelte");
            }

            if (_transaction != null)
            {
                _transaction.Cancel();
            }

            EndTransaction();
        }
예제 #6
0
        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();
                            }
                        }
                    }
                }
            }
        }
예제 #7
0
        private void FormatDocument()
        {
            using (ITextUndoTransaction transaction = _undoManager.TextBufferUndoHistory.CreateTransaction(Resources.Text.FormatDocument))
            {
                EditorConfigFormatter formatter = _view.Properties.GetOrCreateSingletonProperty(() => new EditorConfigFormatter(_view.TextBuffer));
                bool changed = formatter.Format();

                if (changed)
                {
                    transaction.Complete();
                }
                else
                {
                    transaction.Cancel();
                }
            }
        }
예제 #8
0
        public void Dispose()
        {
            if (_undoTransaction != null)
            {
                if (_discardChanges)
                {
                    _undoTransaction.Cancel();
                }
                else
                {
                    _editorOperations.AddAfterTextBufferChangePrimitive();
                    _undoTransaction.Complete();

                    if (_undoAfterClose)
                    {
                        _undoManager.TextBufferUndoHistory.Undo(1);
                    }
                }
            }
        }
예제 #9
0
        public void Dispose()
        {
            IREditorDocument document = REditorDocument.TryFromTextBuffer(_textBuffer);
            bool             changed  = true;

            if (document != null)
            {
                changed = document.EndMassiveChange();
            }

            if (!changed)
            {
                _transaction.Cancel();
            }
            else
            {
                _transaction.AddUndo(new EndMassiveChangeUndoUnit(_textBuffer));
                _transaction.Complete();
            }

            _transaction.Dispose();
        }
예제 #10
0
        public void Dispose()
        {
            var document = _textBuffer.GetEditorDocument <IREditorDocument>();
            var changed  = true;

            if (document != null)
            {
                changed = document.EndMassiveChange();
            }

            if (!changed)
            {
                _transaction.Cancel();
            }
            else
            {
                _transaction.AddUndo(new EndMassiveChangeUndoUnit(_textBuffer));
                _transaction.Complete();
            }

            _transaction.Dispose();
        }
예제 #11
0
        public void PreBackspace(out bool handledCommand)
        {
            handledCommand = false;

            SnapshotPoint?caretPos = CaretPosition;
            ITextSnapshot snapshot = SubjectBuffer.CurrentSnapshot;

            if (caretPos.HasValue && caretPos.Value.Position > 0 && (caretPos.Value.Position - 1) == _openingPoint.GetPoint(snapshot).Position &&
                !HasForwardTyping)
            {
                using (ITextUndoTransaction undo = CreateUndoTransaction())
                {
                    using (ITextEdit edit = SubjectBuffer.CreateEdit())
                    {
                        SnapshotSpan span = new SnapshotSpan(_openingPoint.GetPoint(snapshot), _closingPoint.GetPoint(snapshot));

                        edit.Delete(span);

                        if (edit.HasFailedChanges)
                        {
                            edit.Cancel();
                            undo.Cancel();
                            Debug.Fail("Unable to clear braces");
                            // just let this backspace proceed normally
                        }
                        else
                        {
                            // handle the command so the backspace does
                            // not go through since we've already cleared the braces
                            handledCommand = true;
                            edit.Apply();
                            undo.Complete();
                            EndSession();
                        }
                    }
                }
            }
        }
예제 #12
0
            private void Start(CancellationToken cancellationToken)
            {
                // this is where the caret should go after the change
                SnapshotPoint  pos = TextView.Caret.Position.BufferPosition;
                ITrackingPoint beforeTrackingPoint = pos.Snapshot.CreateTrackingPoint(pos.Position, PointTrackingMode.Negative);

                ITextSnapshot snapshot             = SubjectBuffer.CurrentSnapshot;
                SnapshotPoint closingSnapshotPoint = ClosingPoint.GetPoint(snapshot);

                if (closingSnapshotPoint.Position < 1)
                {
                    Debug.Fail("The closing point was not found at the expected position.");
                    EndSession();
                    return;
                }

                SnapshotPoint openingSnapshotPoint = closingSnapshotPoint.Subtract(1);

                if (openingSnapshotPoint.GetChar() != OpeningBrace)
                {
                    // there is a bug in editor brace completion engine on projection buffer that already fixed in vs_pro. until that is FIed to use
                    // I will make this not to assert
                    // Debug.Fail("The opening brace was not found at the expected position.");
                    EndSession();
                    return;
                }

                OpeningPoint = snapshot.CreateTrackingPoint(openingSnapshotPoint, PointTrackingMode.Positive);
                var document = snapshot.GetOpenDocumentInCurrentContextWithChanges();

                if (!_session.CheckOpeningPoint(this, cancellationToken))
                {
                    EndSession();
                    return;
                }

                using (ITextUndoTransaction undo = CreateUndoTransaction())
                {
                    // insert the closing brace
                    using (ITextEdit edit = SubjectBuffer.CreateEdit())
                    {
                        edit.Insert(closingSnapshotPoint, ClosingBrace.ToString());

                        if (edit.HasFailedChanges)
                        {
                            Debug.Fail("Unable to insert closing brace");

                            // exit without setting the closing point which will take us off the stack
                            edit.Cancel();
                            undo.Cancel();
                            return;
                        }
                        else
                        {
                            snapshot = edit.Apply();
                        }
                    }

                    SnapshotPoint beforePoint = beforeTrackingPoint.GetPoint(TextView.TextSnapshot);

                    // switch from positive to negative tracking so it stays against the closing brace
                    ClosingPoint = SubjectBuffer.CurrentSnapshot.CreateTrackingPoint(ClosingPoint.GetPoint(snapshot), PointTrackingMode.Negative);

                    Debug.Assert(ClosingPoint.GetPoint(snapshot).Position > 0 && (new SnapshotSpan(ClosingPoint.GetPoint(snapshot).Subtract(1), 1))
                                 .GetText().Equals(ClosingBrace.ToString()), "The closing point does not match the closing brace character");

                    // move the caret back between the braces
                    TextView.Caret.MoveTo(beforePoint);

                    _session.AfterStart(this, cancellationToken);

                    undo.Complete();
                }
            }
예제 #13
0
        public void Start()
        {
            // this is where the caret should go after the change
            SnapshotPoint  pos = _textView.Caret.Position.BufferPosition;
            ITrackingPoint beforeTrackingPoint = pos.Snapshot.CreateTrackingPoint(pos.Position, PointTrackingMode.Negative);

            ITextSnapshot snapshot             = _subjectBuffer.CurrentSnapshot;
            SnapshotPoint closingSnapshotPoint = _closingPoint.GetPoint(snapshot);

            if (closingSnapshotPoint.Position < 1)
            {
                Debug.Fail("The closing point was not found at the expected position.");
                EndSession();
                return;
            }

            SnapshotPoint openingSnapshotPoint = closingSnapshotPoint.Subtract(1);

            if (openingSnapshotPoint.GetChar() != OpeningBrace)
            {
                Debug.Fail("The opening brace was not found at the expected position.");
                EndSession();
                return;
            }

            _openingPoint = SubjectBuffer.CurrentSnapshot.CreateTrackingPoint(openingSnapshotPoint, PointTrackingMode.Positive);

            using (ITextUndoTransaction undo = CreateUndoTransaction())
            {
                // insert the closing brace
                using (ITextEdit edit = _subjectBuffer.CreateEdit())
                {
                    edit.Insert(closingSnapshotPoint, _closingBrace.ToString(CultureInfo.CurrentCulture));

                    if (edit.HasFailedChanges)
                    {
                        Debug.Fail("Unable to insert closing brace");

                        // exit without setting the closing point which will take us off the stack
                        edit.Cancel();
                        undo.Cancel();
                        return;
                    }
                    else
                    {
                        snapshot = edit.Apply();
                    }
                }

                SnapshotPoint beforePoint = beforeTrackingPoint.GetPoint(_textView.TextSnapshot);

                // switch from positive to negative tracking so it stays against the closing brace
                _closingPoint = SubjectBuffer.CurrentSnapshot.CreateTrackingPoint(_closingPoint.GetPoint(snapshot), PointTrackingMode.Negative);

                Debug.Assert(_closingPoint.GetPoint(snapshot).Position > 0 && (new SnapshotSpan(_closingPoint.GetPoint(snapshot).Subtract(1), 1))
                             .GetText().Equals(_closingBrace.ToString(CultureInfo.CurrentCulture), System.StringComparison.Ordinal), "The closing point does not match the closing brace character");

                // move the caret back between the braces
                _textView.Caret.MoveTo(beforePoint);

                if (_context != null)
                {
                    // allow the context to do extra formatting
                    _context.Start(this);
                }

                undo.Complete();
            }
        }