public LazyResultsList(GrepSearchResult result, FormattedGrepResult formattedResult) { this.result = result; this.formattedResult = formattedResult; if ((result.Matches != null && result.Matches.Count > 0) || !result.IsSuccess) { GrepSearchResult.GrepLine emptyLine = new GrepSearchResult.GrepLine(-1, "", true, null); var dummyLine = new FormattedGrepLine(emptyLine, formattedResult, 30, false); this.Add(dummyLine); isLoaded = false; } }
public FormattedGrepLine(GrepSearchResult.GrepLine line, FormattedGrepResult parent, int initialColumnWidth) { Parent = parent; grepLine = line; Parent.PropertyChanged += new PropertyChangedEventHandler(Parent_PropertyChanged); LineNumberColumnWidth = initialColumnWidth; formattedLineNumber = (line.LineNumber == -1 ? "" : line.LineNumber.ToString()); //string fullText = lineSummary; if (line.IsContext) { style = "Context"; } if (line.LineNumber == -1 && line.LineText == "") { style = "Empty"; } }
private InlineCollection formatLine(GrepSearchResult.GrepLine line) { Paragraph paragraph = new Paragraph(); const int MAX_LINE_LENGTH = 500; string fullLine = line.LineText; if (line.LineText.Length > MAX_LINE_LENGTH) { fullLine = line.LineText.Substring(0, MAX_LINE_LENGTH); } if (line.Matches.Count == 0) { Run mainRun = new Run(fullLine); paragraph.Inlines.Add(mainRun); } else { int counter = 0; GrepSearchResult.GrepMatch[] lineMatches = new GrepSearchResult.GrepMatch[line.Matches.Count]; line.Matches.CopyTo(lineMatches); foreach (GrepSearchResult.GrepMatch m in lineMatches) { try { string regLine = null; string fmtLine = null; if (fullLine.Length < m.StartLocation + m.Length) { regLine = fullLine.Substring(counter, fullLine.Length - counter); } else { regLine = fullLine.Substring(counter, m.StartLocation - counter); fmtLine = fullLine.Substring(m.StartLocation, m.Length); } Run regularRun = new Run(regLine); paragraph.Inlines.Add(regularRun); if (fmtLine != null) { Run highlightedRun = new Run(fmtLine); highlightedRun.Background = Brushes.Yellow; paragraph.Inlines.Add(highlightedRun); } else { break; } } catch { Run regularRun = new Run(fullLine); paragraph.Inlines.Add(regularRun); } finally { counter = m.StartLocation + m.Length; } } if (counter < fullLine.Length) { try { string regLine = fullLine.Substring(counter); Run regularRun = new Run(regLine); paragraph.Inlines.Add(regularRun); } catch { Run regularRun = new Run(fullLine); paragraph.Inlines.Add(regularRun); } } if (line.LineText.Length > MAX_LINE_LENGTH) { string msg = string.Format("...(+{0:n0} characters)", line.LineText.Length - MAX_LINE_LENGTH); Run run = new Run(msg); run.Background = Brushes.AliceBlue; paragraph.Inlines.Add(run); var hiddenMatches = line.Matches.Where(m => m.StartLocation > MAX_LINE_LENGTH).Select(m => m); int count = hiddenMatches.Count(); if (count > 0) { paragraph.Inlines.Add(new Run(" additional matches:")); } // if close to getting them all, then take them all, // otherwise, stop at 20 and just show the remaining count int takeCount = count > 25 ? 20 : count; foreach (GrepSearchResult.GrepMatch m in hiddenMatches.Take(takeCount)) { paragraph.Inlines.Add(new Run(" ")); string fmtLine = line.LineText.Substring(m.StartLocation, m.Length); run = new Run(fmtLine); run.Background = Brushes.Yellow; paragraph.Inlines.Add(run); paragraph.Inlines.Add(new Run(string.Format(" at position {0}", m.StartLocation))); } if (count > takeCount) { paragraph.Inlines.Add(new Run(string.Format(", +{0} more matches", count - takeCount))); } } } return(paragraph.Inlines); }
public void Load(bool isAsync) { if (IsLoaded || IsLoading) { return; } IsLoading = true; if (!isAsync) { int currentLine = -1; List <GrepSearchResult.GrepLine> linesWithContext = new List <GrepSearchResult.GrepLine>(); if (GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext)) { linesWithContext = result.GetLinesWithContext(GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesBefore), GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesAfter)); } else { linesWithContext = result.GetLinesWithContext(0, 0); } if (this.Count == 1 && this[0].GrepLine.LineNumber == -1) { this.Clear(); } for (int i = 0; i < linesWithContext.Count; i++) { GrepSearchResult.GrepLine line = linesWithContext[i]; bool isSectionBreak = false; // Adding separator if (this.Count > 0 && GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext) && (currentLine != line.LineNumber && currentLine + 1 != line.LineNumber)) { isSectionBreak = true; } currentLine = line.LineNumber; if (currentLine <= 999 && LineNumberColumnWidth < 30) { LineNumberColumnWidth = 30; } else if (currentLine > 999 && LineNumberColumnWidth < 35) { LineNumberColumnWidth = 35; } else if (currentLine > 9999 && LineNumberColumnWidth < 47) { LineNumberColumnWidth = 47; } else if (currentLine > 99999 && LineNumberColumnWidth < 50) { LineNumberColumnWidth = 50; } this.Add(new FormattedGrepLine(line, formattedResult, LineNumberColumnWidth, isSectionBreak)); } IsLoaded = true; IsLoading = false; } else { int currentLine = -1; var asyncTask = Task.Factory.StartNew <List <GrepSearchResult.GrepLine> >(() => { List <GrepSearchResult.GrepLine> linesWithContext = new List <GrepSearchResult.GrepLine>(); if (GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext)) { linesWithContext = result.GetLinesWithContext(GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesBefore), GrepSettings.Instance.Get <int>(GrepSettings.Key.ContextLinesAfter)); } else { linesWithContext = result.GetLinesWithContext(0, 0); } return(linesWithContext); }).ContinueWith(task => { if (this.Count == 1 && this[0].GrepLine.LineNumber == -1) { if (Application.Current != null) { Application.Current.Dispatcher.Invoke(new Action(() => this.Clear() )); } } List <GrepSearchResult.GrepLine> linesWithContext = task.Result; List <FormattedGrepLine> tempList = new List <FormattedGrepLine>(); for (int i = 0; i < linesWithContext.Count; i++) { GrepSearchResult.GrepLine line = linesWithContext[i]; bool isSectionBreak = false; // Adding separator if (tempList.Count > 0 && GrepSettings.Instance.Get <bool>(GrepSettings.Key.ShowLinesInContext) && (currentLine != line.LineNumber && currentLine + 1 != line.LineNumber)) { isSectionBreak = true; } currentLine = line.LineNumber; if (currentLine <= 999 && LineNumberColumnWidth < 30) { LineNumberColumnWidth = 30; } else if (currentLine > 999 && LineNumberColumnWidth < 35) { LineNumberColumnWidth = 35; } else if (currentLine > 9999 && LineNumberColumnWidth < 47) { LineNumberColumnWidth = 47; } else if (currentLine > 99999 && LineNumberColumnWidth < 50) { LineNumberColumnWidth = 50; } tempList.Add(new FormattedGrepLine(line, formattedResult, LineNumberColumnWidth, isSectionBreak)); } if (Application.Current != null) { Application.Current.Dispatcher.Invoke(new Action(() => { foreach (var l in tempList) { this.Add(l); } } )); } IsLoaded = true; IsLoading = false; LoadFinished(this, EventArgs.Empty); }); } }
private InlineCollection formatLine(GrepSearchResult.GrepLine line) { Paragraph paragraph = new Paragraph(); var font = new FontFamily("Consolas"); if (line.Matches.Count == 0) { Run mainRun = new Run(line.LineText); paragraph.Inlines.Add(mainRun); } else { int counter = 0; string fullLine = line.LineText; GrepSearchResult.GrepMatch[] lineMatches = new GrepSearchResult.GrepMatch[line.Matches.Count]; line.Matches.CopyTo(lineMatches); foreach (GrepSearchResult.GrepMatch m in lineMatches) { try { string regLine = null; string fmtLine = null; if (fullLine.Length < m.StartLocation + m.Length) { regLine = fullLine; } else { regLine = fullLine.Substring(counter, m.StartLocation - counter); fmtLine = fullLine.Substring(m.StartLocation, m.Length); } Run regularRun = new Run(regLine); regularRun.FontFamily = font; paragraph.Inlines.Add(regularRun); if (fmtLine != null) { Run highlightedRun = new Run(fmtLine); highlightedRun.FontFamily = font; highlightedRun.Background = Brushes.Yellow; paragraph.Inlines.Add(highlightedRun); } else { break; } } catch { Run regularRun = new Run(fullLine); regularRun.FontFamily = font; paragraph.Inlines.Add(regularRun); } finally { counter = m.StartLocation + m.Length; } } if (counter < fullLine.Length) { try { string regLine = fullLine.Substring(counter); Run regularRun = new Run(regLine); regularRun.FontFamily = font; paragraph.Inlines.Add(regularRun); } catch { Run regularRun = new Run(fullLine); regularRun.FontFamily = font; paragraph.Inlines.Add(regularRun); } } } return(paragraph.Inlines); }
private void appendResults(GrepSearchResult result) { if (result == null) { return; } // Populate icon list string ext = Path.GetExtension(result.FileNameDisplayed); if (!treeViewExtensionList.Contains(ext)) { treeViewExtensionList.Add(ext); FileIcons.LoadImageList(treeViewExtensionList.ToArray()); tvSearchResult.ImageList = FileIcons.SmallIconList; } bool isFileReadOnly = Utils.IsReadOnly(result); string displayedName = Path.GetFileName(result.FileNameDisplayed); if (Properties.Settings.Default.ShowFilePathInResults && result.FileNameDisplayed.Contains(Utils.GetBaseFolder(tbFolderName.Text) + "\\")) { displayedName = result.FileNameDisplayed.Substring(Utils.GetBaseFolder(tbFolderName.Text).Length + 1); } int lineCount = Utils.MatchCount(result); if (lineCount > 0) { displayedName = string.Format("{0} ({1})", displayedName, lineCount); } if (isFileReadOnly) { displayedName = displayedName + " [read-only]"; } TreeNode node = new TreeNode(displayedName); node.Tag = result; tvSearchResult.Nodes.Add(node); node.ImageKey = ext; node.SelectedImageKey = node.ImageKey; node.StateImageKey = node.ImageKey; if (isFileReadOnly) { node.ForeColor = Color.DarkGray; } if (result.SearchResults != null) { for (int i = 0; i < result.SearchResults.Count; i++) { GrepSearchResult.GrepLine line = result.SearchResults[i]; string lineSummary = line.LineText.Replace("\n", "").Replace("\t", "").Replace("\r", "").Trim(); if (lineSummary.Length == 0) { lineSummary = " "; } else if (lineSummary.Length > 100) { lineSummary = lineSummary.Substring(0, 100) + "..."; } string lineNumber = (line.LineNumber == -1 ? "" : line.LineNumber + ": "); TreeNode lineNode = new TreeNode(lineNumber + lineSummary); lineNode.ImageKey = "%line%"; lineNode.SelectedImageKey = lineNode.ImageKey; lineNode.StateImageKey = lineNode.ImageKey; lineNode.Tag = line.LineNumber; if (!line.IsContext && Properties.Settings.Default.ShowLinesInContext) { lineNode.ForeColor = Color.Red; } node.Nodes.Add(lineNode); } } }