|
5 | 5 |
|
6 | 6 | using System;
|
7 | 7 | using System.Collections.Generic;
|
| 8 | +using HeapExplorer.Utilities; |
8 | 9 | using UnityEngine;
|
9 | 10 | using UnityEditor.IMGUI.Controls;
|
10 | 11 | using UnityEditor;
|
| 12 | +using static HeapExplorer.Utilities.Option; |
11 | 13 |
|
12 | 14 | namespace HeapExplorer
|
13 | 15 | {
|
@@ -36,7 +38,6 @@ public HeapExplorerWindow window
|
36 | 38 | IList<int> m_Expanded = new List<int>(32);
|
37 | 39 | TreeViewItem m_Tree;
|
38 | 40 | string[] m_SearchCache = new string[32];
|
39 |
| - System.Text.StringBuilder m_SearchBuilder = new System.Text.StringBuilder(); |
40 | 41 |
|
41 | 42 | public AbstractTreeView(HeapExplorerWindow window, string editorPrefsKey, TreeViewState state)
|
42 | 43 | : base(state)
|
@@ -301,23 +302,36 @@ protected override bool DoesItemMatchSearch(TreeViewItem item, string search)
|
301 | 302 | var i = item as AbstractTreeViewItem;
|
302 | 303 | if (i != null)
|
303 | 304 | {
|
304 |
| - int searchCount; |
305 |
| - string type; |
306 |
| - string label; |
307 |
| - i.GetItemSearchString(m_SearchCache, out searchCount, out type, out label); |
| 305 | + if (!i.m_MaybeCachedItemSearchString.valueOut(out var searchString)) { |
| 306 | + int searchCount; |
| 307 | + string type; |
| 308 | + string label; |
| 309 | + i.GetItemSearchString(m_SearchCache, out searchCount, out type, out label); |
| 310 | + |
| 311 | + var names = new List<string>(capacity: searchCount); |
| 312 | + for (var n=0; n < searchCount; ++n) |
| 313 | + { |
| 314 | + var str = m_SearchCache[n]; |
| 315 | + if (!string.IsNullOrEmpty(str)) names.Add(str.ToLowerInvariant()); |
| 316 | + } |
308 | 317 |
|
309 |
| - if (!m_Search.IsTypeMatch(type) || !m_Search.IsLabelMatch(label)) |
310 |
| - return false; |
| 318 | + searchString = new AbstractTreeViewItem.Cache( |
| 319 | + lowerCasedNames: names.ToArray(), type: type, label: label |
| 320 | + ); |
| 321 | + i.m_MaybeCachedItemSearchString = Some(searchString); |
| 322 | + } |
311 | 323 |
|
312 |
| - m_SearchBuilder.Length = 0; |
313 |
| - for (var n=0; n < searchCount; ++n) |
314 |
| - { |
315 |
| - m_SearchBuilder.Append(m_SearchCache[n]); |
316 |
| - m_SearchBuilder.Append(" "); |
| 324 | + if (!m_Search.IsTypeMatch(searchString.type) || !m_Search.IsLabelMatch(searchString.label)) { |
| 325 | + return false; |
317 | 326 | }
|
318 |
| - m_SearchBuilder.Append("\0"); |
| 327 | + else { |
| 328 | + // ReSharper disable once LoopCanBeConvertedToQuery |
| 329 | + foreach (var lowerCasedName in searchString.lowerCasedNames) { |
| 330 | + if (m_Search.IsNameMatch(lowerCasedName)) return true; |
| 331 | + } |
319 | 332 |
|
320 |
| - return m_Search.IsNameMatch(m_SearchBuilder.ToString()); |
| 333 | + return false; |
| 334 | + } |
321 | 335 | }
|
322 | 336 |
|
323 | 337 | return base.DoesItemMatchSearch(item, search);
|
@@ -461,7 +475,34 @@ public virtual void GetItemSearchString(string[] target, out int count, out stri
|
461 | 475 | label = null;
|
462 | 476 | }
|
463 | 477 |
|
| 478 | + /// <summary> |
| 479 | + /// Results of <see cref="GetItemSearchString"/> are cached here to avoid re-computation. If this is `None`, |
| 480 | + /// invoke the <see cref="GetItemSearchString"/> and store the result here. |
| 481 | + /// </summary> |
| 482 | + public Option<Cache> m_MaybeCachedItemSearchString; |
| 483 | + |
464 | 484 | public abstract void OnGUI(Rect position, int column);
|
| 485 | + |
| 486 | + public sealed class Cache { |
| 487 | + /// <summary> |
| 488 | + /// Parameters for <see cref="SearchTextParser.Result.IsNameMatch"/>. |
| 489 | + /// <para/> |
| 490 | + /// The search will match if any of these matches. |
| 491 | + /// </summary> |
| 492 | + public readonly string[] lowerCasedNames; |
| 493 | + |
| 494 | + /// <summary>Parameter for <see cref="SearchTextParser.Result.IsTypeMatch"/>.</summary> |
| 495 | + public readonly string type; |
| 496 | + |
| 497 | + /// <summary>Parameter for <see cref="SearchTextParser.Result.IsLabelMatch"/>.</summary> |
| 498 | + public readonly string label; |
| 499 | + |
| 500 | + public Cache(string[] lowerCasedNames, string type, string label) { |
| 501 | + this.lowerCasedNames = lowerCasedNames; |
| 502 | + this.type = type; |
| 503 | + this.label = label; |
| 504 | + } |
| 505 | + } |
465 | 506 | }
|
466 | 507 |
|
467 | 508 | public static class TreeViewUtility
|
|
0 commit comments