No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearchWindowAdapter.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. using UnityEditor.ShaderGraph.Drawing;
  7. using UnityEditor.Searcher;
  8. namespace UnityEditor.ShaderGraph
  9. {
  10. public class SearchWindowAdapter : SearcherAdapter
  11. {
  12. readonly VisualTreeAsset m_DefaultItemTemplate;
  13. public override bool HasDetailsPanel => false;
  14. public SearchWindowAdapter(string title) : base(title)
  15. {
  16. m_DefaultItemTemplate = Resources.Load<VisualTreeAsset>("SearcherItem");
  17. }
  18. private SearcherItem GetFirstChildItem(SearcherItem item)
  19. {
  20. if (item.Children.Count != 0)
  21. {
  22. SearcherItem childIterator = null;
  23. // Discard searcher item for selection if it is a category, get next best child item from it instead
  24. // There is no utility in selecting category headers/titles, only the leaf entries
  25. childIterator = item.Children[0];
  26. while (childIterator != null && childIterator.Children.Count != 0)
  27. {
  28. childIterator = childIterator.Children[0];
  29. }
  30. item = childIterator;
  31. }
  32. return item;
  33. }
  34. private int ComputeScoreForMatch(string[] queryTerms, SearcherItem matchItem)
  35. {
  36. // Scoring Criteria:
  37. // - Exact name match is most preferred.
  38. // - Partial name match is next.
  39. // - Exact synonym match is next.
  40. // - Partial synonym match is next.
  41. // - No match is last.
  42. int score = 0;
  43. // Split the entry name so that we can remove suffix that looks like "Clamp: In(4)"
  44. var nameSansSuffix = matchItem.Name.Split(':').First();
  45. int nameCharactersMatched = 0;
  46. foreach (var queryWord in queryTerms)
  47. {
  48. if (nameSansSuffix.Contains(queryWord, StringComparison.OrdinalIgnoreCase))
  49. {
  50. score += 100000;
  51. nameCharactersMatched += queryWord.Length;
  52. }
  53. // Check for synonym matches -- give a bonus to each
  54. if (matchItem.Synonyms != null)
  55. {
  56. foreach (var syn in matchItem.Synonyms)
  57. {
  58. if (syn.Equals(queryWord, StringComparison.OrdinalIgnoreCase))
  59. {
  60. score += 10000;
  61. }
  62. else if (syn.Contains(queryWord, StringComparison.OrdinalIgnoreCase))
  63. {
  64. score += 1000;
  65. score -= (syn.Length - queryWord.Length);
  66. }
  67. }
  68. }
  69. }
  70. if (nameCharactersMatched > 0)
  71. {
  72. int unmatchedCharacters = (nameSansSuffix.Length - nameCharactersMatched);
  73. score -= unmatchedCharacters;
  74. }
  75. return score;
  76. }
  77. public override SearcherItem OnSearchResultsFilter(IEnumerable<SearcherItem> searchResults, string searchQuery)
  78. {
  79. if (searchQuery.Length == 0)
  80. return GetFirstChildItem(searchResults.FirstOrDefault());
  81. // Sort results by length so that shorter length results are prioritized
  82. // prevents entries with short names getting stuck at end of list after entries with longer names when both contain the same word
  83. searchResults = searchResults.OrderBy(x => x.Name.Length).ToList();
  84. var bestMatch = GetFirstChildItem(searchResults.FirstOrDefault());
  85. int bestScore = 0;
  86. List<int> visitedItems = new List<int>();
  87. var queryTerms = searchQuery.Split(' ');
  88. foreach (var result in searchResults)
  89. {
  90. var currentItem = GetFirstChildItem(result);
  91. if (currentItem.Parent != null)
  92. {
  93. SearcherItem parentItem = currentItem.Parent;
  94. foreach (var matchItem in parentItem.Children)
  95. {
  96. if (visitedItems.Contains(matchItem.Id))
  97. continue;
  98. int currentScore = ComputeScoreForMatch(queryTerms, matchItem);
  99. if (currentScore > bestScore)
  100. {
  101. bestScore = currentScore;
  102. bestMatch = matchItem;
  103. }
  104. visitedItems.Add(matchItem.Id);
  105. }
  106. }
  107. }
  108. return bestMatch;
  109. }
  110. }
  111. internal class SearchNodeItem : SearcherItem
  112. {
  113. public NodeEntry NodeGUID;
  114. public SearchNodeItem(string name, NodeEntry nodeGUID, string[] synonyms,
  115. string help = " ", List<SearchNodeItem> children = null) : base(name)
  116. {
  117. NodeGUID = nodeGUID;
  118. Synonyms = synonyms;
  119. }
  120. }
  121. }