src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/Tree/Dependencies/GraphNodes/Actions/SearchGraphActionHandler.cs
/// <summary>
/// Generates search graph containing nodes matching search criteria in Solution Explorer 
/// and attaches it to correct top level node.
/// </summary>
Generates search graph containing nodes matching search criteria in Solution Explorer
and attaches it to correct top level node.
Generates search graph containing nodes matching search criteria in Solution Explorer and attaches it to correct top level node .
private void Search(IGraphContext graphContext)
        {
            string searchParametersTypeName = typeof(ISolutionSearchParameters).GUID.ToString();
            ISolutionSearchParameters searchParameters = graphContext.GetValue<ISolutionSearchParameters>(searchParametersTypeName);
            string? searchTerm = searchParameters?.SearchQuery.SearchString;
            if (searchTerm == null)
            {
                return;
            }
            var cachedDependencyToMatchingResultsMap = new Dictionary<string, HashSet<IDependency>>(StringComparers.DependencyTreeIds);
            var searchResultsPerContext = new Dictionary<string, HashSet<IDependency>>(StringComparers.Paths);
            System.Collections.Generic.IReadOnlyCollection<DependenciesSnapshot> snapshots = AggregateSnapshotProvider.GetSnapshots();
            foreach (DependenciesSnapshot snapshot in snapshots)
            {
                searchResultsPerContext[snapshot.ProjectPath] = SearchFlat(searchTerm, snapshot);
            }
            foreach (DependenciesSnapshot snapshot in snapshots)
            {
                IEnumerable<IDependency> allTopLevelDependencies = snapshot.GetFlatTopLevelDependencies();
                HashSet<IDependency> matchedDependencies = searchResultsPerContext[snapshot.ProjectPath];
                using var scope = new GraphTransactionScope();
                foreach (IDependency topLevelDependency in allTopLevelDependencies)
                {
                    TargetedDependenciesSnapshot targetedSnapshot = snapshot.DependenciesByTargetFramework[topLevelDependency.TargetFramework];
                    if (!cachedDependencyToMatchingResultsMap.TryGetValue(
                        topLevelDependency.Id,
                        out HashSet<IDependency>? topLevelDependencyMatches))
                    {
                        IDependenciesGraphViewProvider? viewProvider = FindViewProvider(topLevelDependency);
                        if (viewProvider == null)
                        {
                            continue;
                        }
                        if (!viewProvider.MatchSearchResults(
                            topLevelDependency,
                            searchResultsPerContext,
                            out topLevelDependencyMatches))
                        {
                            if (matchedDependencies.Count == 0)
                            {
                                continue;
                            }
                            topLevelDependencyMatches = GetMatchingResultsForDependency(
                                topLevelDependency,
                                targetedSnapshot,
                                matchedDependencies,
                                cachedDependencyToMatchingResultsMap);
                        }
                        cachedDependencyToMatchingResultsMap[topLevelDependency.Id] = topLevelDependencyMatches;
                    }
                    if (topLevelDependencyMatches.Count == 0)
                    {
                        continue;
                    }
                    GraphNode topLevelNode = _builder.AddTopLevelGraphNode(
                        graphContext,
                        snapshot.ProjectPath,
                        topLevelDependency.ToViewModel(targetedSnapshot));
                    foreach (IDependency matchedDependency in topLevelDependencyMatches)
                    {
                        GraphNode matchedDependencyNode = _builder.AddGraphNode(
                            graphContext,
                            snapshot.ProjectPath,
                            topLevelNode,
                            matchedDependency.ToViewModel(targetedSnapshot));
                        graphContext.Graph.Links.GetOrCreate(
                            topLevelNode,
                            matchedDependencyNode,
                            label: null,
                            GraphCommonSchema.Contains);
                    }
                    if (topLevelNode != null)
                    {
                        topLevelNode.AddCategory(CodeNodeCategories.ProjectItem);
                    }
                }
                scope.Complete();
            }
            graphContext.OnCompleted();
        }
private void Search ( IGraphContext graphContext ) { string searchParametersTypeName = typeof ( ISolutionSearchParameters ) . GUID . ToString ( ) ; ISolutionSearchParameters searchParameters = graphContext . GetValue < ISolutionSearchParameters > ( searchParametersTypeName ) ; string ? searchTerm = searchParameters ? . SearchQuery . SearchString ; if ( searchTerm == null ) { return ; } var cachedDependencyToMatchingResultsMap = new Dictionary < string , HashSet < IDependency > > ( StringComparers . DependencyTreeIds ) ; var searchResultsPerContext = new Dictionary < string , HashSet < IDependency > > ( StringComparers . Paths ) ; System . Collections . Generic . IReadOnlyCollection < DependenciesSnapshot > snapshots = AggregateSnapshotProvider . GetSnapshots ( ) ; foreach ( DependenciesSnapshot snapshot in snapshots ) { searchResultsPerContext [ snapshot . ProjectPath ] = SearchFlat ( searchTerm , snapshot ) ; } foreach ( DependenciesSnapshot snapshot in snapshots ) { IEnumerable < IDependency > allTopLevelDependencies = snapshot . GetFlatTopLevelDependencies ( ) ; HashSet < IDependency > matchedDependencies = searchResultsPerContext [ snapshot . ProjectPath ] ; using var scope = new GraphTransactionScope ( ) ; foreach ( IDependency topLevelDependency in allTopLevelDependencies ) { TargetedDependenciesSnapshot targetedSnapshot = snapshot . DependenciesByTargetFramework [ topLevelDependency . TargetFramework ] ; if ( ! cachedDependencyToMatchingResultsMap . TryGetValue ( topLevelDependency . Id , out HashSet < IDependency > ? topLevelDependencyMatches ) ) { IDependenciesGraphViewProvider ? viewProvider = FindViewProvider ( topLevelDependency ) ; if ( viewProvider == null ) { continue ; } if ( ! viewProvider . MatchSearchResults ( topLevelDependency , searchResultsPerContext , out topLevelDependencyMatches ) ) { if ( matchedDependencies . Count == 0 ) { continue ; } topLevelDependencyMatches = GetMatchingResultsForDependency ( topLevelDependency , targetedSnapshot , matchedDependencies , cachedDependencyToMatchingResultsMap ) ; } cachedDependencyToMatchingResultsMap [ topLevelDependency . Id ] = topLevelDependencyMatches ; } if ( topLevelDependencyMatches . Count == 0 ) { continue ; } GraphNode topLevelNode = _builder . AddTopLevelGraphNode ( graphContext , snapshot . ProjectPath , topLevelDependency . ToViewModel ( targetedSnapshot ) ) ; foreach ( IDependency matchedDependency in topLevelDependencyMatches ) { GraphNode matchedDependencyNode = _builder . AddGraphNode ( graphContext , snapshot . ProjectPath , topLevelNode , matchedDependency . ToViewModel ( targetedSnapshot ) ) ; graphContext . Graph . Links . GetOrCreate ( topLevelNode , matchedDependencyNode , label : null , GraphCommonSchema . Contains ) ; } if ( topLevelNode != null ) { topLevelNode . AddCategory ( CodeNodeCategories . ProjectItem ) ; } } scope . Complete ( ) ; } graphContext . OnCompleted ( ) ; }
Generates search graph containing nodes matching search criteria in Solution Explorer
and attaches it to correct top level node.
Generates search graph containing nodes matching search criteria in Solution Explorer and attaches it to correct top level node .
// 'node' is a GraphNode for top level dependency (which is part of solution explorer tree) // Setting ProjectItem category (and correct GraphNodeId) ensures that search graph appears  // under right solution explorer hierarchy item
graphContext
IGraphContext