Пример #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);
        }
        /// <summary>
        /// Determine if this text point's current word is a blank line on the next line.
        /// </summary>
        /// <param name="currentWord">The current word to check.</param>
        private bool IsCurrentWordABlankLine(TextExtent currentWord)
        {
            TextPoint endOfCurrentWord = Clone();

            endOfCurrentWord.MoveTo(currentWord.Span.End);
            return((EndOfLine == CurrentPosition) && (currentWord.Span.End == endOfCurrentWord.EndOfLine));
        }
Пример #3
0
        protected override IEnumerator <TextPoint> GetEnumeratorInternal()
        {
            for (int position = _startPoint.CurrentPosition; position <= _endPoint.CurrentPosition; position++)
            {
                TextPoint enumeratedPoint = _startPoint.Clone();
                enumeratedPoint.MoveTo(position);

                yield return(enumeratedPoint);
            }
        }
        public override TextRange GetNextWord()
        {
            TextExtent currentWord = GetTextExtent(CurrentPosition);

            if (currentWord.Span.End < _textBuffer.AdvancedTextBuffer.CurrentSnapshot.Length)
            {
                // If the current point is at the end of the line, look for the next word on the next line
                if ((CurrentPosition == EndOfLine) && (CurrentPosition != _textBuffer.AdvancedTextBuffer.CurrentSnapshot.Length))
                {
                    TextPoint textPoint = Clone();
                    textPoint.MoveToBeginningOfNextLine();
                    textPoint.MoveTo(textPoint.StartOfLine);
                    TextExtent wordOnNextLine = GetTextExtent(textPoint.CurrentPosition);
                    if (wordOnNextLine.IsSignificant)
                    {
                        return(textPoint.GetCurrentWord());
                    }
                    else if (wordOnNextLine.Span.End >= textPoint.EndOfLine)
                    {
                        return(textPoint.GetTextRange(textPoint.EndOfLine));
                    }
                    return(textPoint.GetNextWord());
                }
                // By default, VS stops at line breaks when determing word
                // boundaries.
                else if (ShouldStopAtEndOfLine(currentWord.Span.End) || IsCurrentWordABlankLine(currentWord))
                {
                    return(GetTextRange(currentWord.Span.End));
                }

                TextExtent nextWord = GetTextExtent(currentWord.Span.End);

                if (!nextWord.IsSignificant)
                {
                    nextWord = GetTextExtent(nextWord.Span.End);
                }

                int start = nextWord.Span.Start;
                int end   = nextWord.Span.End;

                // The text structure navigator can return a word with whitespace attached at the end.
                // Handle that case here.
                start = Math.Max(start, currentWord.Span.End);

                TextPoint startPoint = Clone();
                TextPoint endPoint   = Clone();
                startPoint.MoveTo(start);
                endPoint.MoveTo(end);

                return(_bufferPrimitivesFactory.CreateTextRange(_textBuffer, startPoint, endPoint));
            }

            return(_bufferPrimitivesFactory.CreateTextRange(_textBuffer, TextBuffer.GetEndPoint(), TextBuffer.GetEndPoint()));
        }
        public override TextRange GetPreviousWord()
        {
            TextRange currentWord = GetCurrentWord();

            if (currentWord.GetStartPoint().CurrentPosition > 0)
            {
                // By default, VS stops at line breaks when determing word
                // boundaries.
                if ((currentWord.GetStartPoint().CurrentPosition == StartOfLine) &&
                    (CurrentPosition != StartOfLine))
                {
                    return(GetTextRange(currentWord.GetStartPoint()));
                }

                // If the point is at the end of a word that is not whitespace, it is possible
                // that the "current word" is also the previous word in standard VS.
                if ((currentWord.GetEndPoint().CurrentPosition == CurrentPosition) &&
                    (!currentWord.IsEmpty))
                {
                    return(currentWord);
                }

                TextPoint pointInPreviousWord = currentWord.GetStartPoint();
                pointInPreviousWord.MoveTo(pointInPreviousWord.CurrentPosition - 1);

                TextRange previousWord = pointInPreviousWord.GetCurrentWord();

                if (previousWord.GetStartPoint().CurrentPosition > 0)
                {
                    if (ShouldContinuePastPreviousWord(previousWord))
                    {
                        pointInPreviousWord.MoveTo(previousWord.GetStartPoint().CurrentPosition - 1);
                        previousWord = pointInPreviousWord.GetCurrentWord();
                    }
                }

                return(previousWord);
            }

            return(_bufferPrimitivesFactory.CreateTextRange(_textBuffer, TextBuffer.GetStartPoint(), TextBuffer.GetStartPoint()));
        }
        public override TextRange GetTextRange(int otherPosition)
        {
            if ((otherPosition < 0) || (otherPosition > TextBuffer.AdvancedTextBuffer.CurrentSnapshot.Length))
            {
                throw new ArgumentOutOfRangeException(nameof(otherPosition));
            }

            TextPoint otherPoint = this.Clone();

            otherPoint.MoveTo(otherPosition);

            return(_bufferPrimitivesFactory.CreateTextRange(_textBuffer, this.Clone(), otherPoint));
        }
        public override TextRange GetCurrentWord()
        {
            // If the line is blank, just return a blank range for the line
            SnapshotPoint     currentPoint = AdvancedTextPoint;
            ITextSnapshotLine line         = currentPoint.GetContainingLine();

            if (currentPoint == line.Start && line.Length == 0)
            {
                return(_bufferPrimitivesFactory.CreateTextRange(_textBuffer, this, this));
            }

            TextExtent textExtent = GetTextExtent(CurrentPosition);

            // If the word is not significant, then see if there is a significant word right before this one,
            // and return that instead to mimic VS 9 behavior.
            if (!textExtent.IsSignificant)
            {
                if ((textExtent.Span.Start > 0) && (textExtent.Span.Length > 0))
                {
                    if (textExtent.Span.Start == CurrentPosition)
                    {
                        if (!char.IsWhiteSpace(_textBuffer.AdvancedTextBuffer.CurrentSnapshot[textExtent.Span.Start - 1]))
                        {
                            textExtent = GetTextExtent(textExtent.Span.Start - 1);
                        }
                    }
                    else if (CurrentPosition == EndOfLine)
                    {
                        // If this text point is on the end of the line, then check to see if there is a word
                        // just before the whitespace.
                        if (!char.IsWhiteSpace(_textBuffer.AdvancedTextBuffer.CurrentSnapshot[textExtent.Span.Start - 1]))
                        {
                            TextExtent newExtent = new TextExtent(GetTextExtent(textExtent.Span.Start - 1));
                            textExtent = new TextExtent(new SnapshotSpan(newExtent.Span.Start, textExtent.Span.End), true);
                        }
                    }
                }
            }

            TextPoint startPoint = Clone();

            startPoint.MoveTo(textExtent.Span.Start);
            TextPoint endPoint = Clone();

            endPoint.MoveTo(textExtent.Span.End);

            return(_bufferPrimitivesFactory.CreateTextRange(_textBuffer, startPoint, endPoint));
        }
        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 void MoveTo(int position)
        {
            SnapshotPoint point = new SnapshotPoint(TextView.AdvancedTextView.TextSnapshot, position);

            _bufferPoint.MoveTo(TextView.AdvancedTextView.GetTextElementSpan(point).Start);
        }