private void AddMatchToResults(Match match, int matchCount, bool selectNode = false) { var onMatch = new Action <SearchLib.Match>(x => { var node = new TreeNode($"({x.LineNumber},{x.StartIndex}): {x.Line}"); node.Tag = match; var nodes = tvMatches.Nodes.Find(match.File, false); if (nodes.Length > 0) { var fileNode = nodes[0]; fileNode.Nodes.Add(node); if (fileNode.Nodes.Count == matchCount && !_expandedFirstNode) { _expandedFirstNode = true; fileNode.Expand(); SelectNode(fileNode); SelectNode(fileNode.Nodes[0], true); tvMatches.SelectedNode = fileNode.Nodes[0]; } } }); if (this.InvokeRequired) { this.Invoke(onMatch, match); } else { onMatch(match); } }
private void SelectNode(TreeNode node, bool scrollToCaret = false) { if (node == null) { return; } var match = node.Tag as SearchLib.Match; string file; if (match == null) { // file node file = node.Tag as string; UnhighlightPreviousSelectedLine(); foreach (TreeNode childNode in node.Nodes) { SelectNode(childNode, false); } return; } else { // match node file = match.File; } if (file == null) { return; } // Load file if not already loaded if (file != _currentFile) { _currentFile = file; _lastSelectedMatch = null; txtFileViewer.ReadOnly = false; txtFileViewer.Text = File.ReadAllText(file); txtFileViewer.ReadOnly = true; lblFileName.Text = file; } if (match != null) { if (scrollToCaret) { // unhighlight previously highlighted line, if any UnhighlightPreviousSelectedLine(); // unhighlight all words in current line UnHighlightSelection(match.LineStartIndex, match.LineEndIndex, HighlightLayer.HighlightWordLayer); // highlight line HighlightSelection(match.LineStartIndex, match.LineEndIndex, Color.LightGoldenrodYellow, HighlightLayer.LineLayer); // highlight word //Debug.WriteLine($"{file}, {match.Position}, {match.Position + match.Word.Length}"); HighlightSelection(match.Position, match.Position + match.Word.Length, Color.Orange, HighlightLayer.HighlightWordLayer); // Scroll view if selection is out of visible range int scrollStart = Math.Max(match.LineStartIndex, 0); int scrollEnd = Math.Min(match.LineEndIndex, txtFileViewer.Text.Length - 1); txtFileViewer.ScrollRange(scrollStart - 1, scrollEnd - 1); } else { HighlightSelection(match.Position, match.Position + match.Word.Length, Color.BurlyWood, HighlightLayer.WordLayer); } _lastSelectedMatch = match; } else { txtFileViewer.SetSelection(0, 0); } SetLineAndColumn(); }