// Set selection text to currentIndex startPoint and endPoint for every case except ReplaceAll public void FindReplace(string textToSearch, string replacement, FindOptions searchOptions) { if (string.IsNullOrWhiteSpace(textToSearch)) { if (searchResult != null) { searchResult.Clear(); } return; } // Different search option, Find all occurences. bool isFindOnly = !searchOptions.HasFlag(FindOptions.ReplaceAll) && !searchOptions.HasFlag(FindOptions.MatchCase) && !searchOptions.HasFlag(FindOptions.FindNext) && !searchOptions.Equals(FindOptions.FindPrevious) && string.IsNullOrEmpty(replacement); searchTerm = textToSearch; searchOption = (int)searchOptions; if (!previousSearchTerm.Equals(textToSearch) && !searchOptions.HasFlag(FindOptions.ReplaceOnce) && !searchOptions.HasFlag(FindOptions.MatchCase)) { FindAllOccurences(textToSearch, searchOptions, isFindOnly); } else { if (prevSearchResult != null && prevSearchResult.Count > 0 && prevSearchResult.Equals(searchResult)) { searchResult.RemoveAt(CurrentSearchIndex); } else { if (!previousSearchTerm.Equals(textToSearch)) { FindAllOccurences(textToSearch, searchOptions, isFindOnly); } } } if (searchResult == null) { return; } // Nothing found if (searchResult.Count <= 0) { return; } // Just a find if (isFindOnly) { return; } if (replacement == null) { // Find Previous if (searchOptions.HasFlag(FindOptions.FindPrevious)) { CurrentSearchIndex--; if (CurrentSearchIndex < 0) { CurrentSearchIndex = searchResult.Count - 1; } } else if (searchOptions.HasFlag(FindOptions.FindNext)) { // FindNext CurrentSearchIndex++; if (CurrentSearchIndex >= searchResult.Count) { CurrentSearchIndex = 0; } } return; } if ((searchOptions.HasFlag(FindOptions.ReplaceAll))) { if (UndoGroupNeedsClosing(searchResult[0].startPoint.Y, Operation.Replace)) { undoRedoRecorder.CloseUndoGroup(); } int index = 0; int findCount = searchResult.Count; for (int i = 0; i < findCount; i++) { ReplaceSingleOccurence(searchResult[i], replacement); UpdateStartEnd(index++, replacement.Length - textToSearch.Length); UpdateEnd(i, replacement.Length - textToSearch.Length); } CurrentSearchIndex = -1; undoRedoRecorder.CloseUndoGroup(); } else { // Replace once if (CurrentSearchIndex == -1) { CurrentSearchIndex = 0; } UpdateStartEnd(CurrentSearchIndex, replacement.Length - textToSearch.Length); if (UndoGroupNeedsClosing(searchResult[CurrentSearchIndex].startPoint.Y, Operation.Replace)) { undoRedoRecorder.CloseUndoGroup(); } ReplaceSingleOccurence(searchResult[CurrentSearchIndex], replacement); UpdateEnd(CurrentSearchIndex, replacement.Length - textToSearch.Length); undoRedoRecorder.CloseUndoGroup(); if (CurrentSearchIndex > 0) { CurrentSearchIndex--; } else { prevSearchResult = searchResult; } } ArrangeTextBuffer(); if (null != LinesUpdated) { // Notify listeners of line changes. LinesUpdated(this, new LinesUpdateEventArgs(searchResult[0].startPoint.Y, lineList.Count, lineList.Count)); } }
private void FindAllOccurences(string textToSearch, FindOptions option, bool isFindOnly) { searchResult = new List <FindPosition>(); int lineNumber = 0; int column = 0; foreach (string line in lineList) { if (option.HasFlag(FindOptions.MatchCase) || (prevOption != null && ((option.HasFlag(FindOptions.FindNext) || option.Equals(FindOptions.FindPrevious)) && prevOption.Equals("MatchCase")))) { column = line.IndexOf(textToSearch); } else { column = line.IndexOf(textToSearch, StringComparison.CurrentCultureIgnoreCase); } if (column != -1) { while (column != -1) { FindPosition position = new FindPosition(); position.startPoint = new Point(column, lineNumber); position.endPoint = new Point(column + textToSearch.Length - 1, lineNumber); searchResult.Add(position); if (column + textToSearch.Length < line.Length) { // "previousColumn stores how much column has already moved int previousColumn = column; if (option.HasFlag(FindOptions.MatchCase) || (prevOption != null && ((option.HasFlag(FindOptions.FindNext) || option.Equals(FindOptions.FindPrevious)) && prevOption.Equals("MatchCase")))) { column = line.Substring(column + textToSearch.Length).IndexOf(textToSearch); } else { column = line.Substring(column + textToSearch.Length).IndexOf(textToSearch, StringComparison.CurrentCultureIgnoreCase); } if (column != -1) { column += textToSearch.Length + previousColumn; } } else { break; } } } lineNumber++; } if (searchResult.Count <= 0) { CurrentSearchIndex = -1; } if ((option.HasFlag(FindOptions.FindNext) || option.HasFlag(FindOptions.FindPrevious) || option.HasFlag(FindOptions.ReplaceOnce) && !isFindOnly)) { return; } if (searchResult.Count > 0) { CurrentSearchIndex = 0; } if (option.HasFlag(FindOptions.MatchCase) || isFindOnly) { prevOption = option.ToString(); } }