Skip to content

Commit cb9c3e0

Browse files
author
Andy
authored
Don't provide a path completion if a directory name was already typed (microsoft#25055)
1 parent 3f21444 commit cb9c3e0

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/services/pathCompletions.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ namespace ts.Completions.PathCompletions {
1717
}
1818

1919
export function getStringLiteralCompletionsFromModuleNames(sourceFile: SourceFile, node: LiteralExpression, compilerOptions: CompilerOptions, host: LanguageServiceHost, typeChecker: TypeChecker): ReadonlyArray<PathCompletion> {
20-
return addReplacementSpans(node.text, node.getStart(sourceFile) + 1, getStringLiteralCompletionsFromModuleNamesWorker(node, compilerOptions, host, typeChecker));
20+
return addReplacementSpans(node.text, node.getStart(sourceFile) + 1, getStringLiteralCompletionsFromModuleNamesWorker(sourceFile, node, compilerOptions, host, typeChecker));
2121
}
2222

23-
function getStringLiteralCompletionsFromModuleNamesWorker(node: LiteralExpression, compilerOptions: CompilerOptions, host: LanguageServiceHost, typeChecker: TypeChecker): ReadonlyArray<NameAndKind> {
23+
function getStringLiteralCompletionsFromModuleNamesWorker(sourceFile: SourceFile, node: LiteralExpression, compilerOptions: CompilerOptions, host: LanguageServiceHost, typeChecker: TypeChecker): ReadonlyArray<NameAndKind> {
2424
const literalValue = normalizeSlashes(node.text);
2525

26-
const scriptPath = node.getSourceFile().path;
26+
const scriptPath = sourceFile.path;
2727
const scriptDirectory = getDirectoryPath(scriptPath);
2828

2929
if (isPathRelativeToScript(literalValue) || isRootedDiskPath(literalValue)) {
@@ -37,7 +37,6 @@ namespace ts.Completions.PathCompletions {
3737
}
3838
}
3939
else {
40-
// Check for node modules
4140
return getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, compilerOptions, host, typeChecker);
4241
}
4342
}
@@ -225,16 +224,17 @@ namespace ts.Completions.PathCompletions {
225224
): ReadonlyArray<NameAndKind> {
226225
if (!endsWith(path, "*")) {
227226
// For a path mapping "foo": ["/x/y/z.ts"], add "foo" itself as a completion.
228-
return !stringContains(path, "*") && startsWith(path, fragment) ? [{ name: path, kind: ScriptElementKind.directory }] : emptyArray;
227+
return !stringContains(path, "*") ? justPathMappingName(path) : emptyArray;
229228
}
230229

231230
const pathPrefix = path.slice(0, path.length - 1);
232-
if (!startsWith(fragment, pathPrefix)) {
233-
return [{ name: pathPrefix, kind: ScriptElementKind.directory }];
234-
}
231+
const remainingFragment = tryRemovePrefix(fragment, pathPrefix);
232+
return remainingFragment === undefined ? justPathMappingName(pathPrefix) : flatMap(patterns, pattern =>
233+
getModulesForPathsPattern(remainingFragment, baseUrl, pattern, fileExtensions, host));
235234

236-
const remainingFragment = fragment.slice(pathPrefix.length);
237-
return flatMap(patterns, pattern => getModulesForPathsPattern(remainingFragment, baseUrl, pattern, fileExtensions, host));
235+
function justPathMappingName(name: string): ReadonlyArray<NameAndKind> {
236+
return startsWith(name, fragment) ? [{ name, kind: ScriptElementKind.directory }] : emptyArray;
237+
}
238238
}
239239

240240
function getModulesForPathsPattern(fragment: string, baseUrl: string, pattern: string, fileExtensions: ReadonlyArray<string>, host: LanguageServiceHost): ReadonlyArray<NameAndKind> | undefined {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /user.ts
4+
////import {} from "something//**/";
5+
6+
// @Filename: /tsconfig.json
7+
////{
8+
//// "compilerOptions": {
9+
//// "baseUrl": ".",
10+
//// "paths": {
11+
//// "mapping/*": ["whatever"],
12+
//// }
13+
//// }
14+
////}
15+
16+
verify.completions({ marker: "", exact: [], isNewIdentifierLocation: true });

0 commit comments

Comments
 (0)