/// <summary>
        /// Returns a formatted file path with highlighted search hits.
        /// </summary>
        /// <param name="filePath">File path search hit.</param>
        /// <returns>Text run object.</returns>
        internal static TextBlock GetFormattedFilePath(SearchHitViewModel filePath)
        {
            var formattedPath = new TextBlock();

            string path      = filePath.FilePath;
            int    lastIndex = 0;

            foreach (var occurrence in filePath.SearchHit.Occurrences)
            {
                if (occurrence.Column > lastIndex)
                {
                    formattedPath.Inlines.Add(path.Substring(lastIndex, occurrence.Column - lastIndex));
                    lastIndex = occurrence.Column;
                }
                var inline = new Run(occurrence.Match);
                inline.Background = System.Windows.Media.Brushes.Aqua;
                formattedPath.Inlines.Add(inline);
                lastIndex = lastIndex + occurrence.Match.Length;
            }
            if (lastIndex < path.Length)
            {
                formattedPath.Inlines.Add(path.Substring(lastIndex));
            }
            return(formattedPath);
        }
Exemple #2
0
        /// <summary>
        /// Handles search completion.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event arguments.</param>
        private void searchWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            IsSearching = false;
            if (e.Result != null)
            {
                var result = (Tuple <IEnumerable <ISearchHit>, int, string>)e.Result;

                if (result.Item2 > 0)
                {
                    // Start background worker, which will count the total number of matches
                    if (!mRequery)
                    {
                        if (mCountMatches)
                        {
                            IsCountingMatches = true;
                            if (mSearchHitCountWorker != null)
                            {
                                mSearchHitCountWorker.CancelAsync();
                                DetachFromSearchHitCountBackgroundWorker();
                            }
                            mSearchHitCountWorker = new BackgroundWorker();
                            mSearchHitCountWorker.WorkerReportsProgress      = true;
                            mSearchHitCountWorker.WorkerSupportsCancellation = true;
                            mSearchHitCountWorker.DoWork             += searchHitCounter_DoWork;
                            mSearchHitCountWorker.RunWorkerCompleted += searchHitCounter_RunWorkerCompleted;
                            mSearchHitCountWorker.ProgressChanged    += searchHitCounter_ProgressChanged;
                            mSearchHitCountWorker.RunWorkerAsync(result.Item1);
                        }
                    }
                    var resultsEnumerable = new ObservableEnumerable();
                    resultsEnumerable.Enumerable = CreateViewModelsForSearchResults(result.Item1, resultsEnumerable);
                    SearchResults = resultsEnumerable;
                }
                else
                {
                    SearchResults   = new SearchHitViewModel[] {};
                    TotalMatchCount = 0;
                }

                // Set results.
                SearchResultCount  = result.Item2;
                SearchResultsQuery = result.Item3;
            }
            else
            {
                // Set results.
                SearchResultCount = 0;
                TotalMatchCount   = 0;
                SearchResults     = null;
            }

            // Restart search if requested.
            if (mRequery)
            {
                mRequery = false;
                StartSearch(mSearchType, mSearchQuery, mUseFileTypeFilter ? FileTypeFilter.AllowedFileTypes : null, mUseDirectoryFilter ? DirectoryFilter.AllowedDirectories : null);
            }
        }
        /// <summary>
        /// Returns an existing or creates a new view model for the specified search hit.
        /// </summary>
        /// <param name="searchHit">Search hit for which a view model should be returned, or created.</param>
        /// <param name="parent">Parent enumerable</param>
        /// <returns>Created or existing view model.</returns>
        public static SearchHitViewModel GetOrCreateSearchHitViewModel(Engine.Interfaces.ISearchHit searchHit, ObservableEnumerable parent)
        {
            SearchHitViewModel viewModel = null;

            if (!mViewModels.TryGetValue(searchHit, out viewModel))
            {
                viewModel = new SearchHitViewModel(searchHit, parent);
                mViewModels.Add(searchHit, viewModel);
            }
            return(viewModel);
        }
Exemple #4
0
        /// <summary>
        /// Processes search results and creates a SearchHitViewModel for each search result.
        /// </summary>
        /// <param name="searchResults">Search results, for which view models should be created.</param>
        /// <returns></returns>
        private static IEnumerable CreateViewModelsForSearchResults(IEnumerable <ISearchHit> searchResults, ObservableEnumerable parent)
        {
            if (searchResults != null)
            {
                foreach (var searchResult in searchResults)
                {
                    SearchHitViewModel viewModel = ViewModel.SearchHitViewModel.GetOrCreateSearchHitViewModel(searchResult, parent);
                    yield return(viewModel);

                    if (viewModel.IsExpanded)
                    {
                        foreach (var occurrence in viewModel.Occurrences)
                        {
                            yield return(occurrence);
                        }
                    }
                }
            }
            yield break;
        }