public override bool DeletePrevious()
        {
            ITextSnapshot snapshot         = _textBuffer.AdvancedTextBuffer.CurrentSnapshot;
            int           previousPosition = CurrentPosition - 1;

            if (previousPosition < 0)
            {
                return(true);
            }

            int  index            = previousPosition;
            char currentCharacter = snapshot[previousPosition];

            // By default VS (and many other apps) will delete only the last character
            // of a combining character sequence.  The one exception to this rule is
            // surrogate pais which we are handling here.
            if (char.GetUnicodeCategory(currentCharacter) == UnicodeCategory.Surrogate)
            {
                index--;
            }

            if (index > 0)
            {
                if (currentCharacter == '\n')
                {
                    if (snapshot[previousPosition - 1] == '\r')
                    {
                        index--;
                    }
                }
            }

            return(PrimitivesUtilities.Delete(_textBuffer.AdvancedTextBuffer, index, previousPosition - index + 1));
        }
예제 #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);
        }
 public override bool DeleteNext()
 {
     if (_textView.AdvancedTextView.TextViewModel.IsPointInVisualBuffer(AdvancedTextPoint, PositionAffinity.Successor))
     {
         return(PrimitivesUtilities.Delete(GetNextTextElementSpan()));
     }
     else
     {
         return(_bufferPoint.DeleteNext());
     }
 }
        public override bool InsertText(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (text.Length > 0)
            {
                return(PrimitivesUtilities.Insert(_textBuffer.AdvancedTextBuffer, _trackingPoint.GetPosition(_textBuffer.AdvancedTextBuffer.CurrentSnapshot), text));
            }
            else
            {
                return(true);
            }
        }
        public override bool TransposeCharacter()
        {
            int insertionIndex = CurrentPosition;

            ITextSnapshotLine line = _textBuffer.AdvancedTextBuffer.CurrentSnapshot.GetLineFromPosition(insertionIndex);

            string lineText = line.GetText();

            if (StringInfo.ParseCombiningCharacters(lineText).Length < 2)
            {
                return(true);
            }

            Span   textElementLeftSpan, textElementRightSpan;
            string textElementLeft, textElementRight;
            int    linePosition = CurrentPosition - StartOfLine;

            // We're at the start of a line
            if (insertionIndex == line.Start)
            {
                textElementLeft      = StringInfo.GetNextTextElement(lineText, linePosition);
                textElementLeftSpan  = new Span(linePosition + line.Start, textElementLeft.Length);
                textElementRight     = StringInfo.GetNextTextElement(lineText, linePosition + textElementLeft.Length);
                textElementRightSpan = new Span(textElementLeftSpan.End, textElementRight.Length);
            }
            // We're at the end of a line
            else if (insertionIndex == line.End)
            {
                textElementRight     = StringInfo.GetNextTextElement(lineText, linePosition - 1);
                textElementRightSpan = new Span(linePosition - 1 + line.Start, textElementRight.Length);
                textElementLeft      = StringInfo.GetNextTextElement(lineText, textElementRightSpan.Start - line.Start - 1);
                textElementLeftSpan  = new Span(textElementRightSpan.Start - 1, textElementRight.Length);
            }
            // We're at the middle of a line
            else
            {
                textElementRight     = StringInfo.GetNextTextElement(lineText, linePosition);
                textElementRightSpan = new Span(linePosition + line.Start, textElementRight.Length);
                textElementLeft      = StringInfo.GetNextTextElement(lineText, textElementRightSpan.Start - line.Start - 1);
                textElementLeftSpan  = new Span(textElementRightSpan.Start - 1, textElementRight.Length);
            }

            string transposedText = textElementRight + textElementLeft;

            return(PrimitivesUtilities.Replace(_textBuffer.AdvancedTextBuffer, new Span(textElementLeftSpan.Start, transposedText.Length), transposedText));
        }
예제 #6
0
        public override bool ReplaceText(string newText)
        {
            if (string.IsNullOrEmpty(newText))
            {
                throw new ArgumentNullException(nameof(newText));
            }

            int startPoint = _startPoint.CurrentPosition;

            if (!PrimitivesUtilities.Replace(TextBuffer.AdvancedTextBuffer, Span.FromBounds(_startPoint.CurrentPosition, _endPoint.CurrentPosition), newText))
            {
                return(false);
            }

            _startPoint.MoveTo(startPoint);

            return(true);
        }
        public override bool DeletePrevious()
        {
            SnapshotSpan previousElementSpan = GetPreviousTextElementSpan();

            if ((previousElementSpan.Length > 0) &&
                (_textView.AdvancedTextView.TextViewModel.IsPointInVisualBuffer(AdvancedTextPoint, PositionAffinity.Successor)) &&
                (!_textView.AdvancedTextView.TextViewModel.IsPointInVisualBuffer(previousElementSpan.End - 1, PositionAffinity.Successor)))
            {
                // Since the previous character is not visible but the current one is, delete
                // the entire previous text element span.
                return(PrimitivesUtilities.Delete(previousElementSpan));
            }
            else
            {
                // Delegate to the buffer point's DeletePrevious implementation to handle deleting single
                // characters.  A single character should be deleted if this point and the previous one
                // are both visible or both not visible.
                return(_bufferPoint.DeletePrevious());
            }
        }
        public override bool RemovePreviousIndent()
        {
            if (Column > 0)
            {
                int tabSize = _editorOptions.GetTabSize();

                int previousTabStop = Column - tabSize;
                if (Column % tabSize > 0)
                {
                    previousTabStop = (Column / tabSize) * tabSize;
                }

                int positionToDeleteTo = CurrentPosition;

                TextPoint newPoint = Clone();
                for (int i = CurrentPosition - 1; newPoint.Column >= previousTabStop; i--)
                {
                    newPoint.MoveTo(i);
                    string character = newPoint.GetNextCharacter();
                    if (!string.Equals(character, " ", StringComparison.Ordinal) && !string.Equals(character, "\t", StringComparison.Ordinal))
                    {
                        break;
                    }

                    positionToDeleteTo = i;

                    if (newPoint.Column == previousTabStop)
                    {
                        break;
                    }
                }

                return(PrimitivesUtilities.Delete(_textBuffer.AdvancedTextBuffer, Span.FromBounds(positionToDeleteTo, CurrentPosition)));
            }
            else
            {
                return(true);
            }
        }
        public override bool TransposeCharacter()
        {
            SnapshotPoint insertionPoint = AdvancedTextPoint;

            ITextSnapshotLine line = _textView.AdvancedTextView.TextSnapshot.GetLineFromPosition(insertionPoint);

            string lineText = line.GetText();

            if (StringInfo.ParseCombiningCharacters(lineText).Length < 2)
            {
                return(true);
            }

            SnapshotSpan textElementLeftSpan, textElementRightSpan;

            // We're at the start of a line
            if (insertionPoint == line.Start)
            {
                textElementLeftSpan  = TextView.AdvancedTextView.GetTextElementSpan(insertionPoint);
                textElementRightSpan = TextView.AdvancedTextView.GetTextElementSpan(textElementLeftSpan.End);
            }
            // We're at the end of a line
            else if (insertionPoint == line.End)
            {
                textElementRightSpan = TextView.AdvancedTextView.GetTextElementSpan(insertionPoint - 1);
                textElementLeftSpan  = TextView.AdvancedTextView.GetTextElementSpan(textElementRightSpan.Start - 1);
            }
            // We're at the middle of a line
            else
            {
                textElementRightSpan = TextView.AdvancedTextView.GetTextElementSpan(insertionPoint);
                textElementLeftSpan  = TextView.AdvancedTextView.GetTextElementSpan(textElementRightSpan.Start - 1);
            }

            string transposedText = _textView.AdvancedTextView.TextSnapshot.GetText(textElementRightSpan)
                                    + _textView.AdvancedTextView.TextSnapshot.GetText(textElementLeftSpan);

            return(PrimitivesUtilities.Replace(TextBuffer.AdvancedTextBuffer, new Span(textElementLeftSpan.Start, transposedText.Length), transposedText));
        }
        public override bool DeleteNext()
        {
            string nextCharacter = GetNextCharacter();

            return(PrimitivesUtilities.Delete(_textBuffer.AdvancedTextBuffer, CurrentPosition, nextCharacter.Length));
        }
예제 #11
0
 public override bool Delete()
 {
     return(PrimitivesUtilities.Delete(TextBuffer.AdvancedTextBuffer, Span.FromBounds(_startPoint.CurrentPosition, _endPoint.CurrentPosition)));
 }