static IEnumerable <SearchItem> FetchTemplates(SearchContext context, SearchProvider provider) { long score = 0; var templates = SceneTemplateUtils.GetSceneTemplateInfos(); List <int> matches = new List <int>(); foreach (var t in templates) { var id = t.sceneTemplate?.GetInstanceID().ToString() ?? t.name; if (string.IsNullOrEmpty(context.searchQuery) || FuzzySearch.FuzzyMatch(context.searchQuery, $"{t.name} {t.description}", ref score, matches)) { yield return(provider.CreateItem(context, id, ~(int)score, t.name, t.description, t.thumbnail, t)); } score++; } }
public static GUIContent FormatDescription(SearchItem item, SearchContext context, float availableSpace, bool useColor = true) { var desc = item.GetDescription(context); if (desc != null && item.options.HasAny(SearchItemOptions.Compacted)) { desc = desc.Replace("\n", " "); } if (String.IsNullOrEmpty(desc)) { return(Styles.emptyContent); } var content = Take(desc); if (item.options == SearchItemOptions.None || Event.current.type != EventType.Repaint) { return(content); } var truncatedDesc = desc; var truncated = false; if (useColor) { if (item.options.HasAny(SearchItemOptions.Ellipsis)) { int maxCharLength = Utils.GetNumCharactersThatFitWithinWidth(Styles.itemDescription, truncatedDesc + "...", availableSpace); if (maxCharLength < 0) { maxCharLength = truncatedDesc.Length; } truncated = desc.Length > maxCharLength; if (truncated) { if (item.options.HasAny(SearchItemOptions.RightToLeft)) { truncatedDesc = "..." + desc.Replace("<b>", "").Replace("</b>", ""); truncatedDesc = truncatedDesc.Substring(Math.Max(0, truncatedDesc.Length - maxCharLength)); } else { truncatedDesc = desc.Substring(0, Math.Min(maxCharLength, desc.Length)) + "..."; } } } if (context != null) { if (item.options.HasAny(SearchItemOptions.Highlight)) { var parts = context.searchQuery.Split('*', ' ', '.').Where(p => p.Length > 2); foreach (var p in parts) { truncatedDesc = Regex.Replace(truncatedDesc, Regex.Escape(p), string.Format(Styles.highlightedTextColorFormat, "$0"), RegexOptions.IgnoreCase); } } else if (item.options.HasAny(SearchItemOptions.FuzzyHighlight)) { long score = 1; var matches = new List <int>(); var sq = Utils.CleanString(context.searchQuery.ToLowerInvariant()); if (FuzzySearch.FuzzyMatch(sq, Utils.CleanString(truncatedDesc), ref score, matches)) { truncatedDesc = RichTextFormatter.FormatSuggestionTitle(truncatedDesc, matches); } } } } content.text = truncatedDesc; if (truncated) { content.tooltip = Utils.StripHTML(desc); } return(content); }