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 GrepSearchResult.GrepMatch[] getFilePositions(string text, List<XPathPosition> positions) { bool[] endFound = new bool[positions.Count]; // Getting line lengths List<int> lineLengths = new List<int>(); using (StringReader reader = new StringReader(text)) { while (reader.Peek() >= 0) { lineLengths.Add(reader.ReadLine(true).Length); } } // These are absolute positions GrepSearchResult.GrepMatch[] results = new GrepSearchResult.GrepMatch[positions.Count]; if (positions.Count == 0) return results; using (StringReader textReader = new StringReader(text)) using (XmlReader reader = XmlReader.Create(textReader)) { List<int> currPos = new List<int>(); try { IXmlLineInfo lineInfo = ((IXmlLineInfo)reader); if (lineInfo.HasLineInfo()) { bool readyToBreak = false; // Parse the XML and display each node. while (reader.Read() && !readyToBreak) { switch (reader.NodeType) { case XmlNodeType.Element: if (currPos.Count <= reader.Depth) { currPos.Add(1); } else { currPos[reader.Depth]++; } break; case XmlNodeType.EndElement: while (reader.Depth < currPos.Count - 1) { currPos.RemoveAt(reader.Depth + 1); // currPos.Count - 1 would work too. } for (int i = 0; i < positions.Count; i++) { if (xPathPositionsMatch(currPos, positions[i].Path)) { endFound[i] = true; } } break; default: break; } if (reader.NodeType == XmlNodeType.EndElement) { for (int i = 0; i < positions.Count; i++) { if (endFound[i] && !xPathPositionsMatch(currPos, positions[i].Path)) { if (results[i] != null) { results[i].EndPosition = getAbsoluteCharPosition(lineInfo.LineNumber - 1, lineInfo.LinePosition - 3, text, lineLengths, true) + 1; } endFound[i] = false; } } } if (reader.NodeType == XmlNodeType.Element) { for (int i = 0; i < positions.Count; i++) { if (endFound[i]) { if (results[i] != null) { results[i].EndPosition = getAbsoluteCharPosition(lineInfo.LineNumber - 1, lineInfo.LinePosition - 3, text, lineLengths, true) + 1; } endFound[i] = false; } if (xPathPositionsMatch(currPos, positions[i].Path)) { results[i] = new GrepSearchResult.GrepMatch(0, getAbsoluteCharPosition(lineInfo.LineNumber - 1, lineInfo.LinePosition - 2, text, lineLengths, false), 0); } // If empty element (e.g.<element/>) if (reader.IsEmptyElement) { if (xPathPositionsMatch(currPos, positions[i].Path)) { endFound[i] = true; } } } } } } } finally { for (int i = 0; i < positions.Count; i++) { if (results[i] != null && results[i].Length == 0) results[i].EndPosition = text.Length - 1; } reader.Close(); } // Close the reader. } return results; }
public GrepSearchResult.GrepMatch[] getFilePositions(string text, List <XPathPosition> positions) { bool[] endFound = new bool[positions.Count]; // Getting line lengths List <int> lineLengths = new List <int>(); using (StringReader reader = new StringReader(text)) { while (reader.Peek() >= 0) { lineLengths.Add(reader.ReadLine(true).Length); } } // These are absolute positions GrepSearchResult.GrepMatch[] results = new GrepSearchResult.GrepMatch[positions.Count]; if (positions.Count == 0) { return(results); } using (StringReader textReader = new StringReader(text)) using (XmlReader reader = XmlReader.Create(textReader)) { List <int> currPos = new List <int>(); try { IXmlLineInfo lineInfo = ((IXmlLineInfo)reader); if (lineInfo.HasLineInfo()) { bool readyToBreak = false; // Parse the XML and display each node. while (reader.Read() && !readyToBreak) { switch (reader.NodeType) { case XmlNodeType.Element: if (currPos.Count <= reader.Depth) { currPos.Add(1); } else { currPos[reader.Depth]++; } break; case XmlNodeType.EndElement: while (reader.Depth < currPos.Count - 1) { currPos.RemoveAt(reader.Depth + 1); // currPos.Count - 1 would work too. } for (int i = 0; i < positions.Count; i++) { if (xPathPositionsMatch(currPos, positions[i].Path)) { endFound[i] = true; } } break; default: break; } if (reader.NodeType == XmlNodeType.EndElement) { for (int i = 0; i < positions.Count; i++) { if (endFound[i] && !xPathPositionsMatch(currPos, positions[i].Path)) { if (results[i] != null) { results[i].EndPosition = getAbsoluteCharPosition(lineInfo.LineNumber - 1, lineInfo.LinePosition - 3, text, lineLengths, true) + 1; } endFound[i] = false; } } } if (reader.NodeType == XmlNodeType.Element) { for (int i = 0; i < positions.Count; i++) { if (endFound[i]) { if (results[i] != null) { results[i].EndPosition = getAbsoluteCharPosition(lineInfo.LineNumber - 1, lineInfo.LinePosition - 3, text, lineLengths, true) + 1; } endFound[i] = false; } if (xPathPositionsMatch(currPos, positions[i].Path)) { results[i] = new GrepSearchResult.GrepMatch(lineInfo.LineNumber - 1, getAbsoluteCharPosition(lineInfo.LineNumber - 1, lineInfo.LinePosition - 2, text, lineLengths, false), 0); } // If empty element (e.g.<element/>) if (reader.IsEmptyElement) { if (xPathPositionsMatch(currPos, positions[i].Path)) { endFound[i] = true; } } } } } } } finally { for (int i = 0; i < positions.Count; i++) { if (results[i] != null && results[i].Length == 0) { results[i].EndPosition = text.Length - 1; } } reader.Close(); } // Close the reader. } return(results); }
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 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); }