Ejemplo n.º 1
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));
            }
        }
Ejemplo n.º 2
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));
            }
        }