diff --git a/src/services/completions.ts b/src/services/completions.ts index c8d7c019e5410..3b76fdc8c3b82 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -1,3 +1,5 @@ +/// + /* @internal */ namespace ts.Completions { export function getCompletionsAtPosition(host: LanguageServiceHost, typeChecker: TypeChecker, log: (message: string) => void, compilerOptions: CompilerOptions, sourceFile: SourceFile, position: number): CompletionInfo { @@ -951,7 +953,6 @@ namespace ts.Completions { if (!tryGetGlobalSymbols()) { return undefined; } - isGlobalCompletion = true; } log("getCompletionData: Semantic work: " + (timestamp() - semanticStart)); @@ -1030,7 +1031,6 @@ namespace ts.Completions { if ((jsxContainer.kind === SyntaxKind.JsxSelfClosingElement) || (jsxContainer.kind === SyntaxKind.JsxOpeningElement)) { // Cursor is inside a JSX self-closing element or opening element attrsType = typeChecker.getJsxElementAttributesType(jsxContainer); - isGlobalCompletion = false; if (attrsType) { symbols = filterJsxAttributes(typeChecker.getPropertiesOfType(attrsType), (jsxContainer).attributes); @@ -1038,7 +1038,6 @@ namespace ts.Completions { isNewIdentifierLocation = false; return true; } - } } @@ -1079,6 +1078,13 @@ namespace ts.Completions { position; const scopeNode = getScopeNode(contextToken, adjustedPosition, sourceFile) || sourceFile; + if (scopeNode) { + isGlobalCompletion = + scopeNode.kind === SyntaxKind.SourceFile || + scopeNode.kind === SyntaxKind.TemplateExpression || + scopeNode.kind === SyntaxKind.JsxExpression || + isStatement(scopeNode); + } /// TODO filter meaning based on the current context const symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Alias; diff --git a/tests/cases/fourslash/completionListIsGlobalCompletion.ts b/tests/cases/fourslash/completionListIsGlobalCompletion.ts index 73cb6bb2176ba..a1fadc6bb1275 100644 --- a/tests/cases/fourslash/completionListIsGlobalCompletion.ts +++ b/tests/cases/fourslash/completionListIsGlobalCompletion.ts @@ -1,27 +1,39 @@ /// +// @Filename: file.ts +////export var x = 10; +////export var y = 10; +////export default class C { +////} + +// @Filename: a.ts +////import { /*1*/ } from "./file.ts"; // no globals in imports - export found + //@Filename: file.tsx -/////// // no globals in reference paths -////import { /*2*/ } from "./file.ts"; // no globals in imports -////var test = "/*3*/"; // no globals in strings -/////*4*/class A { // insert globals +/////// // no globals in reference paths +////import { /*3*/ } from "./file1.ts"; // no globals in imports - export not found +////var test = "/*4*/"; // no globals in strings +/////*5*/class A { // insert globals //// foo(): string { return ''; } ////} //// -////class /*5*/B extends A { // no globals after class keyword +////class /*6*/B extends A { // no globals after class keyword //// bar(): string { -//// /*6*/ // insert globals +//// /*7*/ // insert globals //// return ''; //// } ////} //// -////class C { // no globals at beginning of generics +////class C { // no globals at beginning of generics //// x: U; -//// y = this./*8*/x; // no globals inserted for member completions -//// /*9*/ // insert globals +//// y = this./*9*/x; // no globals inserted for member completions +//// /*10*/ // insert globals ////} -/////*10*/ // insert globals -////const y =
; +/////*11*/ // insert globals +////const y =
; // no globals in jsx attribute found +////const z =
; // no globals in jsx attribute with syntax error +////const x = `/*14*/ ${/*15*/}`; // globals only in template expression +////var user = ; // globals only in JSX expression (but not in JSX expression strings) goTo.marker("1"); verify.completionListIsGlobal(false); goTo.marker("2"); @@ -29,18 +41,34 @@ verify.completionListIsGlobal(false); goTo.marker("3"); verify.completionListIsGlobal(false); goTo.marker("4"); -verify.completionListIsGlobal(true); -goTo.marker("5"); verify.completionListIsGlobal(false); -goTo.marker("6"); +goTo.marker("5"); verify.completionListIsGlobal(true); -goTo.marker("7"); +goTo.marker("6"); verify.completionListIsGlobal(false); +goTo.marker("7"); +verify.completionListIsGlobal(true); goTo.marker("8"); verify.completionListIsGlobal(false); goTo.marker("9"); -verify.completionListIsGlobal(true); +verify.completionListIsGlobal(false); goTo.marker("10"); verify.completionListIsGlobal(true); goTo.marker("11"); +verify.completionListIsGlobal(true); +goTo.marker("12"); +verify.completionListIsGlobal(false); +goTo.marker("13"); verify.completionListIsGlobal(false); +goTo.marker("14"); +verify.completionListIsGlobal(false); +goTo.marker("15"); +verify.completionListIsGlobal(true); +goTo.marker("16"); +verify.completionListIsGlobal(false); +goTo.marker("17"); +verify.completionListIsGlobal(false); +goTo.marker("18"); +verify.completionListIsGlobal(true); +goTo.marker("19"); +verify.completionListIsGlobal(false); \ No newline at end of file