diff --git a/StabilityMatrix.Avalonia/Models/TagCompletion/CompletionProvider.cs b/StabilityMatrix.Avalonia/Models/TagCompletion/CompletionProvider.cs index e3275a93..8d9413df 100644 --- a/StabilityMatrix.Avalonia/Models/TagCompletion/CompletionProvider.cs +++ b/StabilityMatrix.Avalonia/Models/TagCompletion/CompletionProvider.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; using System.Threading.Tasks; using AutoComplete.Builders; using AutoComplete.Clients.IndexSearchers; @@ -76,9 +78,46 @@ public class CompletionProvider : ICompletionProvider searcher = new InMemoryIndexSearcher(headerFile, indexFile, tailFile); searcher.Init(); } - - /// + public IEnumerable GetCompletions(string searchTerm, int itemsCount, bool suggest) + { + return GetCompletionsImpl_Index(searchTerm, itemsCount, suggest); + } + + private IEnumerable GetCompletionsImpl_Fuzzy(string searchTerm, int itemsCount, bool suggest) + { + var extracted = FuzzySharp.Process + .ExtractTop(searchTerm, entries.Keys); + + var results = extracted + .Where(r => r.Score > 40) + .Select(r => r.Value) + .ToImmutableArray(); + + // No results + if (results.IsEmpty) + { + Logger.Trace("No results for {Term}", searchTerm); + return Array.Empty(); + } + + Logger.Trace("Got {Count} results for {Term}", results.Length, searchTerm); + + // Get entry for each result + var completions = new List(); + foreach (var item in results) + { + if (entries.TryGetValue(item, out var entry)) + { + var entryType = TagTypeExtensions.FromE621(entry.Type.GetValueOrDefault(-1)); + completions.Add(new TagCompletionData(entry.Name!, entryType)); + } + } + + return completions; + } + + private IEnumerable GetCompletionsImpl_Index(string searchTerm, int itemsCount, bool suggest) { if (searcher is null) { @@ -91,7 +130,7 @@ public class CompletionProvider : ICompletionProvider MaxItemCount = itemsCount, SuggestWhenFoundStartsWith = suggest }; - + var result = searcher.Search(searchOptions); // No results @@ -101,6 +140,13 @@ public class CompletionProvider : ICompletionProvider return Array.Empty(); } + // Is null for some reason? + if (result.Items is null) + { + Logger.Warn("Got null results for {Term}", searchTerm); + return Array.Empty(); + } + Logger.Trace("Got {Count} results for {Term}", result.Items.Length, searchTerm); // Get entry for each result