Exemplo n.º 1
0
        static void DeleteExistingDefinition(ITextSnapshot snapshot, Parser.Result result)
        {
            DTE2      dte  = Global.GetDTE2();
            ITextEdit edit = snapshot.TextBuffer.CreateEdit();

            try
            {
                ITextSnapshotLine currentLine;
                for (int i = result.StartLine - 1; i <= result.EndLine - 1; i++)
                {
                    currentLine = snapshot.GetLineFromLineNumber(i);
                    edit.Delete(currentLine.Start.Position, currentLine.LengthIncludingLineBreak);
                }

                //remove separating empty line if found
                if (snapshot.LineCount > result.EndLine)
                {
                    currentLine = snapshot.GetLineFromLineNumber(result.EndLine);
                    if (string.IsNullOrWhiteSpace(currentLine.GetText()))
                    {
                        edit.Delete(currentLine.Start.Position, currentLine.LengthIncludingLineBreak);
                    }
                }

                edit.Apply();
            }
            catch
            {
                edit.Cancel();
            }

            dte.ActiveDocument.Save();
        }
Exemplo n.º 2
0
        public override bool Capitalize()
        {
            int startPosition = _startPoint.CurrentPosition;

            if (IsEmpty)
            {
                int       endPosition   = _endPoint.CurrentPosition;
                TextRange currentWord   = _startPoint.GetCurrentWord();
                string    nextCharacter = _startPoint.GetNextCharacter();
                if (_startPoint.CurrentPosition == currentWord.GetStartPoint().CurrentPosition)
                {
                    nextCharacter = nextCharacter.ToUpper(CultureInfo.CurrentCulture);
                }
                else
                {
                    nextCharacter = nextCharacter.ToLower(CultureInfo.CurrentCulture);
                }
                if (!PrimitivesUtilities.Replace(TextBuffer.AdvancedTextBuffer, new Span(_startPoint.CurrentPosition, nextCharacter.Length), nextCharacter))
                {
                    return(false);
                }
                _endPoint.MoveTo(endPosition);
            }
            else
            {
                using (ITextEdit edit = TextBuffer.AdvancedTextBuffer.CreateEdit())
                {
                    TextRange currentWord = _startPoint.GetCurrentWord();

                    // If the current word extends past this range, go to the next word
                    if (currentWord.GetStartPoint().CurrentPosition < _startPoint.CurrentPosition)
                    {
                        currentWord = currentWord.GetEndPoint().GetNextWord();
                    }

                    while (currentWord.GetStartPoint().CurrentPosition < _endPoint.CurrentPosition)
                    {
                        string wordText     = currentWord.GetText();
                        string startElement = StringInfo.GetNextTextElement(wordText);
                        wordText = startElement.ToUpper(CultureInfo.CurrentCulture) + wordText.Substring(startElement.Length).ToLower(CultureInfo.CurrentCulture);
                        if (!edit.Replace(currentWord.AdvancedTextRange.Span, wordText))
                        {
                            edit.Cancel();
                            return(false);
                        }

                        currentWord = currentWord.GetEndPoint().GetNextWord();
                    }

                    edit.Apply();

                    if (edit.Canceled)
                    {
                        return(false);
                    }
                }
            }
            _startPoint.MoveTo(startPosition);
            return(true);
        }
Exemplo n.º 3
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();
                                }
                            }
                        }
                    }
                }
            }
Exemplo n.º 4
0
        /// <summary>
        /// Rollback all modified text in the document except specified <see cref="IDiffChange"/>.
        /// </summary>
        /// <param name="diffChange">Information about a specific difference between two sequences.</param>
        private void RollbackAllButThisChange(IDiffChange diffChange)
        {
            ITextEdit edit = _textView.TextBuffer.CreateEdit();
            Span      viewSpan;

            try
            {
                string modifiedRegionText = _marginCore.GetModifiedText(diffChange, false);
                string originalText       = _marginCore.GetOriginalText();

                edit.Delete(0, edit.Snapshot.Length);
                edit.Insert(0, originalText);
                ApplyEdit(edit, "Undo Modified Text");

                edit = _textView.TextBuffer.CreateEdit();

                ITextSnapshotLine startLine = edit.Snapshot.GetLineFromLineNumber(diffChange.OriginalStart);
                ITextSnapshotLine endLine   = edit.Snapshot.GetLineFromLineNumber(diffChange.OriginalEnd);
                int start  = startLine.Start.Position;
                int length = endLine.EndIncludingLineBreak.Position - start;

                switch (diffChange.ChangeType)
                {
                case DiffChangeType.Insert:
                    edit.Insert(start, modifiedRegionText);
                    viewSpan = new Span(start, modifiedRegionText.Length);
                    break;

                case DiffChangeType.Delete:
                    edit.Delete(start, length);
                    viewSpan = new Span(start, 0);
                    break;

                case DiffChangeType.Change:
                    edit.Replace(start, length, modifiedRegionText);
                    viewSpan = new Span(start, modifiedRegionText.Length);
                    break;

                default:
                    throw new InvalidEnumArgumentException();
                }

                ApplyEdit(edit, "Restore Modified Region");
            }
            catch (Exception)
            {
                edit.Cancel();
                throw;
            }

            var viewSnapshotSpan = new SnapshotSpan(_textView.TextSnapshot, viewSpan);

            _textView.ViewScroller.EnsureSpanVisible(viewSnapshotSpan, EnsureSpanVisibleOptions.AlwaysCenter);
        }
Exemplo n.º 5
0
        public override bool ToggleCase()
        {
            if (IsEmpty)
            {
                TextPoint nextPoint = _startPoint.Clone();
                nextPoint.MoveToNextCharacter();
                TextRange nextCharacter       = _startPoint.GetTextRange(nextPoint);
                string    nextCharacterString = nextCharacter.GetText();
                if (char.IsUpper(nextCharacterString, 0))
                {
                    nextCharacterString = nextCharacterString.ToLower(CultureInfo.CurrentCulture);
                }
                else
                {
                    nextCharacterString = nextCharacterString.ToUpper(CultureInfo.CurrentCulture);
                }
                return(nextCharacter.ReplaceText(nextCharacterString));
            }
            else
            {
                int startPosition = _startPoint.CurrentPosition;
                using (ITextEdit textEdit = TextBuffer.AdvancedTextBuffer.CreateEdit())
                {
                    for (int i = _startPoint.CurrentPosition; i < _endPoint.CurrentPosition; i++)
                    {
                        char newChar = textEdit.Snapshot[i];
                        if (char.IsUpper(newChar))
                        {
                            newChar = char.ToLower(newChar, CultureInfo.CurrentCulture);
                        }
                        else
                        {
                            newChar = char.ToUpper(newChar, CultureInfo.CurrentCulture);
                        }

                        if (!textEdit.Replace(i, 1, newChar.ToString(CultureInfo.CurrentCulture)))
                        {
                            textEdit.Cancel();
                            return(false); // break out early if any edit fails to reduce the time of the failure case
                        }
                    }

                    textEdit.Apply();

                    if (textEdit.Canceled)
                    {
                        return(false);
                    }
                }
                _startPoint.MoveTo(startPosition);
            }
            return(true);
        }
        public override bool TransposeLine(int lineNumber)
        {
            if ((lineNumber < 0) || (lineNumber > _textBuffer.AdvancedTextBuffer.CurrentSnapshot.LineCount))
            {
                throw new ArgumentOutOfRangeException(nameof(lineNumber));
            }

            ITextSnapshot currentSnapshot = _textBuffer.AdvancedTextBuffer.CurrentSnapshot;

            // If there is only a single line in this buffer, don't do anything.
            if (currentSnapshot.LineCount <= 1)
            {
                return(true);
            }

            ITextSnapshotLine currentLine = currentSnapshot.GetLineFromPosition(CurrentPosition);
            ITextSnapshotLine nextLine    = currentSnapshot.GetLineFromLineNumber(lineNumber);

            using (ITextEdit edit = _textBuffer.AdvancedTextBuffer.CreateEdit())
            {
                // Make sure that both replaces succeed before applying
                if ((edit.Replace(currentLine.Extent, nextLine.GetText())) &&
                    (edit.Replace(nextLine.Extent, currentLine.GetText())))
                {
                    edit.Apply();
                    if (edit.Canceled)
                    {
                        return(false);
                    }
                }
                else
                {
                    edit.Cancel();
                    return(false);
                }
            }

            int newLineNumber = nextLine.LineNumber;

            if (currentLine.LineNumber == currentSnapshot.LineCount - 1)
            {
                newLineNumber = currentLine.LineNumber;
            }

            MoveTo(_textBuffer.AdvancedTextBuffer.CurrentSnapshot.GetLineFromLineNumber(newLineNumber).Start);

            return(true);
        }
Exemplo n.º 7
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();
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Rollback modified text.
        /// </summary>
        /// <param name="diffChange">Information about a specific difference between two sequences.</param>
        private void RollbackChange(IDiffChange diffChange)
        {
            ITextEdit edit = _textView.TextBuffer.CreateEdit();

            try
            {
                ITextSnapshot     snapshot  = edit.Snapshot;
                ITextSnapshotLine startLine = snapshot.GetLineFromLineNumber(diffChange.ModifiedStart);
                ITextSnapshotLine endLine   = snapshot.GetLineFromLineNumber(diffChange.ModifiedEnd);

                int start;
                if (diffChange.ChangeType != DiffChangeType.Delete)
                {
                    start = startLine.Start.Position;
                    int length = endLine.EndIncludingLineBreak.Position - start;
                    edit.Delete(start, length);
                }
                else
                {
                    if (startLine.LineNumber == 0 && endLine.LineNumber == 0)
                    {
                        start = startLine.Start.Position;
                    }
                    else
                    {
                        start = startLine.EndIncludingLineBreak.Position;
                    }
                }

                if (diffChange.ChangeType != DiffChangeType.Insert)
                {
                    string text = _marginCore.GetOriginalText(diffChange, false);
                    edit.Insert(start, text);
                }

                ApplyEdit(edit, "Rollback Modified Region");
            }
            catch (Exception)
            {
                edit.Cancel();
                throw;
            }
        }
Exemplo n.º 9
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();
                        }
                    }
                }
            }
        }
Exemplo n.º 10
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();
                }
            }
Exemplo n.º 11
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();
            }
        }