Exemple #1
0
        private void Search(object sender, DoWorkEventArgs e)
        {
            string pattern = (string)e.Argument;
            string lastPattern;
            List<SearchResult> results = new List<SearchResult>();
            List<Document> resultScope = new List<Document>();

            if (pattern.Equals(string.Empty))
            {
                e.Result = SetResultArgs(pattern, results);
                return;
            }
            else if (_pastResults.ContainsKey(pattern))
            {
                e.Result = SetResultArgs(pattern, _pastResults[pattern].SearchResults);
                return;
            }
            else if (TryGetLastPattern(pattern, out lastPattern))
            {
                SetScope(_pastResults[lastPattern].ResultScope);
            }

            // Search
            Regex rgx = new Regex(Regex.Escape(pattern), RegexOptions.IgnoreCase);
            foreach (Document doc in _scope)
            {
                SearchResult result = new SearchResult(doc, pattern);

                string title = doc.DisplayName;
                MatchCollection titleMatches = rgx.Matches(title);
                result.SetTitleMatches(title, titleMatches);

                if (SettingUtils.GetSetting<bool>(Setting.SearchFileText))
                {
                    string text = WhitespaceRgx.Replace(doc.Text, " ");
                    MatchCollection textMatches = rgx.Matches(text);
                    result.SetTextMatches(text, textMatches);
                }

                if (result.HasMatches)
                {
                    results.Add(result);
                    resultScope.Add(doc);
                }
            }

            Triple t = new Triple(resultScope, results);
            _pastResults.Add(pattern, t);
            e.Result = SetResultArgs(pattern, results);
        }