Support Object Explorer FindNodes request (#589)

This commit is contained in:
Matt Irvine
2018-03-15 10:47:52 -07:00
committed by GitHub
parent d36efb578c
commit 365fe2282e
8 changed files with 584 additions and 33 deletions

View File

@@ -41,37 +41,38 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer
return VisitChildAndParents(child.Parent, visitor);
}
/// <summary>
/// Finds a node by traversing the tree starting from the given node through all the children
/// </summary>
/// <param name="node">node to start traversing at</param>
/// <summary>
/// Finds a node by traversing the tree starting from the given node through all the children
/// </summary>
/// <param name="node">node to start traversing at</param>
/// <param name="condition">Predicate function that accesses the tree and
/// determines whether to stop going further up the tree</param>
/// <param name="filter">Predicate function to filter the children when traversing</param>
/// determines whether to stop going further up the tree</param>
/// <param name="filter">Predicate function to filter the children when traversing</param>
/// <returns>A Tree Node that matches the condition</returns>
public static TreeNode FindNode(TreeNode node, Predicate<TreeNode> condition, Predicate<TreeNode> filter)
{
if(node == null)
{
return null;
}
if (condition(node))
{
return node;
}
foreach (var child in node.GetChildren())
{
if (filter != null && filter(child))
{
TreeNode childNode = FindNode(child, condition, filter);
if (childNode != null)
{
return childNode;
}
}
}
return null;
}
public static TreeNode FindNode(TreeNode node, Predicate<TreeNode> condition, Predicate<TreeNode> filter, bool refreshChildren = false)
{
if(node == null)
{
return null;
}
if (condition(node))
{
return node;
}
var children = refreshChildren && !node.IsAlwaysLeaf ? node.Refresh() : node.GetChildren();
foreach (var child in children)
{
if (filter != null && filter(child))
{
TreeNode childNode = FindNode(child, condition, filter, refreshChildren);
if (childNode != null)
{
return childNode;
}
}
}
return null;
}
}
}