示例#1
0
        public override void BeginSearch(ISearchContext context, string query)
        {
            if (!searchSessions.ContainsKey(context.guid))
            {
                return;
            }
            base.BeginSearch(context, query);

            var searchSession = searchSessions[context.guid];

            searchSession.context.searchText = query;
            searchSession.context.wantsMore  = true;
            if (context.requiredTypeNames != null && context.requiredTypeNames.Any())
            {
                searchSession.context.filterType = Utils.GetTypeFromName(context.requiredTypeNames.First());
            }
            else
            {
                searchSession.context.filterType = typeof(GameObject);
            }

            if (!m_SearchItemsBySession.ContainsKey(context.guid))
            {
                m_SearchItemsBySession.Add(context.guid, new HashSet <int>());
            }
            var searchItemsSet = m_SearchItemsBySession[context.guid];

            searchItemsSet.Clear();

            foreach (var id in SearchService.GetItems(searchSession.context, SearchFlags.Synchronous).Select(item => Convert.ToInt32(item.id)))
            {
                searchItemsSet.Add(id);
            }
        }
示例#2
0
        public IEnumerator FetchItems1()
        {
            var ctx = new SearchContext {
                searchText = "p:test_material_42", wantsMore = true
            };

            var fetchedItems = SearchService.GetItems(ctx);

            while (AsyncSearchSession.SearchInProgress)
            {
                yield return(null);
            }

            Assert.IsNotEmpty(fetchedItems);
            var foundItem = fetchedItems.Find(item => item.label == Path.GetFileName(k_TestFileName));

            Assert.IsNotNull(foundItem);
            Assert.IsNotNull(foundItem.id);
            Assert.IsNull(foundItem.description);

            Assert.IsNotNull(foundItem.provider);
            Assert.IsNotNull(foundItem.provider.fetchDescription);
            var fetchedDescription = foundItem.provider.fetchDescription(foundItem, ctx);

            Assert.IsTrue(fetchedDescription.Contains(k_TestFileName));
        }
示例#3
0
        public void Resolve(Action <IEnumerable <SearchItem> > onSearchItemReceived, Action <SearchRequest> finishedHandler)
        {
            if (onSearchItemReceived != null)
            {
                resultsReceived += onSearchItemReceived;
            }

            if (finishedHandler != null)
            {
                resolved += finishedHandler;
            }

            DebugLog($"Resolving <b>{type}</b>");
            resolving = true;
            while (pendingQueries.Count > 0)
            {
                var r = pendingQueries.Dequeue();
                runningQueries.Add(r);

                r.sessionEnded      += OnSearchEnded;
                r.asyncItemReceived += OnSearchItemsReceived;

                DebugLog($"Fetching items with <a>{r.searchQuery}</a>");
                SearchService.GetItems(r, SearchFlags.FirstBatchAsync);
            }

            UpdateState();

            // Check if we still have some pending queries to resolve.
            if (resolving)
            {
                EditorApplication.update -= DeferredResolve;
                EditorApplication.update += DeferredResolve;
            }
        }
示例#4
0
        public virtual IEnumerable <string> Search(ISearchContext context, string query, Action <IEnumerable <string> > asyncItemsReceived)
        {
            if (!searchSessions.ContainsKey(context.guid))
            {
                return new string[] { }
            }
            ;

            var searchSession = searchSessions[context.guid];

            if (asyncItemsReceived != null)
            {
                searchSession.onAsyncItemsReceived = asyncItemsReceived;
            }

            if (context.requiredTypeNames != null && context.requiredTypeNames.Any())
            {
                searchSession.context.wantsMore  = true;
                searchSession.context.filterType = Utils.GetTypeFromName(context.requiredTypeNames.First());
            }
            else
            {
                searchSession.context.wantsMore  = false;
                searchSession.context.filterType = null;
            }
            searchSession.context.searchText = query;
            var items = SearchService.GetItems(searchSession.context);

            return(items.Select(item => item.id));
        }
    }
示例#5
0
        public IEnumerator FetchItems()
        {
            var ctx = new SearchContext {
                searchText = "test_material_42", wantsMore = true
            };

            var fetchedItems = SearchService.GetItems(ctx);

            while (AsyncSearchSession.SearchInProgress)
            {
                yield return(null);
            }

            Assert.IsNotEmpty(fetchedItems);
            var foundItem = fetchedItems.Find(item => item.label == Path.GetFileName(k_TestFileName));

            Assert.IsNotNull(foundItem);
            Assert.IsNotNull(foundItem.id);
            Assert.IsNull(foundItem.description);

            Assert.IsNotNull(foundItem.provider);
            Assert.IsNotNull(foundItem.provider.fetchDescription);
            var fetchedDescription = foundItem.provider.fetchDescription(foundItem, ctx);

            Assert.AreEqual("Packages/com.unity.quicksearch/Tests/Editor/Content/test_material_42.mat (2.0 KB)", fetchedDescription);
        }
示例#6
0
        public IEnumerator FetchItemsAsync()
        {
            const string k_SearchType = "___test_async";
            var          ctx          = new SearchContext {
                searchText = k_SearchType + ":async", wantsMore = true
            };

            bool wasCalled = false;

            void OnAsyncCallback(IEnumerable <SearchItem> items)
            {
                wasCalled = items.Any(i => i.id == k_SearchType);
            }

            IEnumerable <SearchItem> TestFetchAsyncCallback(SearchProvider provider)
            {
                var ft = UnityEditor.EditorApplication.timeSinceStartup + 1;

                while (UnityEditor.EditorApplication.timeSinceStartup < ft)
                {
                    yield return(null);
                }

                yield return(provider.CreateItem(k_SearchType));
            }

            var testSearchProvider = new SearchProvider(k_SearchType, k_SearchType)
            {
                filterId           = k_SearchType + ":",
                isExplicitProvider = true,
                fetchItems         = (context, items, provider) => TestFetchAsyncCallback(provider)
            };

            AsyncSearchSession.asyncItemReceived += OnAsyncCallback;

            SearchService.Providers.Add(testSearchProvider);
            SearchService.RefreshProviders();

            SearchService.GetItems(ctx);
            yield return(null);

            while (AsyncSearchSession.SearchInProgress)
            {
                yield return(null);
            }

            while (!wasCalled)
            {
                yield return(null);
            }

            Assert.True(wasCalled);

            SearchService.Providers.Remove(testSearchProvider);
            SearchService.RefreshProviders();
            AsyncSearchSession.asyncItemReceived -= OnAsyncCallback;
        }
示例#7
0
 public void Refresh()
 {
     SearchService.SearchTextChanged(m_Context);
     m_FilteredItems    = SearchService.GetItems(m_Context);
     m_SelectedIndex    = -1;
     m_ScrollPosition.y = 0;
     UpdateWindowTitle();
     Repaint();
 }
示例#8
0
        public virtual IEnumerable <string> Search(string query, UnityEditor.SearchService.ISearchContext context, Action <IEnumerable <string> > asyncItemsReceived)
        {
            var searchContext = new SearchContext();

            if (context.requiredTypeNames != null && context.requiredTypeNames.Any())
            {
                searchContext.wantsMore  = true;
                searchContext.filterType = Utils.GetTypeFromName(context.requiredTypeNames.First());
            }
            searchContext.searchText = query;
            var items = SearchService.GetItems(searchContext, provider);

            return(items.Select(item => item.id));
        }
        static void HandleQueryRequests(int connectionId, Dictionary <string, object> queryData)
        {
            var requestId    = (long)queryData["requestId"];
            var query        = queryData["query"] as string;
            var fetchOptions = (FetchOptions)Enum.Parse(typeof(FetchOptions), queryData["fetchOptions"].ToString());
            var maxCount     = (long)queryData["maxCount"];

            if (!s_SearchRequestsById.TryGetValue(connectionId, out var request))
            {
                request = new ClientSearchRequest
                {
                    // Create a new search context
                    context = new SearchContext(SearchService.Providers.Where(p => p.active), "")
                };
                request.context.asyncItemReceived += (context, items) =>
                {
                    var itemsList = items.ToList();
                    if (request.maxCount > -1)
                    {
                        var remainingItems = request.maxCount - request.currentCount;
                        if (remainingItems < 1)
                        {
                            return;
                        }
                        itemsList = itemsList.Take(remainingItems).ToList();
                    }
                    request.currentCount += itemsList.Count;
                    CacheResults(request, itemsList);
                    SendResults(connectionId, request, SearchRequestType.Query, fetchOptions, itemsList);
                };
                s_SearchRequestsById.Add(connectionId, request);
            }

            request.context.sessions.StopAllAsyncSearchSessions();
            request.context.searchText = query;
            request.requestId          = requestId;
            request.maxCount           = (int)maxCount;
            var initialResults = SearchService.GetItems(request.context);

            if (request.maxCount > -1)
            {
                initialResults = initialResults.Take(request.maxCount).ToList();
            }
            request.currentCount   = initialResults.Count;
            request.temporaryCache = new Dictionary <string, SearchItem>();
            CacheResults(request, initialResults);
            SendResults(connectionId, request, SearchRequestType.Query, fetchOptions, initialResults);
        }
示例#10
0
        public IEnumerator SearchCompleteFilenames([ValueSource(nameof(s_QueryVariations))] string query)
        {
            var ctx = new SearchContext {
                searchText = query
            };

            var fetchedItems = SearchService.GetItems(ctx);

            while (AsyncSearchSession.SearchInProgress)
            {
                yield return(null);
            }

            Assert.IsNotEmpty(fetchedItems);
            Assert.True(fetchedItems.Any(item => item.label == "test_material_42.mat"));
        }
示例#11
0
        public IEnumerator FetchItems3()
        {
            var fetchedItems = SearchService.GetItems(new SearchContext {
                searchText = "p:t:material 42"
            });

            while (AsyncSearchSession.SearchInProgress)
            {
                yield return(null);
            }

            Assert.IsNotEmpty(fetchedItems);
            var foundItem = fetchedItems.Find(item => item.label == Path.GetFileName(k_TestFileName));

            Assert.IsNotNull(foundItem);
            Assert.IsTrue(foundItem.id.EndsWith(k_TestFileName));
        }
示例#12
0
        public override void BeginSearch(string query, UnityEditor.SearchService.ISearchContext context)
        {
            base.BeginSearch(query, context);
            this.context.searchText = query;
            this.context.wantsMore  = true;
            if (context.requiredTypeNames != null && context.requiredTypeNames.Any())
            {
                this.context.filterType = Utils.GetTypeFromName(context.requiredTypeNames.First());
            }
            else
            {
                this.context.filterType = typeof(GameObject);
            }

            m_SearchItems = new HashSet <int>();
            foreach (var id in SearchService.GetItems(this.context, SearchFlags.Synchronous).Select(item => Convert.ToInt32(item.id)))
            {
                m_SearchItems.Add(id);
            }
        }
示例#13
0
        public virtual IEnumerable <string> Search(string query, UnityEditor.SearchService.ISearchContext context, Action <IEnumerable <string> > asyncItemsReceived)
        {
            if (asyncItemsReceived != null)
            {
                m_OnAsyncItemReceived = asyncItemsReceived;
            }

            if (context.requiredTypeNames != null && context.requiredTypeNames.Any())
            {
                this.context.wantsMore  = true;
                this.context.filterType = Utils.GetTypeFromName(context.requiredTypeNames.First());
            }
            else
            {
                this.context.wantsMore  = false;
                this.context.filterType = null;
            }
            this.context.searchText = query;
            var items = SearchService.GetItems(this.context);

            return(items.Select(item => item.id));
        }
示例#14
0
        public void FetchItems()
        {
            var ctx = new SearchContext {
                searchText = "test", wantsMore = true
            };

            Assert.AreEqual(0, ctx.searchId);

            var fetchedItems = SearchService.GetItems(ctx);

            Assert.AreEqual(1, ctx.searchId);
            Assert.IsNotEmpty(fetchedItems);
            var foundItem = fetchedItems.Find(item => item.label == Path.GetFileName(k_TestFileName));

            Assert.IsNotNull(foundItem.id);
            Assert.IsNull(foundItem.description);

            Assert.IsNotNull(foundItem.provider);
            Assert.IsNotNull(foundItem.provider.fetchDescription);
            var fetchedDescription = foundItem.provider.fetchDescription(foundItem, ctx);

            Assert.AreEqual("Packages/com.unity.quicksearch/Tests/Editor/Content/test_material_42.mat (2.0 KB)", fetchedDescription);
        }
示例#15
0
        private void DrawToolbar(SearchContext context)
        {
            if (context == null)
            {
                return;
            }

            GUILayout.BeginHorizontal(Styles.toolbar);
            {
                var rightRect = EditorGUILayout.GetControlRect(GUILayout.MaxWidth(32f), GUILayout.ExpandHeight(true));
                if (EditorGUI.DropdownButton(rightRect, Styles.filterButtonContent, FocusType.Passive, Styles.filterButton) || m_ShowFilterWindow)
                {
                    if (FilterWindow.canShow)
                    {
                        rightRect.x += 12f; rightRect.y -= 3f;
                        if (m_ShowFilterWindow)
                        {
                            rightRect.y += 30f;
                        }

                        m_ShowFilterWindow = false;
                        if (FilterWindow.ShowAtPosition(this, rightRect))
                        {
                            GUIUtility.ExitGUI();
                        }
                    }
                }

                EditorGUI.BeginChangeCheck();

                using (new BlinkCursorScope(m_CursorBlinking, new Color(0, 0, 0, 0.01f)))
                {
                    var userSearchQuery = context.searchBoxText;
                    if (!String.IsNullOrEmpty(m_CycledSearch) && (Event.current.type == EventType.Repaint || Event.current.type == EventType.Layout))
                    {
                        userSearchQuery  = m_CycledSearch;
                        m_CycledSearch   = null;
                        m_SearchBoxFocus = true;
                        GUI.changed      = true;
                    }

                    GUI.SetNextControlName(k_QuickSearchBoxName);
                    context.searchBoxText = EditorGUILayout.TextField(userSearchQuery, Styles.searchField, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
                }

                if (String.IsNullOrEmpty(context.searchBoxText))
                {
                    EditorGUI.BeginDisabledGroup(true);
                    EditorGUI.TextArea(GUILayoutUtility.GetLastRect(), "search anything...", Styles.placeholderTextStyle);
                    EditorGUI.EndDisabledGroup();
                }
                if (!String.IsNullOrEmpty(context.searchBoxText))
                {
                    if (GUILayout.Button(Icons.clear, Styles.searchFieldClear, GUILayout.Width(24), GUILayout.Height(24)))
                    {
                        context.searchBoxText = "";
                        GUI.changed           = true;
                        GUI.FocusControl(null);
                    }
                }

                if (EditorGUI.EndChangeCheck() || m_FilteredItems == null)
                {
                    m_SelectedIndex    = -1;
                    m_ScrollPosition.y = 0;
                    SearchService.SearchTextChanged(context);
                    m_FilteredItems = SearchService.GetItems(context);
                    UpdateWindowTitle();
                }

                #if QUICKSEARCH_DEBUG
                DrawDebugTools();
                #endif
            }
            GUILayout.EndHorizontal();
        }