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

Simplify completion helper code.

上级 4a79e90e
...@@ -247,15 +247,14 @@ protected int GetPrefixLength(string text, string pattern) ...@@ -247,15 +247,14 @@ protected int GetPrefixLength(string text, string pattern)
protected int CompareMatches(PatternMatch match1, PatternMatch match2, CompletionItem item1, CompletionItem item2) protected int CompareMatches(PatternMatch match1, PatternMatch match2, CompletionItem item1, CompletionItem item2)
{ {
int diff; // 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
diff = PatternMatch.CompareType(match1, match2); // a prefix match, that should always be better than a substring match.
if (diff != 0) //
{ // The reason we ignore case is that it's very common for people to type expecting
return diff; // 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);
diff = PatternMatch.CompareCamelCase(match1, match2);
if (diff != 0) if (diff != 0)
{ {
return diff; return diff;
...@@ -271,23 +270,19 @@ protected int CompareMatches(PatternMatch match1, PatternMatch match2, Completio ...@@ -271,23 +270,19 @@ protected int CompareMatches(PatternMatch match1, PatternMatch match2, Completio
return -1; return -1;
} }
// preselected items are prefered // Now, after comparing matches, check if an item wants to be preselected. If so,
if (item1.Rules.Preselect && !item2.Rules.Preselect) // 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.
return -1; // 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.
else if (item2.Rules.Preselect && !item1.Rules.Preselect) if (item1.Rules.Preselect != item2.Rules.Preselect)
{
return 1;
}
diff = PatternMatch.CompareCase(match1, match2);
if (diff != 0)
{ {
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) if (diff != 0)
{ {
return diff; return diff;
......
...@@ -37,7 +37,7 @@ internal struct PatternMatch : IComparable<PatternMatch> ...@@ -37,7 +37,7 @@ internal struct PatternMatch : IComparable<PatternMatch>
bool isCaseSensitive, bool isCaseSensitive,
TextSpan? matchedSpan, TextSpan? matchedSpan,
int? camelCaseWeight = null) int? camelCaseWeight = null)
: this(resultType, punctuationStripped, isCaseSensitive, : this(resultType, punctuationStripped, isCaseSensitive,
matchedSpan == null ? null : new[] { matchedSpan.Value }, matchedSpan == null ? null : new[] { matchedSpan.Value },
camelCaseWeight) camelCaseWeight)
{ {
...@@ -64,11 +64,16 @@ internal struct PatternMatch : IComparable<PatternMatch> ...@@ -64,11 +64,16 @@ internal struct PatternMatch : IComparable<PatternMatch>
} }
public int CompareTo(PatternMatch other) public int CompareTo(PatternMatch other)
{
return CompareTo(other, ignoreCase: false);
}
public int CompareTo(PatternMatch other, bool ignoreCase)
{ {
int diff; int diff;
if ((diff = CompareType(this, other)) != 0 || if ((diff = CompareType(this, other)) != 0 ||
(diff = CompareCamelCase(this, other)) != 0 || (diff = CompareCamelCase(this, other)) != 0 ||
(diff = CompareCase(this, other)) != 0 || (diff = CompareCase(this, other, ignoreCase)) != 0 ||
(diff = ComparePunctuation(this, other)) != 0) (diff = ComparePunctuation(this, other)) != 0)
{ {
return diff; return diff;
...@@ -77,7 +82,7 @@ public int CompareTo(PatternMatch other) ...@@ -77,7 +82,7 @@ public int CompareTo(PatternMatch other)
return 0; 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 // Consider a match to be better if it was successful without stripping punctuation
// versus a match that had to strip punctuation to succeed. // versus a match that had to strip punctuation to succeed.
...@@ -89,22 +94,25 @@ internal static int ComparePunctuation(PatternMatch result1, PatternMatch result ...@@ -89,22 +94,25 @@ internal static int ComparePunctuation(PatternMatch result1, PatternMatch result
return 0; 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; return 0;
} }
internal static int CompareType(PatternMatch result1, PatternMatch result2) private static int CompareType(PatternMatch result1, PatternMatch result2)
{ {
return result1.Kind - result2.Kind; 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) if (result1.Kind == PatternMatchKind.CamelCase && result2.Kind == PatternMatchKind.CamelCase)
{ {
...@@ -116,4 +124,4 @@ internal static int CompareCamelCase(PatternMatch result1, PatternMatch result2) ...@@ -116,4 +124,4 @@ internal static int CompareCamelCase(PatternMatch result1, PatternMatch result2)
return 0; 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.
先完成此消息的编辑!
想要评论请 注册