From fdbc23e9acc1c2bbc04c6b8440a4bfe3577d6f78 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Thu, 28 Jul 2016 13:12:53 -0700 Subject: [PATCH] Add completions for types triple slash directives --- src/services/services.ts | 23 ++++++++++++++----- src/services/utilities.ts | 5 ++++ ...rStringLiteralNonrelativeImportTypings1.ts | 3 ++- ...rStringLiteralNonrelativeImportTypings2.ts | 3 ++- ...rStringLiteralNonrelativeImportTypings3.ts | 3 ++- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index 76aed2ffe01..a4a1a444592 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -4538,11 +4538,10 @@ namespace ts { function getModulesForPathsPattern(fragment: string, baseUrl: string, pattern: string, fileExtensions: string[]): string[] { const parsed = hasZeroOrOneAsteriskCharacter(pattern) ? tryParsePattern(pattern) : undefined; if (parsed) { - const hasTrailingSlash = parsed.prefix.charAt(parsed.prefix.length - 1) === "/" || parsed.prefix.charAt(parsed.prefix.length - 1) === "\\"; - // The prefix has two effective parts: the directory path and the base component after the filepath that is not a // full directory component. For example: directory/path/of/prefix/base* - const normalizedPrefix = hasTrailingSlash ? ensureTrailingDirectorySeparator(normalizePath(parsed.prefix)) : normalizePath(parsed.prefix); + const normalizedPrefix = hasTrailingDirectorySeparator(parsed.prefix) ? + ensureTrailingDirectorySeparator(normalizePath(parsed.prefix)) : normalizePath(parsed.prefix); const normalizedPrefixDirectory = getDirectoryPath(normalizedPrefix); const normalizedPrefixBase = getBaseFileName(normalizedPrefix); @@ -4582,6 +4581,13 @@ namespace ts { } function enumeratePotentialNonRelativeModules(fragment: string, scriptPath: string): string[] { + const trailingSeperator = hasTrailingDirectorySeparator(fragment); + fragment = normalizePath(fragment); + + if (trailingSeperator) { + fragment = ensureTrailingDirectorySeparator(fragment); + } + // If this is a nested module, get the module name const firstSeparator = fragment.indexOf(directorySeparator); const moduleNameFragment = firstSeparator !== -1 ? fragment.substr(0, firstSeparator) : fragment; @@ -4631,9 +4637,9 @@ namespace ts { if (match) { const kind= match[1]; const fragment = match[2]; + const scriptPath = getDirectoryPath(sourceFile.path); if (kind === "path") { // Give completions for a relative path - const scriptPath = getDirectoryPath(sourceFile.path); return { isMemberCompletion: false, isNewIdentifierLocation: false, @@ -4641,7 +4647,12 @@ namespace ts { }; } else { - // Give completions based on what is available in the types directory + // Give completions based on the typings available + return { + isMemberCompletion: false, + isNewIdentifierLocation: false, + entries: getCompletionEntriesFromTypings(host, program.getCompilerOptions(), scriptPath) + }; } } @@ -4649,7 +4660,7 @@ namespace ts { } } - function getCompletionEntriesFromTypings(host: LanguageServiceHost, options: CompilerOptions, scriptPath: string, result: CompletionEntry[]): CompletionEntry[] { + function getCompletionEntriesFromTypings(host: LanguageServiceHost, options: CompilerOptions, scriptPath: string, result: CompletionEntry[] = []): CompletionEntry[] { // Check for typings specified in compiler options if (options.types) { forEach(options.types, moduleName => { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 0c4c0fd1dea..3a9b56584c2 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -927,4 +927,9 @@ namespace ts { } return ensureScriptKind(fileName, scriptKind); } + + export function hasTrailingDirectorySeparator(path: string) { + const lastCharacter = path.charAt(path.length - 1); + return lastCharacter === "/" || lastCharacter === "\\"; + } } \ No newline at end of file diff --git a/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings1.ts b/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings1.ts index 53dc12033c9..edce0ce7e9f 100644 --- a/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings1.ts +++ b/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings1.ts @@ -4,6 +4,7 @@ // @Filename: tests/test0.ts +//// /// //// import * as foo1 from "m/*import_as0*/ //// import foo2 = require("m/*import_equals0*/ //// var foo3 = require("m/*require0*/ @@ -21,7 +22,7 @@ //// export var z = 9; -const kinds = ["import_as", "import_equals", "require"]; +const kinds = ["types_ref", "import_as", "import_equals", "require"]; for (const kind of kinds) { goTo.marker(kind + "0"); diff --git a/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings2.ts b/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings2.ts index 01e6f0ed420..541bb88c073 100644 --- a/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings2.ts +++ b/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings2.ts @@ -5,6 +5,7 @@ // @Filename: tests/test0.ts +//// /// //// import * as foo1 from "m/*import_as0*/ //// import foo2 = require("m/*import_equals0*/ //// var foo3 = require("m/*require0*/ @@ -19,7 +20,7 @@ //// export var z = 9; -const kinds = ["import_as", "import_equals", "require"]; +const kinds = ["types_ref", "import_as", "import_equals", "require"]; for (const kind of kinds) { goTo.marker(kind + "0"); diff --git a/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings3.ts b/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings3.ts index 9710ce05cd5..7a7d4e00df9 100644 --- a/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings3.ts +++ b/tests/cases/fourslash/completionForStringLiteralNonrelativeImportTypings3.ts @@ -1,6 +1,7 @@ /// // @Filename: subdirectory/test0.ts +//// /// //// import * as foo1 from "m/*import_as0*/ //// import foo2 = require("m/*import_equals0*/ //// var foo3 = require("m/*require0*/ @@ -15,7 +16,7 @@ // @Filename: package.json //// { "dependencies": { "@types/module-y": "latest" } } -const kinds = ["import_as", "import_equals", "require"]; +const kinds = ["types_ref", "import_as", "import_equals", "require"]; for (const kind of kinds) { goTo.marker(kind + "0");