private TextRange FindMatch(string pattern, FindOptions findOptions, TextPoint endPoint)
        {
            if (pattern.Length == 0)
            {
                return(_bufferPrimitivesFactory.CreateTextRange(_textBuffer, this.Clone(), this.Clone()));
            }

            FindData findData = new FindData(pattern, _textBuffer.AdvancedTextBuffer.CurrentSnapshot);

            findData.FindOptions = findOptions;

            bool wrapAround = endPoint == null;

            bool searchReverse = ((findData.FindOptions & FindOptions.SearchReverse) == FindOptions.SearchReverse);

            // In the case of a wrap-around search, we can start the search at this point always.  In the case
            // where we are searching a range in reverse, we have to start at the end point.
            int searchStartPosition = (searchReverse && !wrapAround) ? endPoint.CurrentPosition : CurrentPosition;

            SnapshotSpan?snapshotSpan = _findLogic.FindNext(searchStartPosition, wrapAround, findData);

            if (snapshotSpan.HasValue)
            {
                if ((endPoint == null) || (snapshotSpan.Value.End <= endPoint.CurrentPosition))
                {
                    return(_bufferPrimitivesFactory.CreateTextRange(_textBuffer,
                                                                    _bufferPrimitivesFactory.CreateTextPoint(_textBuffer, snapshotSpan.Value.Start),
                                                                    _bufferPrimitivesFactory.CreateTextPoint(_textBuffer, snapshotSpan.Value.End)));
                }
            }

            return(_bufferPrimitivesFactory.CreateTextRange(_textBuffer, this.Clone(), this.Clone()));
        }
Exemplo n.º 2
0
        private void FindBadActions(List <SnapshotSpan> wordSpans, SnapshotSpan currentWord)
        {
            foreach (CodeNamespace ns in _codeModel.CodeElements.OfType <CodeNamespace>())
            {
                foreach (CodeClass2 cls in ns.Members.OfType <CodeClass2>().Where(c => c.Name.EndsWith("Controller")))
                {
                    _controller = cls;
                    var hasBadActions = false;
                    if (cls.ProjectItem != null && cls.ProjectItem.FileCodeModel == _codeModel)
                    {
                        foreach (CodeFunction fn in cls.Members.OfType <CodeFunction>())
                        {
                            if (IsActionBad(fn))
                            {
                                FindData fData = new FindData(fn.Name, currentWord.Snapshot);
                                fData.FindOptions = FindOptions.WholeWord | FindOptions.MatchCase;

                                var res = _textSearchService.FindNext(fn.StartPoint.AbsoluteCharOffset + fn.StartPoint.Line - 1, false, fData);
                                if (res.HasValue)
                                {
                                    wordSpans.Add(res.Value);
                                    hasBadActions = true;
                                }
                            }
                        }

                        if (hasBadActions)
                        {
                            FindData fData = new FindData(cls.Name, currentWord.Snapshot);
                            fData.FindOptions = FindOptions.WholeWord | FindOptions.MatchCase;

                            var res = _textSearchService.FindNext(cls.StartPoint.AbsoluteCharOffset + cls.StartPoint.Line - 1, false, fData);
                            if (res.HasValue)
                            {
                                wordSpans.Add(res.Value);
                            }
                        }
                    }
                    break;
                }
            }
        }
Exemplo n.º 3
0
        public IncrementalSearchResult SelectNextResult()
        {
            if (!IsActive)
            {
                throw new InvalidOperationException();
            }
            if (SearchString.Length == 0)
            {
                return(searchFailedResult);
            }
            TextView.Selection.Clear();
            var res = textSearchService.FindNext(CaretStartPosition.Position, true, new FindData(SearchString, CaretStartPosition.Snapshot, FindOptions, null));

            if (res == null)
            {
                return(searchFailedResult);
            }
            editorOperations.SelectAndMoveCaret(new VirtualSnapshotPoint(res.Value.Start), new VirtualSnapshotPoint(res.Value.End));
            if (SearchDirection == IncrementalSearchDirection.Forward)
            {
                return(new IncrementalSearchResult(passedEndOfBuffer: res.Value.Start < CaretStartPosition, passedStartOfBuffer: false, passedStartOfSearch: res.Value.Start != CaretStartPosition, resultFound: true));
            }
            return(new IncrementalSearchResult(passedEndOfBuffer: false, passedStartOfBuffer: res.Value.End > CaretStartPosition, passedStartOfSearch: res.Value.End != CaretStartPosition, resultFound: true));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Handles finding occurrences, selecting and adding to current selections
        /// </summary>
        /// <param name="reverseDirection">Search document in reverse direction for an occurrence</param>
        /// <param name="exactMatch">Search document for an exact match, overrides find-dialog settings</param>
        internal void SelectNextOccurrence(bool reverseDirection = false, bool exactMatch = false)
        {
            // Caret placed on a word, but nothing selected
            if (!Selections.Any() && view.Selection.IsEmpty)
            {
                editorOperations.SelectCurrentWord();

                if (!String.IsNullOrEmpty(editorOperations.SelectedText))
                {
                    AddCurrentSelectionToSelections();
                }

                IsReversing = false;

                return;
            }

            // First selection is selected by user, future selections will be located and selected on command-invocation
            if (!Selections.Any() && !view.Selection.IsEmpty)
            {
                AddCurrentSelectionToSelections();
            }

            // Multiple selections
            if (Selections.Any())
            {
                // Select words at caret again, this is where we have abandoned selections and goes to carets
                if (Selections.All(s => !s.IsSelection()))
                {
                    var oldSelections = Selections;
                    Selections = new List <Selection>();

                    foreach (var selection in oldSelections)
                    {
                        view.Caret.MoveTo(selection.Caret.GetPoint(Snapshot));
                        editorOperations.SelectCurrentWord();
                        AddCurrentSelectionToSelections();
                    }
                }
                else
                {
                    // Start the search from previous end-position if it exists, otherwise caret
                    int startIndex;

                    if (reverseDirection)
                    {
                        startIndex = Selections.Last().Start != null?
                                     Selections.Last().Start.GetPosition(Snapshot)
                                         : Selections.Last().Caret.GetPosition(Snapshot);
                    }
                    else
                    {
                        startIndex = Selections.Last().End != null?
                                     Selections.Last().End.GetPosition(Snapshot)
                                         : Selections.Last().Caret.GetPosition(Snapshot);
                    }

                    var occurrence = textSearchService.FindNext(
                        startIndex,
                        true,
                        GetFindData(reverseDirection, exactMatch)
                        );

                    if (occurrence.HasValue)
                    {
                        ProcessFoundOccurrence(occurrence.Value);
                    }
                }

                view.Selection.Clear();
            }

            IsReversing = false;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Handles finding occurrences, selecting and adding to current selections
        /// </summary>
        /// <param name="reverseDirection">Search document in reverse direction for an occurrence</param>
        /// <param name="exactMatch">Search document for an exact match, overrides find-dialog settings</param>
        internal void SelectNextOccurrence(bool reverseDirection = false, bool exactMatch = false)
        {
            // Caret placed on a word, but nothing selected
            if (!Selections.Any() && view.Selection.IsEmpty)
            {
                SelectCurrentWord(view.Caret.Position.BufferPosition);
                return;
            }

            // First selection is selected by user, future selections will be located and selected on command-invocation
            if (!Selections.Any() && !view.Selection.IsEmpty)
            {
                AddCurrentSelectionToSelections();
            }

            // Multiple selections
            if (Selections.Any())
            {
                // Select words at caret again, this is where we have abandoned selections and goes to carets
                if (Selections.Any(s => !s.IsSelection()))
                {
                    var oldSelections = Selections;
                    Selections = new List <Selection>();

                    foreach (var selection in oldSelections)
                    {
                        if (!selection.IsSelection())
                        {
                            view.Caret.MoveTo(selection.Caret.GetPoint(Snapshot));
                            SelectCurrentWord(selection.Caret.GetPoint(Snapshot));
                        }
                        else
                        {
                            Selections.Add(selection);
                        }
                    }
                }
                else
                {
                    var orderedSelections = HasWrappedDocument
                        ? Selections
                        : Selections.OrderBy(n => n.Caret.GetPosition(Snapshot)).ToList();

                    var startSelection = reverseDirection && !HasWrappedDocument
                        ? orderedSelections.First()
                        : orderedSelections.Last();

                    var startIndex = reverseDirection ?
                                     startSelection.Start?.GetPosition(Snapshot) ?? startSelection.Caret.GetPosition(Snapshot)
                        : startSelection.End?.GetPosition(Snapshot) ?? startSelection.Caret.GetPosition(Snapshot);

                    var occurrence = textSearchService.FindNext(
                        startIndex,
                        true,
                        GetFindData(reverseDirection, exactMatch)
                        );

                    if (occurrence.HasValue)
                    {
                        ProcessFoundOccurrence(occurrence.Value);

                        if (!reverseDirection && Selections.Last().Caret.GetPosition(Snapshot) <
                            Selections.First().Caret.GetPosition(Snapshot))
                        {
                            HasWrappedDocument = true;
                        }

                        if (reverseDirection && Selections.Last().Caret.GetPosition(Snapshot) >
                            Selections.First().Caret.GetPosition(Snapshot))
                        {
                            HasWrappedDocument = true;
                        }
                    }
                }

                view.Selection.Clear();
                view.Caret.MoveTo(Selections.Last().Caret.GetPoint(Snapshot));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Handles finding occurrences, selecting and adding to current selections
        /// </summary>
        /// <param name="reverseDirection">Search document in reverse direction for an occurrence</param>
        /// <param name="exactMatch">Search document for an exact match, overrides find-dialog settings</param>
        internal void SelectNextOccurrence(bool reverseDirection = false, bool exactMatch = false)
        {
            // Caret placed on a word, but nothing selected
            if (!Selections.Any() && view.Selection.IsEmpty)
            {
                SelectCurrentWord(view.Caret.Position.BufferPosition);
                return;
            }

            // First selection is selected by user, future selections will be located and selected on command-invocation
            if (!Selections.Any() && !view.Selection.IsEmpty)
            {
                AddCurrentSelectionToSelections();
            }

            // Multiple selections
            if (Selections.Any())
            {
                // Select words at caret again, this is where we have abandoned selections and goes to carets
                if (Selections.Any(s => !s.IsSelection()))
                {
                    var oldSelections = Selections;
                    Selections = new List <Selection>();

                    // Note: The list is in reverse order to fix a bug in EditorOperations.SelectCurrentWord()
                    foreach (var selection in oldSelections.OrderByDescending(n => n.Caret.GetPosition(Snapshot)))
                    {
                        if (!selection.IsSelection())
                        {
                            view.Caret.MoveTo(selection.Caret.GetPoint(Snapshot));
                            SelectCurrentWord(selection.Caret.GetPoint(Snapshot));
                        }
                        else
                        {
                            Selections.Add(selection);
                        }
                    }
                }
                else
                {
                    var orderedSelections = Selections.OrderBy(n => n.Caret.GetPosition(Snapshot)).ToList();
                    var startIndex        = ExtensionOptions.Instance.InwardSelection ^ reverseDirection ? 0 : orderedSelections.Count - 1;
                    var direction         = reverseDirection ? -1 : 1;

                    var index = startIndex;
                    do
                    {
                        var position = reverseDirection
                            ? orderedSelections[index].Start.GetPosition(Snapshot)
                            : orderedSelections[index].End.GetPosition(Snapshot);

                        index = (index + direction + orderedSelections.Count) % orderedSelections.Count;
                        if (textSearchService.FindNext(position, true, GetFindData(reverseDirection, exactMatch))
                            is SnapshotSpan occurrence &&
                            !orderedSelections[index].OverlapsWith(occurrence, Snapshot))
                        {
                            ProcessFoundOccurrence(occurrence);
                            break;
                        }
                    } while (startIndex != index);
                }

                view.Selection.Clear();
                view.Caret.MoveTo(Selections.Last().Caret.GetPoint(Snapshot));
            }
        }