From 98a162be2a510a7771e7998f63370252c799741f Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Mon, 1 Aug 2016 16:58:33 -0700 Subject: [PATCH] Replacement spans for import completions --- src/harness/fourslash.ts | 24 ++++++++++--- src/services/services.ts | 34 +++++++++++++------ .../completionForStringLiteralImport1.ts | 19 +++++++++++ .../completionForStringLiteralImport2.ts | 28 +++++++++++++++ tests/cases/fourslash/fourslash.ts | 2 +- 5 files changed, 92 insertions(+), 15 deletions(-) create mode 100644 tests/cases/fourslash/completionForStringLiteralImport1.ts create mode 100644 tests/cases/fourslash/completionForStringLiteralImport2.ts diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 8b0968664e0..9ad5f25e005 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -766,13 +766,29 @@ namespace FourSlash { } } - public verifyImportModuleCompletionListContains(symbol: string) { + public verifyImportModuleCompletionListContains(symbol: string, rangeIndex?: number) { const completions = this.getImportModuleCompletionListAtCaret(); if (completions) { - if (!ts.forEach(completions, completion => completion.name === symbol)) { + const completion = ts.forEach(completions, completion => completion.name === symbol ? completion : undefined); + if (!completion) { const itemsString = completions.map(item => stringify({ name: item.name, span: item.span })).join(",\n"); this.raiseError(`Expected "${symbol}" to be in list [${itemsString}]`); } + else if (rangeIndex !== undefined) { + const ranges = this.getRanges(); + if (ranges && ranges.length > rangeIndex) { + const range = ranges[rangeIndex]; + + const start = completion.span.start; + const end = start + completion.span.length; + if (range.start !== start || range.end !== end) { + this.raiseError(`Expected completion span for '${symbol}', ${stringify(completion.span)}, to cover range ${stringify(range)}`); + } + } + else { + this.raiseError(`Expected completion span for '${symbol}' to cover range at index ${rangeIndex}, but no range was found at that index`); + } + } } else { this.raiseError(`No import module completions at position '${this.currentCaretPosition}' when looking for '${symbol}'.`); @@ -2943,12 +2959,12 @@ namespace FourSlashInterface { this.state.verifyCompletionListItemsCountIsGreaterThan(count, this.negative); } - public importModuleCompletionListContains(symbol: string): void { + public importModuleCompletionListContains(symbol: string, rangeIndex?: number): void { if (this.negative) { this.state.verifyImportModuleCompletionListDoesNotContain(symbol); } else { - this.state.verifyImportModuleCompletionListContains(symbol); + this.state.verifyImportModuleCompletionListContains(symbol, rangeIndex); } } diff --git a/src/services/services.ts b/src/services/services.ts index 11eec28a5f6..61d624c29df 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2069,7 +2069,7 @@ namespace ts { * for completions. * For example, this matches /// position >= commentRange.pos && position <= commentRange.end && commentRange); + + if (!range) { + return undefined; + } + + const text = sourceFile.text.substr(range.pos, position - range.pos); - const text = sourceFile.text.substr(node.pos, position); const match = tripleSlashDirectiveFragmentRegex.exec(text); if (match) { - const kind= match[1]; - const fragment = match[2]; + const prefix = match[1]; + const kind = match[2]; + const toComplete = match[3]; + + const span: TextSpan = { start: range.pos + prefix.length, length: match[0].length - prefix.length }; + const scriptPath = getDirectoryPath(sourceFile.path); if (kind === "path") { // Give completions for a relative path - return getCompletionEntriesForDirectoryFragment(fragment, scriptPath, getSupportedExtensions(program.getCompilerOptions()), /*includeExtensions*/true, span); + return getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, getSupportedExtensions(program.getCompilerOptions()), /*includeExtensions*/true, span); } else { // Give completions based on the typings available diff --git a/tests/cases/fourslash/completionForStringLiteralImport1.ts b/tests/cases/fourslash/completionForStringLiteralImport1.ts new file mode 100644 index 00000000000..42c2dbaf3d4 --- /dev/null +++ b/tests/cases/fourslash/completionForStringLiteralImport1.ts @@ -0,0 +1,19 @@ +/// + +// @Filename: test.ts +//// import * as foo0 from "[|./some|]/*0*/ +//// import foo1 = require( "[|./some|]/*1*/ +//// var foo2 = require( "[|./some|]/*2*/ + +//// import * as foo3 from "[|./some|]/*3*/"; +//// import foo4 = require( "[|./some|]/*4*/"; +//// var foo5 = require( "[|./some|]/*5*/"; + + +// @Filename: someFile.ts +//// /*someFile*/ + +for (let i = 0; i < 6; i++) { + goTo.marker("" + i); + verify.importModuleCompletionListContains("someFile", i); +} \ No newline at end of file diff --git a/tests/cases/fourslash/completionForStringLiteralImport2.ts b/tests/cases/fourslash/completionForStringLiteralImport2.ts new file mode 100644 index 00000000000..a0618c344a7 --- /dev/null +++ b/tests/cases/fourslash/completionForStringLiteralImport2.ts @@ -0,0 +1,28 @@ +/// + +// @typeRoots: my_typings + +// @Filename: test.ts +//// /// +//// /// + +// @Filename: someFile.ts +//// /*someFile*/ + +// @Filename: my_typings/some-module/index.d.ts +//// export var x = 9; + +goTo.marker("0"); +verify.importModuleCompletionListContains("someFile.ts", 0); + +goTo.marker("1"); +verify.importModuleCompletionListContains("some-module", 1); + +goTo.marker("2"); +verify.importModuleCompletionListContains("someFile.ts", 2); + +goTo.marker("3"); +verify.importModuleCompletionListContains("some-module", 3); \ No newline at end of file diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index e011dde4b68..948477b78dd 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -125,7 +125,7 @@ declare namespace FourSlashInterface { completionListItemsCountIsGreaterThan(count: number): void; completionListIsEmpty(): void; completionListAllowsNewIdentifier(): void; - importModuleCompletionListContains(symbol: string): void; + importModuleCompletionListContains(symbol: string, rangeIndex?: number): void; importModuleCompletionListItemsCountIsGreaterThan(count: number): void; importModuleCompletionListIsEmpty(): void; memberListIsEmpty(): void;