public IEnumerable<SearchResult> FindAll(Scope scope, IProgressMonitor monitor, string pattern, string replacePattern, FilterOptions filter) { if (filter.RegexSearch) { RegexOptions regexOptions = RegexOptions.Compiled; if (!filter.CaseSensitive) regexOptions |= RegexOptions.IgnoreCase; regex = new Regex (pattern, regexOptions); } IsRunning = true; FoundMatchesCount = SearchedFilesCount = 0; monitor.BeginTask (scope.GetDescription (filter, pattern, replacePattern), 50); try { int totalWork = scope.GetTotalWork (filter); int step = Math.Max (1, totalWork / 50); string content; foreach (FileProvider provider in scope.GetFiles (monitor, filter)) { if (monitor.IsCancelRequested) yield break; SearchedFilesCount++; try { content = provider.ReadString (); if (replacePattern != null) provider.BeginReplace (content); } catch (System.IO.FileNotFoundException) { Application.Invoke (delegate { MessageService.ShowError (string.Format (GettextCatalog.GetString ("File {0} not found.")), provider.FileName); }); continue; } foreach (SearchResult result in FindAll (monitor, provider, content, pattern, replacePattern, filter)) { if (monitor.IsCancelRequested) yield break; FoundMatchesCount++; yield return result; } if (replacePattern != null) provider.EndReplace (); if (SearchedFilesCount % step == 0) monitor.Step (1); } } finally { monitor.EndTask (); IsRunning = false; } }
public IEnumerable<SearchResult> FindAll (Scope scope, IProgressMonitor monitor, string pattern, string replacePattern, FilterOptions filter) { if (filter.RegexSearch) { RegexOptions regexOptions = RegexOptions.Compiled; if (!filter.CaseSensitive) regexOptions |= RegexOptions.IgnoreCase; regex = new Regex (pattern, regexOptions); } else { CompilePattern (pattern, filter); } IsRunning = true; FoundMatchesCount = SearchedFilesCount = 0; monitor.BeginTask (scope.GetDescription (filter, pattern, replacePattern), 100); try { int totalWork = scope.GetTotalWork (filter); int step = Math.Max (1, totalWork / 100); foreach (FileProvider provider in scope.GetFiles (monitor, filter)) { if (monitor.IsCancelRequested) break; SearchedFilesCount++; if (!string.IsNullOrEmpty (replacePattern)) provider.BeginReplace (); foreach (SearchResult result in FindAll (monitor, provider, pattern, replacePattern, filter)) { if (monitor.IsCancelRequested) break; FoundMatchesCount++; yield return result; } if (!string.IsNullOrEmpty (replacePattern)) provider.EndReplace (); if (SearchedFilesCount % step == 0) monitor.Step (1); DispatchService.RunPendingEvents (); } } finally { monitor.EndTask (); IsRunning = false; } }
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 (); }); } }); }
public IEnumerable<SearchResult> FindAll (Scope scope, ProgressMonitor monitor, string pattern, string replacePattern, FilterOptions filter) { if (filter.RegexSearch) { RegexOptions regexOptions = RegexOptions.Compiled; if (!filter.CaseSensitive) regexOptions |= RegexOptions.IgnoreCase; regex = new Regex (pattern, regexOptions); } IsRunning = true; FoundMatchesCount = SearchedFilesCount = 0; monitor.BeginTask (scope.GetDescription (filter, pattern, replacePattern), 150); try { int totalWork = scope.GetTotalWork (filter); int step = Math.Max (1, totalWork / 50); var contents = new List<Tuple<FileProvider, string, List<SearchResult>>>(); foreach (var provider in scope.GetFiles (monitor, filter)) { try { searchedFilesCount++; contents.Add(Tuple.Create (provider, provider.ReadString (), new List<SearchResult> ())); if (searchedFilesCount % step == 0) monitor.Step (2); } catch (FileNotFoundException) { MessageService.ShowError (string.Format (GettextCatalog.GetString ("File {0} not found.")), provider.FileName); } } var results = new List<SearchResult>(); if (filter.RegexSearch && replacePattern != null) { foreach (var content in contents) { results.AddRange (RegexSearch (monitor, content.Item1, content.Item2, replacePattern, filter)); } } else { Parallel.ForEach (contents, content => { if (monitor.CancellationToken.IsCancellationRequested) return; try { Interlocked.Increment (ref searchedFilesCount); content.Item3.AddRange(FindAll (monitor, content.Item1, content.Item2, pattern, replacePattern, filter)); lock (results) { results.AddRange (content.Item3); } FoundMatchesCount += content.Item3.Count; if (searchedFilesCount % step == 0) monitor.Step (1); } catch (Exception e) { LoggingService.LogError("Exception during search.", e); } }); if (replacePattern != null) { foreach (var content in contents) { if (content.Item3.Count == 0) continue; try { content.Item1.BeginReplace (content.Item2); Replace (content.Item1, content.Item3, replacePattern); content.Item1.EndReplace (); } catch (Exception e) { LoggingService.LogError("Exception during replace.", e); } } } } return results; } finally { monitor.EndTask (); IsRunning = false; } }
public IEnumerable<SearchResult> FindAll (Scope scope, ProgressMonitor monitor, string pattern, string replacePattern, FilterOptions filter, CancellationToken token) { if (filter.RegexSearch) { RegexOptions regexOptions = RegexOptions.Compiled; if (!filter.CaseSensitive) regexOptions |= RegexOptions.IgnoreCase; regex = new Regex (pattern, regexOptions); } IsRunning = true; FoundMatchesCount = SearchedFilesCount = 0; monitor.BeginTask (scope.GetDescription (filter, pattern, replacePattern), 150); try { int totalWork = scope.GetTotalWork (filter); int step = Math.Max (1, totalWork / 50); var contents = new List<FileSearchResult>(); var filenames = new List<string> (); foreach (var provider in scope.GetFiles (monitor, filter)) { if (token.IsCancellationRequested) return Enumerable.Empty<SearchResult> (); try { searchedFilesCount++; contents.Add(new FileSearchResult (provider, null, new List<SearchResult> ())); filenames.Add (Path.GetFullPath (provider.FileName)); if (searchedFilesCount % step == 0) monitor.Step (2); } catch (FileNotFoundException) { MessageService.ShowError (string.Format (GettextCatalog.GetString ("File {0} not found.")), provider.FileName); } } var readers = IdeApp.Workbench.GetDocumentReaders (filenames); int idx = 0; if (readers != null) { foreach (var r in readers) { contents [idx].Reader = r; idx++; } } idx = 0; int c = 0; int t = 0; foreach (var result in contents) { if (readers == null || readers [idx] == null) { result.Reader = result.Provider.GetReaderForFileName (); } else { result.Reader = readers [idx]; c++; } t++; idx++; } var results = new List<SearchResult>(); if (filter.RegexSearch && replacePattern != null) { foreach (var content in contents) { if (token.IsCancellationRequested) return Enumerable.Empty<SearchResult> (); results.AddRange (RegexSearch (monitor, content.Provider, content.Reader, replacePattern, filter)); } } else { var options = new ParallelOptions (); options.MaxDegreeOfParallelism = 4; options.CancellationToken = token; Parallel.ForEach (contents, options, content => { if (token.IsCancellationRequested) return; try { Interlocked.Increment (ref searchedFilesCount); if (replacePattern != null) { content.Text = content.Reader.ReadToEnd (); content.Reader = new StringReader (content.Text); } content.Results.AddRange(FindAll (monitor, content.Provider, content.Reader, pattern, replacePattern, filter)); lock (results) { results.AddRange (content.Results); } FoundMatchesCount += content.Results.Count; if (searchedFilesCount % step == 0) monitor.Step (1); } catch (Exception e) { LoggingService.LogError("Exception during search.", e); } }); if (replacePattern != null) { foreach (var content in contents) { if (token.IsCancellationRequested) return Enumerable.Empty<SearchResult> (); if (content.Results.Count == 0) continue; try { content.Provider.BeginReplace (content.Text); Replace (content.Provider, content.Results, replacePattern); content.Provider.EndReplace (); } catch (Exception e) { LoggingService.LogError("Exception during replace.", e); } } } } return results; } finally { monitor.EndTask (); IsRunning = false; } }
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; } } searchTokenSource.Cancel(); if (scope == null) { return; } find = new FindReplace(); string pattern = findPattern; if (String.IsNullOrEmpty(pattern)) { return; } 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; } var cancelSource = new CancellationTokenSource(); searchTokenSource = cancelSource; var token = cancelSource.Token; currentTask = Task.Run(delegate { using (SearchProgressMonitor searchMonitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor(true, cancellationTokenSource: cancelSource)) { searchMonitor.PathMode = scope.PathMode; searchMonitor.ReportStatus(scope.GetDescription(options, pattern, null)); 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, token)) { if (token.IsCancellationRequested) { 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.CancellationToken.IsCancellationRequested) { 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); } if (UpdateStopButton != null) { Application.Invoke(delegate { UpdateStopButton(); }); } }); }
internal static void SearchReplace (string findPattern, string replacePattern, Scope scope, FilterOptions options, System.Action UpdateStopButton, System.Action<SearchResultPad> UpdateResultPad) { 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; } searchTokenSource.Cancel (); if (scope == null) return; find = new FindReplace (); string pattern = findPattern; if (String.IsNullOrEmpty (pattern)) return; 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; } var cancelSource = new CancellationTokenSource (); searchTokenSource = cancelSource; var token = cancelSource.Token; currentTask = Task.Run (delegate { using (SearchProgressMonitor searchMonitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true)) { searchMonitor.PathMode = scope.PathMode; if (UpdateResultPad != null) { Application.Invoke (delegate { UpdateResultPad (searchMonitor.ResultPad); }); } searchMonitor.ReportStatus (scope.GetDescription (options, pattern, null)); 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, token)) { if (token.IsCancellationRequested) return; results.Add (result); } searchMonitor.ReportResults (results); } catch (Exception ex) { errorMessage = ex.Message; LoggingService.LogError ("Error while search", ex); } string message = null; if (errorMessage != null) { message = GettextCatalog.GetString ("The search could not be finished: {0}", errorMessage); searchMonitor.ReportError (message, null); } else if (!searchMonitor.CancellationToken.IsCancellationRequested) { 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); } if (message != null) searchMonitor.ReportStatus (message); searchMonitor.Log.WriteLine (GettextCatalog.GetString ("Search time: {0} seconds."), (DateTime.Now - timer).TotalSeconds); } if (UpdateStopButton != null) { Application.Invoke (delegate { UpdateStopButton (); }); } }); }