internal static void SearchReplace (string findPattern, string replacePattern, Scope scope, FilterOptions options, System.Action UpdateStopButton)
{
if (find != null && find.IsRunning) {
if (!MessageService.Confirm (GettextCatalog.GetString ("There is a search already in progress. Do you want to stop it?"), AlertButton.Stop))
return;
lock (searchesInProgress) {
foreach (var mon in searchesInProgress)
mon.AsyncOperation.Cancel ();
searchesInProgress.Clear ();
}
}
if (scope == null)
return;
find = new FindReplace ();
string pattern = findPattern;
if (!find.ValidatePattern (options, pattern)) {
MessageService.ShowError (GettextCatalog.GetString ("Search pattern is invalid"));
return;
}
if (replacePattern != null && !find.ValidatePattern (options, replacePattern)) {
MessageService.ShowError (GettextCatalog.GetString ("Replace pattern is invalid"));
return;
}
ThreadPool.QueueUserWorkItem (delegate {
using (ISearchProgressMonitor searchMonitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true)) {
searchMonitor.ReportStatus (scope.GetDescription (options, pattern, null));
lock (searchesInProgress)
searchesInProgress.Add (searchMonitor);
if (UpdateStopButton != null) {
Application.Invoke (delegate {
UpdateStopButton ();
});
}
DateTime timer = DateTime.Now;
string errorMessage = null;
try {
var results = new List<SearchResult> ();
foreach (SearchResult result in find.FindAll (scope, searchMonitor, pattern, replacePattern, options)) {
if (searchMonitor.IsCancelRequested)
return;
results.Add (result);
}
searchMonitor.ReportResults (results);
} catch (Exception ex) {
errorMessage = ex.Message;
LoggingService.LogError ("Error while search", ex);
}
string message;
if (errorMessage != null) {
message = GettextCatalog.GetString ("The search could not be finished: {0}", errorMessage);
searchMonitor.ReportError (message, null);
} else if (searchMonitor.IsCancelRequested) {
message = GettextCatalog.GetString ("Search cancelled.");
searchMonitor.ReportWarning (message);
} else {
string matches = string.Format (GettextCatalog.GetPluralString ("{0} match found", "{0} matches found", find.FoundMatchesCount), find.FoundMatchesCount);
string files = string.Format (GettextCatalog.GetPluralString ("in {0} file.", "in {0} files.", find.SearchedFilesCount), find.SearchedFilesCount);
message = GettextCatalog.GetString ("Search completed.") + Environment.NewLine + matches + " " + files;
searchMonitor.ReportSuccess (message);
}
searchMonitor.ReportStatus (message);
searchMonitor.Log.WriteLine (GettextCatalog.GetString ("Search time: {0} seconds."), (DateTime.Now - timer).TotalSeconds);
searchesInProgress.Remove (searchMonitor);
}
if (UpdateStopButton != null) {
Application.Invoke (delegate {
UpdateStopButton ();
});
}
});
}
}