diff --git a/src/EditorFeatures/Core/Implementation/IntelliSense/AsyncCompletion/FilterResult.cs b/src/EditorFeatures/Core/Implementation/IntelliSense/AsyncCompletion/FilterResult.cs index 21d7b84497b5c98494e1e3148346ca2c8aa75b00..02ac19eec86171a88105d81eae992cba691efc90 100644 --- a/src/EditorFeatures/Core/Implementation/IntelliSense/AsyncCompletion/FilterResult.cs +++ b/src/EditorFeatures/Core/Implementation/IntelliSense/AsyncCompletion/FilterResult.cs @@ -1,12 +1,13 @@  // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Roslyn.Utilities; +using System; using Microsoft.CodeAnalysis.Completion; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense.AsyncCompletion { - internal struct FilterResult + internal struct FilterResult : IComparable { public readonly CompletionItem CompletionItem; public readonly bool MatchedFilterText; @@ -19,50 +20,43 @@ public FilterResult(CompletionItem completionItem, string filterText, bool match FilterText = filterText; } - public static int Compare(FilterResult result1, FilterResult result2) + public int CompareTo(FilterResult other) { - var item1 = result1.CompletionItem; - var item2 = result2.CompletionItem; + var item1 = this.CompletionItem; + var item2 = other.CompletionItem; - var prefixLength1 = item1.FilterText.GetCaseInsensitivePrefixLength(result1.FilterText); - var prefixLength2 = item2.FilterText.GetCaseInsensitivePrefixLength(result2.FilterText); + var prefixLength1 = item1.FilterText.GetCaseInsensitivePrefixLength(this.FilterText); + var prefixLength2 = item2.FilterText.GetCaseInsensitivePrefixLength(other.FilterText); // Prefer the item that matches a longer prefix of the filter text. if (prefixLength1 != prefixLength2) { return prefixLength1.CompareTo(prefixLength2); } - else - { - // If the lengths are the same, prefer the one with the higher match priority. - // But only if it's an item that would have been hard selected. We don't want - // to aggressively select an item that was only going to be softly offered. - var item1Priority = item1.Rules.SelectionBehavior == CompletionItemSelectionBehavior.HardSelection - ? item1.Rules.MatchPriority : MatchPriority.Default; - var item2Priority = item2.Rules.SelectionBehavior == CompletionItemSelectionBehavior.HardSelection - ? item2.Rules.MatchPriority : MatchPriority.Default; - if (item1Priority != item2Priority) - { - return item1Priority > item2Priority ? 1 : -1; - } + // If the lengths are the same, prefer the one with the higher match priority. + // But only if it's an item that would have been hard selected. We don't want + // to aggressively select an item that was only going to be softly offered. + var item1Priority = item1.Rules.SelectionBehavior == CompletionItemSelectionBehavior.HardSelection + ? item1.Rules.MatchPriority : MatchPriority.Default; + var item2Priority = item2.Rules.SelectionBehavior == CompletionItemSelectionBehavior.HardSelection + ? item2.Rules.MatchPriority : MatchPriority.Default; - prefixLength1 = item1.FilterText.GetCaseSensitivePrefixLength(result1.FilterText); - prefixLength2 = item2.FilterText.GetCaseSensitivePrefixLength(result2.FilterText); - - // If there are "Abc" vs "abc", we should prefer the case typed by user. - if (prefixLength1 != prefixLength2) - { - return prefixLength1 > prefixLength2 ? 1 : -1; - } + if (item1Priority != item2Priority) + { + return item1Priority.CompareTo(item2Priority); + } - if (result1.CompletionItem.IsPreferredItem() != result2.CompletionItem.IsPreferredItem()) - { - return result1.CompletionItem.IsPreferredItem() ? 1 : -1; - } + prefixLength1 = item1.FilterText.GetCaseSensitivePrefixLength(this.FilterText); + prefixLength2 = item2.FilterText.GetCaseSensitivePrefixLength(other.FilterText); - return 0; + // If there are "Abc" vs "abc", we should prefer the case typed by user. + if (prefixLength1 != prefixLength2) + { + return prefixLength1.CompareTo(prefixLength2); } + + return this.CompletionItem.IsPreferredItem().CompareTo(other.CompletionItem.IsPreferredItem()); } } } diff --git a/src/EditorFeatures/Core/Implementation/IntelliSense/AsyncCompletion/ItemManager.cs b/src/EditorFeatures/Core/Implementation/IntelliSense/AsyncCompletion/ItemManager.cs index 5927767b0e4ce7a453402a85455d1c73b5a0ea7a..2f9ae692b924017a7d52225a103d4f5d947853ec 100644 --- a/src/EditorFeatures/Core/Implementation/IntelliSense/AsyncCompletion/ItemManager.cs +++ b/src/EditorFeatures/Core/Implementation/IntelliSense/AsyncCompletion/ItemManager.cs @@ -365,7 +365,7 @@ private static bool IsAfterDot(ITextSnapshot snapshot, ITrackingSpan applicableT } else { - var match = FilterResult.Compare(currentFilterResult.FilterResult, bestFilterResult.Value.FilterResult); + var match = currentFilterResult.FilterResult.CompareTo(bestFilterResult.Value.FilterResult); if (match > 0) { moreThanOneMatchWithSamePriority = false; @@ -540,7 +540,7 @@ internal static int GetRecentItemIndex(ImmutableArray recentItems, Rosly } internal static bool IsBetterDeletionMatch(FilterResult result1, FilterResult result2) - => FilterResult.Compare(result1, result2) > 0; + => result1.CompareTo(result2) > 0; internal static bool MatchesFilterText( CompletionHelper helper, RoslynCompletionItem item, @@ -582,7 +582,6 @@ internal static bool IsBetterDeletionMatch(FilterResult result1, FilterResult re return helper.MatchesPattern(item.FilterText, filterText, CultureInfo.CurrentCulture); } - internal static bool IsHardSelection( string fullFilterText, CompletionTriggerKind initialTriggerKind,