Exemplo n.º 1
0
        void SearchReplace(string replacePattern)
        {
            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();
                }
            }

            Scope scope = GetScope();

            if (scope == null)
            {
                return;
            }

            ISearchProgressMonitor searchMonitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor(true);

            lock (searchesInProgress)
                searchesInProgress.Add(searchMonitor);
            UpdateStopButton();
            find = new FindReplace();

            string        pattern = comboboxentryFind.Entry.Text;
            FilterOptions options = GetFilterOptions();

            searchMonitor.ReportStatus(scope.GetDescription(options, pattern, null));

            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;
            }

            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);
            searchMonitor.Dispose();
            searchesInProgress.Remove(searchMonitor);
            UpdateStopButton();
        }
Exemplo n.º 2
0
		void SearchReplace (string replacePattern)
		{
			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 ();
				}
			}
			
			Scope scope = GetScope ();
			if (scope == null)
				return;
			
			ISearchProgressMonitor searchMonitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true);
			lock (searchesInProgress)
				searchesInProgress.Add (searchMonitor);
			UpdateStopButton ();
			find = new FindReplace ();
			
			string pattern = comboboxentryFind.Entry.Text;
			FilterOptions options = GetFilterOptions ();
			searchMonitor.ReportStatus (scope.GetDescription (options, pattern, null));

			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;
			}

			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);
			searchMonitor.Dispose ();
			searchesInProgress.Remove (searchMonitor);
			UpdateStopButton ();
		}
Exemplo n.º 3
0
        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, cancellationTokenSource: cancelSource)) {
                    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;
                    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();
                    });
                }
            });
        }
Exemplo n.º 4
0
		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 ();
					});
				}
			});
		}