Example #1
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 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));
        }
Example #3
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 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));
        }