/// <summary> /// Initializes a new instance with the specified properties. /// </summary> /// <param name="moniker">The unique, non-localized identifier for the /// completion set.</param> /// <param name="displayName">The localized name of the completion set. /// </param> /// <param name="applicableTo">The tracking span to which the /// completions apply.</param> /// <param name="completions">The list of completions.</param> /// <param name="options">The options to use for filtering and /// selecting items.</param> /// <param name="comparer">The comparer to use to order the provided /// completions.</param> public FuzzyCompletionSet(string moniker, string displayName, ITrackingSpan applicableTo, IEnumerable <DynamicallyVisibleCompletion> completions, CompletionOptions options, IComparer <Completion> comparer, Func <string, IEnumerable <DynamicallyVisibleCompletion> > deferredLoadCallback) : base(moniker, displayName, applicableTo, null, null) { _options = options; _initialComparer = comparer; _completions = new BulkObservableCollection <Completion>(); _loadedDeferredCompletionSubsets = new HashSet <string>(StringComparer.OrdinalIgnoreCase); _deferredLoadCallback = deferredLoadCallback; _completions.AddRange(completions .Where(c => c != null && !string.IsNullOrWhiteSpace(c.DisplayText)) .OrderBy(c => c, comparer) ); _comparer = new FuzzyStringMatcher(options.SearchMode); _shouldFilter = options.FilterCompletions; _shouldHideAdvanced = options.HideAdvancedMembers && !_completions.All(IsAdvanced); if (_shouldFilter | _shouldHideAdvanced) { _filteredCompletions = new WritableFilteredObservableCollection <Completion>(_completions); foreach (var c in _completions.Cast <DynamicallyVisibleCompletion>()) { c.Visible = !_shouldHideAdvanced || !IsAdvanced(c); } _filteredCompletions.Filter(IsVisible); } }
/// <summary> /// Determines the best match in the completion set. /// </summary> public override void SelectBestMatch() { var text = ApplicableTo.GetText(ApplicableTo.TextBuffer.CurrentSnapshot); Completion bestMatch = _previousSelection; int bestValue = 0; bool isUnique = true; bool allowSelect = true; if (!string.IsNullOrWhiteSpace(text)) { // Using the Completions property to only search through visible // completions. foreach (var comp in Completions) { int value = _comparer.GetSortKey(comp.DisplayText, text); if (bestMatch == null || value > bestValue) { bestMatch = comp; bestValue = value; isUnique = true; } else if (value == bestValue) { isUnique = false; } } } else { foreach (var comp in Completions) { DateTime temp; if (IntellisenseExtensions.LastCommittedCompletions.TryGetValue(comp.DisplayText, out temp)) { var score = FuzzyStringMatcher.CalculateElapsedTimeBonus(temp); if (bestMatch == null || score > bestValue) { bestMatch = comp; bestValue = score; isUnique = true; } } } } if (bestMatch == null) { bestMatch = Completions[0]; } if (((DynamicallyVisibleCompletion)bestMatch).Visible && bestValue > 0) { SelectionStatus = new CompletionSelectionStatus(bestMatch, isSelected: allowSelect && bestValue > 0, isUnique: isUnique); } else { SelectionStatus = new CompletionSelectionStatus(null, isSelected: false, isUnique: false); } _previousSelection = bestMatch; }