|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using System.ComponentModel.Composition; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Globalization; |
| 8 | +using System.Linq; |
| 9 | +using System.Reactive.Linq; |
| 10 | +using GitHub.Extensions; |
| 11 | +using GitHub.Logging; |
| 12 | +using GitHub.Models; |
| 13 | +using Serilog; |
| 14 | + |
| 15 | +namespace GitHub.Services |
| 16 | +{ |
| 17 | + [Export(typeof(IAutoCompleteAdvisor))] |
| 18 | + [PartCreationPolicy(CreationPolicy.Shared)] |
| 19 | + public class AutoCompleteAdvisor : IAutoCompleteAdvisor |
| 20 | + { |
| 21 | + const int SuggestionCount = 5; // The number of suggestions we'll provide. github.com does 5. |
| 22 | + |
| 23 | + static readonly ILogger log = LogManager.ForContext<AutoCompleteAdvisor>(); |
| 24 | + readonly Lazy<Dictionary<string, IAutoCompleteSource>> prefixSourceMap; |
| 25 | + |
| 26 | + [ImportingConstructor] |
| 27 | + public AutoCompleteAdvisor([ImportMany(typeof(IAutoCompleteSource))]IEnumerable<IAutoCompleteSource> autocompleteSources) |
| 28 | + { |
| 29 | + prefixSourceMap = new Lazy<Dictionary<string, IAutoCompleteSource>>( |
| 30 | + () => autocompleteSources.ToDictionary(s => s.Prefix, s => s)); |
| 31 | + } |
| 32 | + |
| 33 | + public IObservable<AutoCompleteResult> GetAutoCompletionSuggestions(string text, int caretPosition) |
| 34 | + { |
| 35 | + Guard.ArgumentNotNull("text", text); |
| 36 | + |
| 37 | + if (caretPosition < 0 || caretPosition > text.Length) |
| 38 | + { |
| 39 | + string error = String.Format(CultureInfo.InvariantCulture, |
| 40 | + "The CaretPosition '{0}', is not in the range of '0' and the text length '{1}' for the text '{2}'", |
| 41 | + caretPosition, |
| 42 | + text.Length, |
| 43 | + text); |
| 44 | + |
| 45 | + // We need to be alerted when this happens because it should never happen. |
| 46 | + // But it apparently did happen in production. |
| 47 | + Debug.Fail(error); |
| 48 | + log.Error(error); |
| 49 | + return Observable.Empty<AutoCompleteResult>(); |
| 50 | + } |
| 51 | + var tokenAndSource = PrefixSourceMap |
| 52 | + .Select(kvp => new {Source = kvp.Value, Token = ParseAutoCompletionToken(text, caretPosition, kvp.Key)}) |
| 53 | + .FirstOrDefault(s => s.Token != null); |
| 54 | + |
| 55 | + if (tokenAndSource == null) |
| 56 | + { |
| 57 | + return Observable.Return(AutoCompleteResult.Empty); |
| 58 | + } |
| 59 | + |
| 60 | + return tokenAndSource.Source.GetSuggestions() |
| 61 | + .Select(suggestion => new |
| 62 | + { |
| 63 | + suggestion, |
| 64 | + rank = suggestion.GetSortRank(tokenAndSource.Token.SearchSearchPrefix) |
| 65 | + }) |
| 66 | + .Where(suggestion => suggestion.rank > -1) |
| 67 | + .ToList() |
| 68 | + .Select(suggestions => suggestions |
| 69 | + .OrderByDescending(s => s.rank) |
| 70 | + .ThenBy(s => s.suggestion.Name) |
| 71 | + .Take(SuggestionCount) |
| 72 | + .Select(s => s.suggestion) |
| 73 | + .ToList()) |
| 74 | + .Select(suggestions => new AutoCompleteResult(tokenAndSource.Token.Offset, |
| 75 | + new ReadOnlyCollection<AutoCompleteSuggestion>(suggestions))) |
| 76 | + .Catch<AutoCompleteResult, Exception>(e => |
| 77 | + { |
| 78 | + log.Error(e, "Error Getting AutoCompleteResult"); |
| 79 | + return Observable.Return(AutoCompleteResult.Empty); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + [SuppressMessage("Microsoft.Usage", "CA2233:OperationsShouldNotOverflow", MessageId = "caretPosition-1" |
| 84 | + , Justification = "We ensure the argument is greater than -1 so it can't overflow")] |
| 85 | + public static AutoCompletionToken ParseAutoCompletionToken(string text, int caretPosition, string triggerPrefix) |
| 86 | + { |
| 87 | + Guard.ArgumentNotNull("text", text); |
| 88 | + Guard.ArgumentInRange(caretPosition, 0, text.Length, "caretPosition"); |
| 89 | + if (caretPosition == 0 || text.Length == 0) return null; |
| 90 | + |
| 91 | + // :th : 1 |
| 92 | + //:th : 0 |
| 93 | + //Hi :th : 3 |
| 94 | + int beginningOfWord = text.LastIndexOfAny(new[] { ' ', '\n' }, caretPosition - 1) + 1; |
| 95 | + string word = text.Substring(beginningOfWord, caretPosition - beginningOfWord); |
| 96 | + if (!word.StartsWith(triggerPrefix, StringComparison.Ordinal)) return null; |
| 97 | + |
| 98 | + return new AutoCompletionToken(word.Substring(1), beginningOfWord); |
| 99 | + } |
| 100 | + |
| 101 | + Dictionary<string, IAutoCompleteSource> PrefixSourceMap { get { return prefixSourceMap.Value; } } |
| 102 | + } |
| 103 | + |
| 104 | + public class AutoCompletionToken |
| 105 | + { |
| 106 | + public AutoCompletionToken(string searchPrefix, int offset) |
| 107 | + { |
| 108 | + Guard.ArgumentNotNull(searchPrefix, "searchPrefix"); |
| 109 | + Guard.ArgumentNonNegative(offset, "offset"); |
| 110 | + |
| 111 | + SearchSearchPrefix = searchPrefix; |
| 112 | + Offset = offset; |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Used to filter the list of auto complete suggestions to what the user has typed in. |
| 117 | + /// </summary> |
| 118 | + public string SearchSearchPrefix { get; private set; } |
| 119 | + public int Offset { get; private set; } |
| 120 | + } |
| 121 | +} |
0 commit comments