Skip to content

Commit 9aa99b9

Browse files
author
Andy
authored
Include directories in completions for path mapping (#21103)
1 parent fed34cd commit 9aa99b9

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/services/pathCompletions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ namespace ts.Completions.PathCompletions {
9494
*
9595
* both foo.ts and foo.tsx become foo
9696
*/
97-
const foundFiles = createMap<boolean>();
97+
const foundFiles = createMap<true>();
9898
for (let filePath of files) {
9999
filePath = normalizePath(filePath);
100100
if (exclude && comparePaths(filePath, exclude, scriptPath, ignoreCase) === Comparison.EqualTo) {
@@ -103,7 +103,7 @@ namespace ts.Completions.PathCompletions {
103103

104104
const foundFileName = includeExtensions ? getBaseFileName(filePath) : removeFileExtension(getBaseFileName(filePath));
105105

106-
if (!foundFiles.get(foundFileName)) {
106+
if (!foundFiles.has(foundFileName)) {
107107
foundFiles.set(foundFileName, true);
108108
}
109109
}
@@ -226,8 +226,9 @@ namespace ts.Completions.PathCompletions {
226226
const includeGlob = normalizedSuffix ? "**/*" : "./*";
227227

228228
const matches = tryReadDirectory(host, baseDirectory, fileExtensions, /*exclude*/ undefined, [includeGlob]);
229+
const directories = tryGetDirectories(host, baseDirectory);
229230
// Trim away prefix and suffix
230-
return mapDefined(matches, match => {
231+
return mapDefined(concatenate(matches, directories), match => {
231232
const normalizedMatch = normalizePath(match);
232233
if (!endsWith(normalizedMatch, normalizedSuffix) || !startsWith(normalizedMatch, completePrefix)) {
233234
return;
@@ -468,7 +469,7 @@ namespace ts.Completions.PathCompletions {
468469
return tryIOAndConsumeErrors(host, host.getDirectories, directoryName);
469470
}
470471

471-
function tryReadDirectory(host: LanguageServiceHost, path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>): string[] {
472+
function tryReadDirectory(host: LanguageServiceHost, path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>): string[] | undefined {
472473
return tryIOAndConsumeErrors(host, host.readDirectory, path, extensions, exclude, include);
473474
}
474475

tests/cases/fourslash/completionsPaths_pathMapping.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// @Filename: /src/b.ts
44
////export const x = 0;
55

6+
// @Filename: /src/dir/x.ts
7+
/////export const x = 0;
8+
69
// @Filename: /src/a.ts
710
////import {} from "foo/[|/**/|]";
811

@@ -17,4 +20,8 @@
1720
////}
1821

1922
const [replacementSpan] = test.ranges();
20-
verify.completionsAt("", [{ name: "a", replacementSpan }, { name: "b", replacementSpan }]);
23+
verify.completionsAt("", [
24+
{ name: "a", replacementSpan },
25+
{ name: "b", replacementSpan },
26+
{ name: "dir", replacementSpan },
27+
]);

0 commit comments

Comments
 (0)