From 124bd517e71353d8c22c0ee63f2e830c0b18004c Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Thu, 28 Jan 2016 15:18:50 -0800 Subject: [PATCH 1/2] Don't show the currently-completing thing at the cursor in JS files Fixes #6693 --- 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 cb57d415c66a4..ecc0e442dd375 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[]; @@ -1954,8 +1954,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); @@ -7513,7 +7516,7 @@ namespace ts { } /* @internal */ - export function getNameTable(sourceFile: SourceFile): Map { + export function getNameTable(sourceFile: SourceFile): Map { if (!sourceFile.nameTable) { initializeNameTable(sourceFile); } @@ -7522,7 +7525,7 @@ namespace ts { } function initializeNameTable(sourceFile: SourceFile): void { - const nameTable: Map = {}; + const nameTable: Map = {}; walk(sourceFile); sourceFile.nameTable = nameTable; @@ -7530,7 +7533,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: @@ -7542,7 +7545,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 0000000000000..d254705bb9132 --- /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'); From 1231c9e145ef65cf51d5f582abfb71929892c48a Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Thu, 28 Jan 2016 16:05:03 -0800 Subject: [PATCH 2/2] Fix case when position === 0 --- src/services/services.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/services.ts b/src/services/services.ts index ecc0e442dd375..b44a774f72194 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -5490,7 +5490,7 @@ namespace ts { const nameTable = getNameTable(sourceFile); - if (lookUp(nameTable, internedName)) { + if (lookUp(nameTable, internedName) !== undefined) { result = result || []; getReferencesInNode(sourceFile, symbol, declaredName, node, searchMeaning, findInStrings, findInComments, result, symbolToIndex); }