private void ProcessSearchInFileOrPath(List<string> words, List<string> tempWords, ref bool found, string fileName, ref SearchResultItem res, ref bool searchInFile) { if (chbFilenameSearch.Checked) { string fn = Path.GetFileName(fileName); foreach (string word in words) { if (fn.Contains(word, !chbCaseSensitive.Checked)) { tempWords.Remove(word); } } if (chbFilenameOnly.Checked) { /// в этом случае поиск по файлу не интересует пользователя searchInFile = false; } else { /// хотя бы что-то должно найтись в названии файла, /// иначе файл пропускаем searchInFile = tempWords.Count < words.Count; } if (tempWords.Count == 0) { found = true; res = new SearchResultItem() { FileName = fileName, StartLineIndex = -1, EndLineIndex = -1, RelevantText = "" }; } } else if (chbSearchInPath.Checked) { /// поиск по пути, включая расширение файла string fn = fileName; foreach (string word in words) { if (fn.Contains(word, !chbCaseSensitive.Checked)) { tempWords.Remove(word); } } if (chbSearchInPathOnly.Checked) { /// в этом случае поиск по файлу не интересует пользователя searchInFile = false; } else { /// хотя бы что-то должно найтись в названии файла, /// иначе файл пропускаем searchInFile = tempWords.Count < words.Count; } if (tempWords.Count == 0) { found = true; res = new SearchResultItem() { FileName = fileName, StartLineIndex = -1, EndLineIndex = -1, RelevantText = "" }; } } }
private static void Search(int maxDistance, ref bool found, string fileName, ref SearchResultItem res, List<string> tempWords, bool searchInFile, ref List<string> wordsLeftToFind, ref int startLineIndex, ref int lastFoundWordLineIndex, List<string> lines, bool caseSensitive) { bool stopSearchInFile = false; for (int i = 0; i < lines.Count && !stopSearchInFile && searchInFile; i++) { if (lastFoundWordLineIndex == -1 || i - lastFoundWordLineIndex > maxDistance) { wordsLeftToFind = new List<string>(tempWords); } foreach (string word in tempWords) { if (lines[i].Contains(word, !caseSensitive)) { if (wordsLeftToFind.Count == tempWords.Count) startLineIndex = i; wordsLeftToFind.Remove(word); lastFoundWordLineIndex = i; } if (wordsLeftToFind.Count == 0) break; } if (wordsLeftToFind.Count == 0) { //stopSearch = true; string relevantText = ""; bool isTextStartCoutOut = false; if (lastFoundWordLineIndex - startLineIndex <= 5) { lines.Skip(startLineIndex) .Take(lastFoundWordLineIndex - startLineIndex + 1) .ToList() .ForEach(ln => relevantText += ln.WiseTrimTabsAtStart() + "\n"); } else { lines.Skip(lastFoundWordLineIndex - 5) .Take(6) .ToList() .ForEach(ln => relevantText += ln.WiseTrimTabsAtStart() + "\n"); isTextStartCoutOut = true; } res = new SearchResultItem() { FileName = fileName, StartLineIndex = startLineIndex, EndLineIndex = lastFoundWordLineIndex, RelevantText = relevantText, IsTextStartCutOut = isTextStartCoutOut }; found = true; stopSearchInFile = true; } } }