Skip to content

Commit a69fc67

Browse files
committed
Restrict IsGlobalCompletion to:
- SourceFile - Template Expression - Statements
1 parent cd04aa9 commit a69fc67

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

src/services/completions.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference path='../compiler/utilities.ts' />
2+
13
/* @internal */
24
namespace ts.Completions {
35
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 {
951953
if (!tryGetGlobalSymbols()) {
952954
return undefined;
953955
}
954-
isGlobalCompletion = true;
955956
}
956957

957958
log("getCompletionData: Semantic work: " + (timestamp() - semanticStart));
@@ -1030,15 +1031,13 @@ namespace ts.Completions {
10301031
if ((jsxContainer.kind === SyntaxKind.JsxSelfClosingElement) || (jsxContainer.kind === SyntaxKind.JsxOpeningElement)) {
10311032
// Cursor is inside a JSX self-closing element or opening element
10321033
attrsType = typeChecker.getJsxElementAttributesType(<JsxOpeningLikeElement>jsxContainer);
1033-
isGlobalCompletion = false;
10341034

10351035
if (attrsType) {
10361036
symbols = filterJsxAttributes(typeChecker.getPropertiesOfType(attrsType), (<JsxOpeningLikeElement>jsxContainer).attributes);
10371037
isMemberCompletion = true;
10381038
isNewIdentifierLocation = false;
10391039
return true;
10401040
}
1041-
10421041
}
10431042
}
10441043

@@ -1079,6 +1078,12 @@ namespace ts.Completions {
10791078
position;
10801079

10811080
const scopeNode = getScopeNode(contextToken, adjustedPosition, sourceFile) || sourceFile;
1081+
if (scopeNode) {
1082+
isGlobalCompletion =
1083+
scopeNode.kind === SyntaxKind.SourceFile ||
1084+
scopeNode.kind === SyntaxKind.TemplateExpression ||
1085+
isStatement(scopeNode);
1086+
}
10821087

10831088
/// TODO filter meaning based on the current context
10841089
const symbolMeanings = SymbolFlags.Type | SymbolFlags.Value | SymbolFlags.Namespace | SymbolFlags.Alias;
Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,65 @@
11
/// <reference path='fourslash.ts'/>
22

3+
// @Filename: file.ts
4+
////export var x = 10;
5+
////export var y = 10;
6+
////export default class C {
7+
////}
8+
9+
// @Filename: a.ts
10+
////import { /*1*/ } from "./file.ts"; // no globals in imports - export found
11+
312
//@Filename: file.tsx
4-
/////// <reference path="/*1*/..\services\services.ts" /> // no globals in reference paths
5-
////import { /*2*/ } from "./file.ts"; // no globals in imports
6-
////var test = "/*3*/"; // no globals in strings
7-
/////*4*/class A { // insert globals
13+
/////// <reference path="/*2*/..\services\services.ts" /> // no globals in reference paths
14+
////import { /*3*/ } from "./file1.ts"; // no globals in imports - export not found
15+
////var test = "/*4*/"; // no globals in strings
16+
/////*5*/class A { // insert globals
817
//// foo(): string { return ''; }
918
////}
1019
////
11-
////class /*5*/B extends A { // no globals after class keyword
20+
////class /*6*/B extends A { // no globals after class keyword
1221
//// bar(): string {
13-
//// /*6*/ // insert globals
22+
//// /*7*/ // insert globals
1423
//// return '';
1524
//// }
1625
////}
1726
////
18-
////class C</*7*/ U extends A, T extends A> { // no globals at beginning of generics
27+
////class C</*8*/ U extends A, T extends A> { // no globals at beginning of generics
1928
//// x: U;
20-
//// y = this./*8*/x; // no globals inserted for member completions
21-
//// /*9*/ // insert globals
29+
//// y = this./*9*/x; // no globals inserted for member completions
30+
//// /*10*/ // insert globals
2231
////}
23-
/////*10*/ // insert globals
24-
////const y = <div /*11*/ />;
32+
/////*11*/ // insert globals
33+
////const y = <div /*12*/ />; // no globals in jsx attribute found
34+
////const z = <div =/*13*/ />; // no globals in jsx attribute with syntax error
35+
////const x = `/*14*/ ${/*15*/}`; // globals only in template expression
2536
goTo.marker("1");
2637
verify.completionListIsGlobal(false);
2738
goTo.marker("2");
2839
verify.completionListIsGlobal(false);
2940
goTo.marker("3");
3041
verify.completionListIsGlobal(false);
3142
goTo.marker("4");
32-
verify.completionListIsGlobal(true);
33-
goTo.marker("5");
3443
verify.completionListIsGlobal(false);
35-
goTo.marker("6");
44+
goTo.marker("5");
3645
verify.completionListIsGlobal(true);
37-
goTo.marker("7");
46+
goTo.marker("6");
3847
verify.completionListIsGlobal(false);
48+
goTo.marker("7");
49+
verify.completionListIsGlobal(true);
3950
goTo.marker("8");
4051
verify.completionListIsGlobal(false);
4152
goTo.marker("9");
42-
verify.completionListIsGlobal(true);
53+
verify.completionListIsGlobal(false);
4354
goTo.marker("10");
4455
verify.completionListIsGlobal(true);
4556
goTo.marker("11");
57+
verify.completionListIsGlobal(true);
58+
goTo.marker("12");
59+
verify.completionListIsGlobal(false);
60+
goTo.marker("13");
61+
verify.completionListIsGlobal(false);
62+
goTo.marker("14");
4663
verify.completionListIsGlobal(false);
64+
goTo.marker("15");
65+
verify.completionListIsGlobal(true);

0 commit comments

Comments
 (0)