提交 0851be2a 编写于 作者: C CyrusNajmabadi

Simplify completion helper code.

上级 4a79e90e
......@@ -247,15 +247,14 @@ protected int GetPrefixLength(string text, string pattern)
protected int CompareMatches(PatternMatch match1, PatternMatch match2, CompletionItem item1, CompletionItem item2)
{
int diff;
diff = PatternMatch.CompareType(match1, match2);
if (diff != 0)
{
return diff;
}
diff = PatternMatch.CompareCamelCase(match1, match2);
// First see how the two items compare in a case insensitive fashion. Matches that
// are strictly better (ignoring case) should prioritize the item. i.e. if we have
// a prefix match, that should always be better than a substring match.
//
// The reason we ignore case is that it's very common for people to type expecting
// completion to fix up their casing. i.e. 'false' will be written with the
// expectation that it will get fixed by the completion list to 'False'.
var diff = match1.CompareTo(match2, ignoreCase: true);
if (diff != 0)
{
return diff;
......@@ -271,23 +270,19 @@ protected int CompareMatches(PatternMatch match1, PatternMatch match2, Completio
return -1;
}
// preselected items are prefered
if (item1.Rules.Preselect && !item2.Rules.Preselect)
{
return -1;
}
else if (item2.Rules.Preselect && !item1.Rules.Preselect)
{
return 1;
}
diff = PatternMatch.CompareCase(match1, match2);
if (diff != 0)
// Now, after comparing matches, check if an item wants to be preselected. If so,
// we prefer that. i.e. say the user has typed 'f' and we have the items 'foo'
// and 'False' (with the latter being 'Preselected'). Both will be a prefix match.
// And because we are ignoring case, neither will be seen as better. Now, because
// 'False' is preselected we pick it even though 'foo' matches 'f' case sensitively.
if (item1.Rules.Preselect != item2.Rules.Preselect)
{
return diff;
return item1.Rules.Preselect ? -1 : 1;
}
diff = PatternMatch.ComparePunctuation(match1, match2);
// Now compare the matches again in a case sensitive manner. If everything was
// equal up to this point, we prefer the item that better matches based on case.
diff = match1.CompareTo(match2, ignoreCase: false);
if (diff != 0)
{
return diff;
......
......@@ -37,7 +37,7 @@ internal struct PatternMatch : IComparable<PatternMatch>
bool isCaseSensitive,
TextSpan? matchedSpan,
int? camelCaseWeight = null)
: this(resultType, punctuationStripped, isCaseSensitive,
: this(resultType, punctuationStripped, isCaseSensitive,
matchedSpan == null ? null : new[] { matchedSpan.Value },
camelCaseWeight)
{
......@@ -64,11 +64,16 @@ internal struct PatternMatch : IComparable<PatternMatch>
}
public int CompareTo(PatternMatch other)
{
return CompareTo(other, ignoreCase: false);
}
public int CompareTo(PatternMatch other, bool ignoreCase)
{
int diff;
if ((diff = CompareType(this, other)) != 0 ||
(diff = CompareCamelCase(this, other)) != 0 ||
(diff = CompareCase(this, other)) != 0 ||
(diff = CompareCase(this, other, ignoreCase)) != 0 ||
(diff = ComparePunctuation(this, other)) != 0)
{
return diff;
......@@ -77,7 +82,7 @@ public int CompareTo(PatternMatch other)
return 0;
}
internal static int ComparePunctuation(PatternMatch result1, PatternMatch result2)
private static int ComparePunctuation(PatternMatch result1, PatternMatch result2)
{
// Consider a match to be better if it was successful without stripping punctuation
// versus a match that had to strip punctuation to succeed.
......@@ -89,22 +94,25 @@ internal static int ComparePunctuation(PatternMatch result1, PatternMatch result
return 0;
}
internal static int CompareCase(PatternMatch result1, PatternMatch result2)
private static int CompareCase(PatternMatch result1, PatternMatch result2, bool ignoreCase)
{
if (result1.IsCaseSensitive != result2.IsCaseSensitive)
if (!ignoreCase)
{
return result1.IsCaseSensitive ? -1 : 1;
if (result1.IsCaseSensitive != result2.IsCaseSensitive)
{
return result1.IsCaseSensitive ? -1 : 1;
}
}
return 0;
}
internal static int CompareType(PatternMatch result1, PatternMatch result2)
private static int CompareType(PatternMatch result1, PatternMatch result2)
{
return result1.Kind - result2.Kind;
}
internal static int CompareCamelCase(PatternMatch result1, PatternMatch result2)
private static int CompareCamelCase(PatternMatch result1, PatternMatch result2)
{
if (result1.Kind == PatternMatchKind.CamelCase && result2.Kind == PatternMatchKind.CamelCase)
{
......@@ -116,4 +124,4 @@ internal static int CompareCamelCase(PatternMatch result1, PatternMatch result2)
return 0;
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册