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) {
                        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;
            }
        }
Example #2
0
        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;
            }
        }
Example #3
0
        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;
                var    results = new System.Collections.Concurrent.ConcurrentBag <SearchResult>();

                Parallel.ForEach(scope.GetFiles(monitor, filter), provider => {
                    if (monitor.IsCancelRequested)
                    {
                        return;
                    }
                    Interlocked.Increment(ref 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);
                        });
                        return;
                    }
                    foreach (SearchResult result in FindAll(monitor, provider, content, pattern, replacePattern, filter))
                    {
                        if (monitor.IsCancelRequested)
                        {
                            return;
                        }
                        FoundMatchesCount++;
                        results.Add(result);
                    }
                    if (replacePattern != null)
                    {
                        provider.EndReplace();
                    }
                    if (searchedFilesCount % step == 0)
                    {
                        monitor.Step(1);
                    }
                });
                return(results);
            } finally {
                monitor.EndTask();
                IsRunning = false;
            }
        }
Example #4
0
        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);


                var contents = new List <Tuple <FileProvider, string, List <SearchResult> > >();
                foreach (var provider in scope.GetFiles(monitor, filter))
                {
                    try {
                        contents.Add(Tuple.Create(provider, provider.ReadString(), new List <SearchResult> ()));
                    } catch (FileNotFoundException) {
                        MessageService.ShowError(string.Format(GettextCatalog.GetString("File {0} not found.")), provider.FileName);
                    }
                }

                var results = new List <SearchResult>();
                Parallel.ForEach(contents, content => {
                    if (monitor.IsCancelRequested)
                    {
                        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)
		{
			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, 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) {
						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;
			}
		}
Example #8
0
        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;
            }
        }
Example #9
0
		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;
			}
		}