Пример #1
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 void MoveToNextCharacter()
        {
            int currentPosition = this.CurrentPosition;

            MoveTo(GetNextTextElementSpan().End);

            // It's possible that the above code didn't actually move this point.  Dev11 #109752 shows how
            // this can happen, which is that GetNextTextElementSpan() above (which calls into WPF) may say
            // that the current character doesn't combine with anything, but the _bufferPoint (which calls into
            // framework methods that follow the unicode standard) will say that the character does combine.  In that case
            // the GetNextTextElementSpan().End isn't far enough away for this point to escape the combining character
            // sequence, and the point is left where it is, at the start of a base character.
            if (currentPosition == this.CurrentPosition &&
                currentPosition != this.AdvancedTextPoint.Snapshot.Length)
            {
                _bufferPoint.MoveToNextCharacter();

                Debug.Assert(currentPosition != this.CurrentPosition,
                             "DisplayTextPoint.MoveToNextCharacter was unable to successfully move forward. This may cause unexpected behavior or hangs.");
            }
        }