/// <summary> /// Returns the given line with the search text highlighted. /// </summary> /// <param name="line">Line to check</param> /// <param name="grep">Grep Object containing options</param> /// <returns>Line with search text highlighted</returns> /// <history> /// [Curtis_Beard] 09/05/2006 Created /// </history> public static string GetHighlightLine(string line, Grep grep) { string newLine; if (grep.UseRegularExpressions) newLine = HighlightRegEx(line, grep); else newLine = HighlightNormal(line, grep); return newLine + "<br />"; }
/// <summary> /// Returns the given line with the search text highlighted. /// </summary> /// <param name="line">Line to check</param> /// <param name="grep">Grep Object containing options</param> /// <returns>Line with search text highlighted</returns> /// <history> /// [Curtis_Beard] 09/05/2006 Created /// [Curtis_Beard] 05/18/2006 FIX: 1723815, use correct whole word matching regex /// </history> private static string HighlightRegEx(string line, Grep grep) { string _textToSearch = string.Empty; string _tempstring = string.Empty; int _lastPos = 0; int _counter = 0; Regex _regEx = new Regex(grep.SearchText); MatchCollection _col; Match _item; string _newLine = string.Empty; //Retrieve hit text _textToSearch = line; // find all reg ex matches in line if (grep.UseCaseSensitivity && grep.UseWholeWordMatching) { _regEx = new Regex("\\b" + grep.SearchText + "\\b"); _col = _regEx.Matches(_textToSearch); } else if (grep.UseCaseSensitivity) { _regEx = new Regex(grep.SearchText); _col = _regEx.Matches(_textToSearch); } else if (grep.UseWholeWordMatching) { _regEx = new Regex("\\b" + grep.SearchText + "\\b", RegexOptions.IgnoreCase); _col = _regEx.Matches(_textToSearch); } else { _regEx = new Regex(grep.SearchText, RegexOptions.IgnoreCase); _col = _regEx.Matches(_textToSearch); } // loop through the matches _lastPos = 0; for (_counter = 0; _counter < _col.Count; _counter++) { _item = _col[_counter]; // check for empty string to prevent assigning nothing to selection text preventing // a system beep _tempstring = _textToSearch.Substring(_lastPos, _item.Index - _lastPos); if (!_tempstring.Equals(string.Empty)) _newLine += _tempstring; // set the hit text _newLine += string.Format("<span class=\"searchtext\">{0}</span>", _textToSearch.Substring(_item.Index, _item.Length)); // set the end text if (_counter + 1 >= _col.Count) { // no more hits so just set the rest _newLine += _textToSearch.Substring(_item.Index + _item.Length); _lastPos = _item.Index + _item.Length; } else { // another hit so just set inbetween _newLine += _textToSearch.Substring(_item.Index + _item.Length, _col[_counter + 1].Index - (_item.Index + _item.Length)); _lastPos = _col[_counter + 1].Index; } } if (_col.Count == 0) { // no match, just a context line _newLine += _textToSearch; } return _newLine; }
/// <summary> /// Replaces all the search option holders in the given text. /// </summary> /// <param name="text">Text containing holders</param> /// <param name="grep">Grep Object containing options</param> /// <returns>Text with holders replaced</returns> /// <history> /// [Curtis_Beard] 09/05/2006 Created /// </history> public static string ReplaceSearchOptions(string text, Grep grep) { text = text.Replace("%%filetypes%%", "File Types: " + grep.FileFilter); text = text.Replace("%%regex%%", "Regular Expressions: " + grep.UseRegularExpressions.ToString()); text = text.Replace("%%casesen%%", "Case Sensitive: " + grep.UseCaseSensitivity.ToString()); text = text.Replace("%%wholeword%%", "Whole Word: " + grep.UseWholeWordMatching.ToString()); text = text.Replace("%%recurse%%", "Recurse: " + grep.UseRecursion.ToString()); text = text.Replace("%%filenameonly%%", "Show File Names Only: " + grep.ReturnOnlyFileNames.ToString()); text = text.Replace("%%negation%%", "Negation: " + grep.UseNegation.ToString()); text = text.Replace("%%linenumbers%%", "Line Numbers: " + grep.IncludeLineNumbers.ToString()); text = text.Replace("%%contextlines%%", "Context Lines: " + grep.ContextLines.ToString()); text = text.Replace("%%totalfiles%%", grep.Greps.Count.ToString()); text = text.Replace("%%searchterm%%", grep.SearchText); if (grep.UseNegation) text = text.Replace("%%usenegation%%", "not "); else text = text.Replace("%%usenegation%%", string.Empty); return text; }
/// <summary> /// Returns the given line with the search text highlighted. /// </summary> /// <param name="line">Line to check</param> /// <param name="grep">Grep Object containing options</param> /// <returns>Line with search text highlighted</returns> /// <history> /// [Curtis_Beard] 09/05/2006 Created /// </history> private static string HighlightNormal(string line, Grep grep) { string _textToSearch = string.Empty; string _searchText = grep.SearchText; string _tempLine = string.Empty; string _begin = string.Empty; string _text = string.Empty; string _end = string.Empty; int _pos = 0; bool _highlight = false; string _newLine = string.Empty; // Retrieve hit text _textToSearch = line; _tempLine = _textToSearch; // attempt to locate the text in the line if (grep.UseCaseSensitivity) _pos = _tempLine.IndexOf(_searchText); else _pos = _tempLine.ToLower().IndexOf(_searchText.ToLower()); if (_pos > -1) { while (_pos > -1) { _highlight = false; //retrieve parts of text _begin = _tempLine.Substring(0, _pos); _text = _tempLine.Substring(_pos, _searchText.Length); _end = _tempLine.Substring(_pos + _searchText.Length); _newLine += _begin; // do a check to see if begin and end are valid for wholeword searches if (grep.UseWholeWordMatching) _highlight = Grep.WholeWordOnly(_begin, _end); else _highlight = true; // set highlight color for searched text if (_highlight) _newLine += string.Format("<span class=\"searchtext\">{0}</span>", _text); else _newLine += _text; // Check remaining string for other hits in same line if (grep.UseCaseSensitivity) _pos = _end.IndexOf(_searchText); else _pos = _end.ToLower().IndexOf(_searchText.ToLower()); // set default color for end, if no more hits in line _tempLine = _end; if (_pos < 0) _newLine += _end; } } else _newLine += _textToSearch; return _newLine; }
/// <summary> /// Start the searching /// </summary> /// <history> /// [Curtis_Beard] 10/17/2005 Created /// [Curtis_Beard] 07/03/2006 FIX: 1516775, Remove trim on the search expression /// [Curtis_Beard] 07/12/2006 CHG: moved thread actions to grep class /// [Curtis_Beard] 11/22/2006 CHG: Remove use of browse in combobox /// [Curtis_Beard] 08/07/2007 ADD: 1741735, better search error handling /// [Curtis_Beard] 08/21/2007 FIX: 1778467, make sure file pattern is correct if a '\' is present /// </history> private void StartSearch() { string _path; string _fileName; int _index; string _expression; try { _fileName = cboFileName.Text; _path = cboFilePath.Text.Trim(); _expression = cboSearchForText.Text; // update combo selections AddComboSelection(cboSearchForText, _expression); AddComboSelection(cboFileName, _fileName); AddComboSelection(cboFilePath, _path); // Ensure that there is a backslash. if (!_path.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString())) _path += System.IO.Path.DirectorySeparatorChar.ToString(); // update path and fileName if fileName has a path in it int slashPos = _fileName.LastIndexOf(System.IO.Path.DirectorySeparatorChar.ToString()); if (slashPos > -1) { // fileName has a slash, so append the directory and get the file filter _path += _fileName.Substring(0, slashPos); _fileName = _fileName.Substring(slashPos + 1); } // disable gui SetSearchState(false); // reset display SetStatusBarMessage(string.Empty); ClearItems(); txtHits.Clear(); // Clear search errors __ErrorCollection.Clear(); // begin searching __Grep = new Grep(); SetGrepOptions(); __Grep.StartDirectory = _path; __Grep.FileFilter = _fileName; __Grep.SearchText = _expression; // attach events __Grep.FileHit += new libFileSearch.Grep.FileHitHandler(ReceiveFileHit); __Grep.LineHit += new libFileSearch.Grep.LineHitHandler(ReceiveLineHit); __Grep.SearchCancel += new libFileSearch.Grep.SearchCancelHandler(ReceiveSearchCancel); __Grep.SearchComplete += new libFileSearch.Grep.SearchCompleteHandler(ReceiveSearchComplete); __Grep.SearchError += new libFileSearch.Grep.SearchErrorHandler(ReceiveSearchError); __Grep.SearchingFile += new libFileSearch.Grep.SearchingFileHandler(ReceiveSearchingFile); __Grep.BeginExecute(); } catch (Exception ex) { __ErrorCollection.Add(ex.Message); DisplaySearchErrors(); } }