From f84bbcdf59a4ff1c9e4311ecf3eaad7b83bd2b8a Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Thu, 28 Jan 2016 15:18:50 -0800 Subject: [PATCH] Don't show the currently-completing thing at the cursor in JS files Fixes #6693 (cherry picked from commit 124bd517e71353d8c22c0ee63f2e830c0b18004c) --- src/services/services.ts | 23 +++++++++++-------- .../fourslash/getJavaScriptCompletions20.ts | 21 +++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 tests/cases/fourslash/getJavaScriptCompletions20.ts diff --git a/src/services/services.ts b/src/services/services.ts index 0016a27acc3..58d5357eddb 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -62,7 +62,7 @@ namespace ts { export interface SourceFile { /* @internal */ version: string; /* @internal */ scriptSnapshot: IScriptSnapshot; - /* @internal */ nameTable: Map; + /* @internal */ nameTable: Map; /* @internal */ getNamedDeclarations(): Map; @@ -808,7 +808,7 @@ namespace ts { public languageVersion: ScriptTarget; public languageVariant: LanguageVariant; public identifiers: Map; - public nameTable: Map; + public nameTable: Map; public resolvedModules: Map; public imports: LiteralExpression[]; public moduleAugmentations: LiteralExpression[]; @@ -1957,8 +1957,6 @@ namespace ts { const text = scriptSnapshot.getText(0, scriptSnapshot.getLength()); const sourceFile = createSourceFile(fileName, text, scriptTarget, setNodeParents); setSourceFileFields(sourceFile, scriptSnapshot, version); - // after full parsing we can use table with interned strings as name table - sourceFile.nameTable = sourceFile.identifiers; return sourceFile; } @@ -3834,7 +3832,7 @@ namespace ts { if (isRightOfDot && isSourceFileJavaScript(sourceFile)) { const uniqueNames = getCompletionEntriesFromSymbols(symbols, entries); - addRange(entries, getJavaScriptCompletionEntries(sourceFile, uniqueNames)); + addRange(entries, getJavaScriptCompletionEntries(sourceFile, location.pos, uniqueNames)); } else { if (!symbols || symbols.length === 0) { @@ -3867,12 +3865,17 @@ namespace ts { return { isMemberCompletion, isNewIdentifierLocation, entries }; - function getJavaScriptCompletionEntries(sourceFile: SourceFile, uniqueNames: Map): CompletionEntry[] { + function getJavaScriptCompletionEntries(sourceFile: SourceFile, position: number, uniqueNames: Map): CompletionEntry[] { const entries: CompletionEntry[] = []; const target = program.getCompilerOptions().target; const nameTable = getNameTable(sourceFile); for (const name in nameTable) { + // Skip identifiers produced only from the current location + if (nameTable[name] === position) { + continue; + } + if (!uniqueNames[name]) { uniqueNames[name] = name; const displayName = getCompletionEntryDisplayName(name, target, /*performCharacterChecks*/ true); @@ -7525,7 +7528,7 @@ namespace ts { } /* @internal */ - export function getNameTable(sourceFile: SourceFile): Map { + export function getNameTable(sourceFile: SourceFile): Map { if (!sourceFile.nameTable) { initializeNameTable(sourceFile); } @@ -7534,7 +7537,7 @@ namespace ts { } function initializeNameTable(sourceFile: SourceFile): void { - const nameTable: Map = {}; + const nameTable: Map = {}; walk(sourceFile); sourceFile.nameTable = nameTable; @@ -7542,7 +7545,7 @@ namespace ts { function walk(node: Node) { switch (node.kind) { case SyntaxKind.Identifier: - nameTable[(node).text] = (node).text; + nameTable[(node).text] = nameTable[(node).text] === undefined ? node.pos : -1; break; case SyntaxKind.StringLiteral: case SyntaxKind.NumericLiteral: @@ -7554,7 +7557,7 @@ namespace ts { node.parent.kind === SyntaxKind.ExternalModuleReference || isArgumentOfElementAccessExpression(node)) { - nameTable[(node).text] = (node).text; + nameTable[(node).text] = nameTable[(node).text] === undefined ? node.pos : -1; } break; default: diff --git a/tests/cases/fourslash/getJavaScriptCompletions20.ts b/tests/cases/fourslash/getJavaScriptCompletions20.ts new file mode 100644 index 00000000000..d254705bb91 --- /dev/null +++ b/tests/cases/fourslash/getJavaScriptCompletions20.ts @@ -0,0 +1,21 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: file.js +//// /** +//// * A person +//// * @constructor +//// * @param {string} name - The name of the person. +//// * @param {number} age - The age of the person. +//// */ +//// function Person(name, age) { +//// this.name = name; +//// this.age = age; +//// } +//// +//// +//// Person.getName = 10; +//// Person.getNa/**/ = 10; + +goTo.marker(); +verify.not.memberListContains('getNa');