/// <summary> /// Move to the next highlight starting at the <paramref name="caretPosition"/> /// </summary> /// <param name="forward">true for next false for previous</param> /// <param name="caretPosition">caret position</param> /// <returns>the next highlight starting at the <paramref name="caretPosition"/></returns> internal Run MoveAndHighlightNextNextMatch(bool forward, TextPointer caretPosition) { Debug.Assert(caretPosition != null, "a caret position is allways valid"); Debug.Assert(caretPosition.Parent != null && caretPosition.Parent is Run, "a caret PArent is allways a valid Run"); Run caretRun = (Run)caretPosition.Parent; Run currentRun; if (this.currentHighlightedMatch != null) { // restore the curent highlighted background to plain highlighted this.currentHighlightedMatch.Background = ParagraphSearcher.HighlightBrush; } // If the caret is in the end of a highlight we move to the adjacent run // It has to be in the end because if there is a match at the begining of the file // and the caret has not been touched (so it is in the beginning of the file too) // we want to highlight this first match. // Considering the caller allways set the caret to the end of the highlight // The condition below works well for successive searchs // We also need to move to the adjacent run if the caret is at the first run and we // are moving backwards so that a search backwards when the first run is highlighted // and the caret is at the beginning will wrap to the end if ((!forward && IsFirstRun(caretRun)) || ((caretPosition.GetOffsetToPosition(caretRun.ContentEnd) == 0) && ParagraphSearcher.Ishighlighted(caretRun))) { currentRun = ParagraphSearcher.GetNextRun(caretRun, forward); } else { currentRun = caretRun; } currentRun = ParagraphSearcher.GetNextMatch(currentRun, forward); if (currentRun == null) { // if we could not find a next highlight wrap arround currentRun = ParagraphSearcher.GetFirstOrLastRun(caretRun, forward); currentRun = ParagraphSearcher.GetNextMatch(currentRun, forward); } this.currentHighlightedMatch = currentRun; if (this.currentHighlightedMatch != null) { // restore the curent highligthed background to current highlighted this.currentHighlightedMatch.Background = ParagraphSearcher.CurrentHighlightBrush; } return(currentRun); }
/// <summary> /// Gets the next highlighted run starting and including <paramref name="currentRun"/> /// according to the direction specified in <paramref name="forward"/> /// </summary> /// <param name="currentRun">the current run</param> /// <param name="forward">true for next false for previous</param> /// <returns> /// the next highlighted run starting and including <paramref name="currentRun"/> /// according to the direction specified in <paramref name="forward"/> /// </returns> private static Run GetNextMatch(Run currentRun, bool forward) { while (currentRun != null) { if (ParagraphSearcher.Ishighlighted(currentRun)) { return(currentRun); } currentRun = ParagraphSearcher.GetNextRun(currentRun, forward); } return(currentRun); }