public void MakeInvalid() { RapidFindReplaceControlViewModel model = new RapidFindReplaceControlViewModel(); model.FindOptions.UseRegularExpressions = true; Query q = new Query(".*["); RunQueue rq = new RunQueue(1); rq.Enqueue(new System.Windows.Documents.Run("hello this is it")); model.ScanQueuedRuns(rq, q, null); Assert.IsFalse(q.Valid); Assert.AreEqual("Regular expression error, parsing \".*[\" - Unterminated [] set.", q.ReasonInvalid); }
/// <summary> /// New. /// </summary> public RapidFindReplaceControl() { queryContainer = new Query(""); // this.Resources.MergedDictionaries.Add(SharedDictionaryManager.SharedDictionary); //AsYouTypeFindMinimumCharacters = 2; //AsYouTypeFindEnabled = true; ViewModel = new RapidFindReplaceControlViewModel();//must use setter as it does a binding }
/// <summary> /// /// </summary> /// <param name="searchText"></param> /// <param name="currentIndex"></param> /// <param name="text"></param> /// <returns></returns> public Hit GetNextMatch_FOR_TESTING(string searchText, int currentIndex, string text) { Query q = new Query(searchText); return GetNextMatch(q.GetTextMatchers(text, FindOptions), currentIndex); }
/// <summary> /// Scans a queue of runs and highlights matches. /// </summary> /// <param name="runQueue">Run queue</param> /// <param name="query">Query</param> /// <param name="addHighlightDelegate">Delegate to call when a hit is found</param> public virtual void ScanQueuedRuns(RunQueue runQueue, Query query, AddHighlight addHighlightDelegate) { //this.searchText = query; System.Text.StringBuilder queuedText = new System.Text.StringBuilder(100); queuedText.Length = 0; for (int i = 0; i < runQueue.Count && (i <= 1 || queuedText.Length < 100); i++)//look at the text in queue up until 100 chars of the 2nd run { queuedText.Append(runQueue[i].HitAvailableText); } int currentIndex = 0; string text = queuedText.ToString(); int index; Hit hit; TextMatchers textMatchers = query.GetTextMatchers(text, FindOptions); while ((hit = GetNextMatch(textMatchers, currentIndex)).Start > -1 && currentGlobalScanHighlightCount < maximumHitsToHighlight)//find a hit for searchText in the plain text version. { index = hit.Start; //we have a hit, we need to find the runs it was in and highlight int highlightStart; int gobbledChars = 0; int runHitLength = hit.Length; int highlightLength; //string searchSegment = query.QueryText; bool moreToFind = true; while (moreToFind) { Run runAtPos = runQueue.HitPosition(index, runHitLength, out highlightStart, out gobbledChars); //gobbledChars is the number of chars in runAtPos that were used (this could be the entire run length if the runHitLength is less than the //number of chars in the run). //indexOffset is where in the run to start highlighting if (gobbledChars < runHitLength) { moreToFind = true; //there weren't enough chars in the run, so we'll need to look for more index += gobbledChars; runHitLength -= gobbledChars; highlightLength = gobbledChars; } else { moreToFind = false; highlightLength = runHitLength; } addHighlightDelegate(runAtPos, highlightStart, highlightLength); currentGlobalScanHighlightCount++; currentIndex = index + runHitLength; } } }
void ProcessRemainingQueuedRuns(AddHighlight addHighlightDelegate, RunQueue runQueue, Query query) { while (runQueue.Count > 0) { runQueue.Dequeue(); ScanQueuedRuns(runQueue, query, addHighlightDelegate); } }
void ProcessRun(AddHighlight addHighlightDelegate, Run run, RunQueue runQueue, Query searchText) { if (run != null && !string.IsNullOrEmpty(run.Text)) { runQueue.Enqueue(run); ScanQueuedRuns(runQueue, searchText, addHighlightDelegate); } }
/// <summary> /// Finds the <c>searchText</c> query in Runs. /// </summary> /// <param name="runs">Collection of runs to look in</param> /// <param name="addHighlightDelegate">Delegate to call when hits are found</param> /// <param name="searchText">The query to search for</param> public void FindTextIn(List<Run> runs, AddHighlight addHighlightDelegate, Query searchText) { if (searchText == null) searchText = Query; runQueue.Clear(); currentGlobalScanHighlightCount = 0; foreach (Run run in runs) { ProcessRun(addHighlightDelegate, run, runQueue, searchText); } ProcessRemainingQueuedRuns(addHighlightDelegate, runQueue, searchText); }
/// <summary> /// Finds the <c>searchText</c> query in Runs returned by <c>runReader</c>. /// </summary> /// <param name="runReader">IRunReader that will return an enumeration of Runs to search in</param> /// <param name="addHighlightDelegate">Delegate to call when hits are found</param> /// <param name="searchText">The query to search for</param> public void FindTextIn(IRunReader runReader, AddHighlight addHighlightDelegate, Query searchText) { runQueue.Clear(); currentGlobalScanHighlightCount = 0; foreach (Run run in runReader) { ProcessRun(addHighlightDelegate, run, runQueue, searchText); } ProcessRemainingQueuedRuns(addHighlightDelegate, runQueue, searchText); }