Skip to content

Commit f4dc114

Browse files
authored
Merge pull request microsoft#11338 from Microsoft/AddIsGlobalCompletionForSnippets
Add isGlobalCompletion to CompletionInfo for snippet injection
2 parents 8dabe33 + 62fddba commit f4dc114

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

src/harness/fourslash.ts

+11
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,13 @@ namespace FourSlash {
754754
}
755755
}
756756

757+
public verifyCompletionListIsGlobal(expected: boolean) {
758+
const completions = this.getCompletionListAtCaret();
759+
if (completions && completions.isGlobalCompletion !== expected) {
760+
this.raiseError(`verifyCompletionListIsGlobal failed - expected result to be ${completions.isGlobalCompletion}`);
761+
}
762+
}
763+
757764
public verifyCompletionListContains(symbol: string, text?: string, documentation?: string, kind?: string, spanIndex?: number) {
758765
const completions = this.getCompletionListAtCaret();
759766
if (completions) {
@@ -3046,6 +3053,10 @@ namespace FourSlashInterface {
30463053
this.state.verifyCompletionListIsEmpty(this.negative);
30473054
}
30483055

3056+
public completionListIsGlobal(expected: boolean) {
3057+
this.state.verifyCompletionListIsGlobal(expected);
3058+
}
3059+
30493060
public completionListAllowsNewIdentifier() {
30503061
this.state.verifyCompletionListAllowsNewIdentifier(this.negative);
30513062
}

src/server/client.ts

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ namespace ts.server {
214214
const response = this.processResponse<protocol.CompletionsResponse>(request);
215215

216216
return {
217+
isGlobalCompletion: false,
217218
isMemberCompletion: false,
218219
isNewIdentifierLocation: false,
219220
entries: response.body.map(entry => {

src/services/completions.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ namespace ts.Completions {
1414
return undefined;
1515
}
1616

17-
const { symbols, isMemberCompletion, isNewIdentifierLocation, location, isJsDocTagName } = completionData;
17+
const { symbols, isGlobalCompletion, isMemberCompletion, isNewIdentifierLocation, location, isJsDocTagName } = completionData;
1818

1919
if (isJsDocTagName) {
2020
// If the current position is a jsDoc tag name, only tag names should be provided for completion
21-
return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: JsDoc.getAllJsDocCompletionEntries() };
21+
return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries: JsDoc.getAllJsDocCompletionEntries() };
2222
}
2323

2424
const entries: CompletionEntry[] = [];
@@ -56,7 +56,7 @@ namespace ts.Completions {
5656
addRange(entries, keywordCompletions);
5757
}
5858

59-
return { isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, entries };
59+
return { isGlobalCompletion, isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, entries };
6060

6161
function getJavaScriptCompletionEntries(sourceFile: SourceFile, position: number, uniqueNames: Map<string>): CompletionEntry[] {
6262
const entries: CompletionEntry[] = [];
@@ -190,7 +190,7 @@ namespace ts.Completions {
190190
if (type) {
191191
getCompletionEntriesFromSymbols(type.getApparentProperties(), entries, element, /*performCharacterChecks*/false);
192192
if (entries.length) {
193-
return { isMemberCompletion: true, isNewIdentifierLocation: true, entries };
193+
return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: true, entries };
194194
}
195195
}
196196
}
@@ -209,7 +209,7 @@ namespace ts.Completions {
209209
}
210210

211211
if (entries.length) {
212-
return { isMemberCompletion: false, isNewIdentifierLocation: true, entries };
212+
return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: true, entries };
213213
}
214214

215215
return undefined;
@@ -221,7 +221,7 @@ namespace ts.Completions {
221221
if (type) {
222222
getCompletionEntriesFromSymbols(type.getApparentProperties(), entries, node, /*performCharacterChecks*/false);
223223
if (entries.length) {
224-
return { isMemberCompletion: true, isNewIdentifierLocation: true, entries };
224+
return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: true, entries };
225225
}
226226
}
227227
return undefined;
@@ -233,7 +233,7 @@ namespace ts.Completions {
233233
const entries: CompletionEntry[] = [];
234234
addStringLiteralCompletionsFromType(type, entries);
235235
if (entries.length) {
236-
return { isMemberCompletion: false, isNewIdentifierLocation: false, entries };
236+
return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries };
237237
}
238238
}
239239
return undefined;
@@ -281,6 +281,7 @@ namespace ts.Completions {
281281
entries = getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, span);
282282
}
283283
return {
284+
isGlobalCompletion: false,
284285
isMemberCompletion: false,
285286
isNewIdentifierLocation: true,
286287
entries
@@ -558,6 +559,7 @@ namespace ts.Completions {
558559
}
559560

560561
return {
562+
isGlobalCompletion: false,
561563
isMemberCompletion: false,
562564
isNewIdentifierLocation: true,
563565
entries
@@ -812,7 +814,7 @@ namespace ts.Completions {
812814
}
813815

814816
if (isJsDocTagName) {
815-
return { symbols: undefined, isMemberCompletion: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, isJsDocTagName };
817+
return { symbols: undefined, isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, isJsDocTagName };
816818
}
817819

818820
if (!insideJsDocTagExpression) {
@@ -884,6 +886,7 @@ namespace ts.Completions {
884886
}
885887

886888
const semanticStart = timestamp();
889+
let isGlobalCompletion = false;
887890
let isMemberCompletion: boolean;
888891
let isNewIdentifierLocation: boolean;
889892
let symbols: Symbol[] = [];
@@ -919,14 +922,16 @@ namespace ts.Completions {
919922
if (!tryGetGlobalSymbols()) {
920923
return undefined;
921924
}
925+
isGlobalCompletion = true;
922926
}
923927

924928
log("getCompletionData: Semantic work: " + (timestamp() - semanticStart));
925929

926-
return { symbols, isMemberCompletion, isNewIdentifierLocation, location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), isJsDocTagName };
930+
return { symbols, isGlobalCompletion, isMemberCompletion, isNewIdentifierLocation, location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), isJsDocTagName };
927931

928932
function getTypeScriptMemberSymbols(): void {
929933
// Right of dot member completion list
934+
isGlobalCompletion = false;
930935
isMemberCompletion = true;
931936
isNewIdentifierLocation = false;
932937

src/services/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ namespace ts {
503503
}
504504

505505
export interface CompletionInfo {
506+
isGlobalCompletion: boolean;
506507
isMemberCompletion: boolean;
507508
isNewIdentifierLocation: boolean; // true when the current location also allows for a new identifier
508509
entries: CompletionEntry[];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
/////// <reference path="/*1*/..\services\services.ts" /> // no globals in reference paths
4+
////import { /*2*/ } from "./file.ts"; // no globals in imports
5+
////var test = "/*3*/"; // no globals in strings
6+
/////*4*/class A { // insert globals
7+
//// foo(): string { return ''; }
8+
////}
9+
////
10+
////class /*5*/B extends A { // no globals after class keyword
11+
//// bar(): string {
12+
//// /*6*/ // insert globals
13+
//// return '';
14+
//// }
15+
////}
16+
////
17+
////class C</*7*/ U extends A, T extends A> { // no globals at beginning of generics
18+
//// x: U;
19+
//// y = this./*8*/x; // no globals inserted for member completions
20+
//// /*9*/ // insert globals
21+
////}
22+
/////*10*/ // insert globals
23+
goTo.marker("1");
24+
verify.completionListIsGlobal(false);
25+
goTo.marker("2");
26+
verify.completionListIsGlobal(false);
27+
goTo.marker("3");
28+
verify.completionListIsGlobal(false);
29+
goTo.marker("4");
30+
verify.completionListIsGlobal(true);
31+
goTo.marker("5");
32+
verify.completionListIsGlobal(false);
33+
goTo.marker("6");
34+
verify.completionListIsGlobal(true);
35+
goTo.marker("7");
36+
verify.completionListIsGlobal(false);
37+
goTo.marker("8");
38+
verify.completionListIsGlobal(false);
39+
goTo.marker("9");
40+
verify.completionListIsGlobal(true);
41+
goTo.marker("10");
42+
verify.completionListIsGlobal(true);

0 commit comments

Comments
 (0)