Skip to content

Commit 4c607e7

Browse files
author
Andy Hanson
committed
Replace startsWith with stringContainsCharactersInOrder
1 parent a5b846f commit 4c607e7

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/services/completions.ts

+25-1
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 characterIndex = 0;
964+
for (let strIndex = 0; strIndex < str.length; strIndex++) {
965+
if (str.charCodeAt(strIndex) === characters.charCodeAt(characterIndex)) {
966+
characterIndex++;
967+
if (characterIndex === characters.length) {
968+
return true;
969+
}
970+
}
971+
}
972+
973+
// Did not find all characters
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.
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)