Add completions for types triple slash directives

This commit is contained in:
Richard Knoll 2016-07-28 13:12:53 -07:00
parent dbf19f18af
commit fdbc23e9ac
5 changed files with 28 additions and 9 deletions

View File

@ -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 => {

View File

@ -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 === "\\";
}
}

View File

@ -4,6 +4,7 @@
// @Filename: tests/test0.ts
//// /// <reference types="m/*types_ref0*/" />
//// 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");

View File

@ -5,6 +5,7 @@
// @Filename: tests/test0.ts
//// /// <reference types="m/*types_ref0*/" />
//// 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");

View File

@ -1,6 +1,7 @@
/// <reference path='fourslash.ts' />
// @Filename: subdirectory/test0.ts
//// /// <reference types="m/*types_ref0*/" />
//// 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");