Skip to content

Commit 86224ff

Browse files
author
Andy Hanson
committed
Replace startsWith with stringContainsCharactersInOrder
1 parent a5b846f commit 86224ff

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/services/completions.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,14 +942,38 @@ namespace ts.Completions {
942942
}
943943

944944
const id = getUniqueSymbolId(symbol, typeChecker);
945-
if (!symbolIdMap[id] && startsWith(name.toLowerCase(), tokenTextLowerCase)) {
945+
if (!symbolIdMap[id] && stringContainsCharactersInOrder(name.toLowerCase(), tokenTextLowerCase)) {
946946
symbols.push(symbol);
947947
symbolToOriginInfoMap[id] = { moduleSymbol, isDefaultExport };
948948
}
949949
}
950950
});
951951
}
952952

953+
/**
954+
* True if you could remove some characters in `a` to get `b`.
955+
* E.g., true for "abcdef" and "bdf".
956+
* But not true for "abcdef" and "dbf".
957+
*/
958+
function stringContainsCharactersInOrder(str: string, characters: string): boolean {
959+
if (characters.length === 0) {
960+
return true;
961+
}
962+
963+
let bi = 0;
964+
for (let ai = 0; ai < str.length; ai++) {
965+
if (str.charCodeAt(ai) === characters.charCodeAt(bi)) {
966+
bi++;
967+
if (bi === characters.length) {
968+
return true;
969+
}
970+
}
971+
}
972+
973+
// Did not find all characters in 'b'
974+
return false;
975+
}
976+
953977
/**
954978
* Finds the first node that "embraces" the position, so that one may
955979
* accurately aggregate locals from the closest containing scope.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a.ts
4+
// Not included:
5+
////export function abcde() {}
6+
////export function dbf() {}
7+
// Included:
8+
////export function bdf() {}
9+
////export function abcdef() {}
10+
////export function BDF() {}
11+
12+
// @Filename: /b.ts
13+
////bdf/**/
14+
15+
goTo.marker("");
16+
17+
verify.not.completionListContains("abcde");
18+
verify.not.completionListContains("dbf");
19+
20+
verify.completionListContains("bdf", "function bdf(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true);
21+
verify.completionListContains("abcdef", "function abcdef(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true);
22+
verify.completionListContains("BDF", "function BDF(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true);

0 commit comments

Comments
 (0)