Skip to content

Commit

Permalink
Replace startsWith with stringContainsCharactersInOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hanson committed Oct 16, 2017
1 parent a5b846f commit 86224ff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,14 +942,38 @@ namespace ts.Completions {
}

const id = getUniqueSymbolId(symbol, typeChecker);
if (!symbolIdMap[id] && startsWith(name.toLowerCase(), tokenTextLowerCase)) {
if (!symbolIdMap[id] && stringContainsCharactersInOrder(name.toLowerCase(), tokenTextLowerCase)) {
symbols.push(symbol);
symbolToOriginInfoMap[id] = { moduleSymbol, isDefaultExport };
}
}
});
}

/**
* True if you could remove some characters in `a` to get `b`.
* E.g., true for "abcdef" and "bdf".
* But not true for "abcdef" and "dbf".
*/
function stringContainsCharactersInOrder(str: string, characters: string): boolean {
if (characters.length === 0) {
return true;
}

let bi = 0;
for (let ai = 0; ai < str.length; ai++) {
if (str.charCodeAt(ai) === characters.charCodeAt(bi)) {
bi++;
if (bi === characters.length) {
return true;
}
}
}

// Did not find all characters in 'b'
return false;
}

/**
* Finds the first node that "embraces" the position, so that one may
* accurately aggregate locals from the closest containing scope.
Expand Down
22 changes: 22 additions & 0 deletions tests/cases/fourslash/completionsImport_matching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path="fourslash.ts" />

// @Filename: /a.ts
// Not included:
////export function abcde() {}
////export function dbf() {}
// Included:
////export function bdf() {}
////export function abcdef() {}
////export function BDF() {}

// @Filename: /b.ts
////bdf/**/

goTo.marker("");

verify.not.completionListContains("abcde");
verify.not.completionListContains("dbf");

verify.completionListContains("bdf", "function bdf(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true);
verify.completionListContains("abcdef", "function abcdef(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true);
verify.completionListContains("BDF", "function BDF(): void", "", "function", /*spanIndex*/ undefined, /*hasAction*/ true);

0 comments on commit 86224ff

Please sign in to comment.