Skip to content

Commit ae25d09

Browse files
KingwlAndy
authored and
Andy
committed
add supports of completion label list (#20362)
1 parent 3a3bb8e commit ae25d09

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/services/completions.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ namespace ts.Completions {
3939
return getStringLiteralCompletionEntries(sourceFile, position, typeChecker, compilerOptions, host, log);
4040
}
4141

42+
const contextToken = findPrecedingToken(position, sourceFile);
43+
if (isInBreakOrContinue(contextToken)) {
44+
return getLabelCompletionAtPosition(contextToken);
45+
}
46+
4247
const completionData = getCompletionData(typeChecker, log, sourceFile, position, allSourceFiles, options, compilerOptions.target);
4348
if (!completionData) {
4449
return undefined;
@@ -223,6 +228,18 @@ namespace ts.Completions {
223228
return uniques;
224229
}
225230

231+
function isInBreakOrContinue(contextToken: Node): boolean {
232+
return contextToken && isBreakOrContinueStatement(contextToken.parent) &&
233+
(contextToken.kind === SyntaxKind.BreakKeyword || contextToken.kind === SyntaxKind.ContinueKeyword || contextToken.kind === SyntaxKind.Identifier);
234+
}
235+
236+
function getLabelCompletionAtPosition(contextToken: Node): CompletionInfo | undefined {
237+
const entries = getLabelStatementCompletions(contextToken.parent);
238+
if (entries.length) {
239+
return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries };
240+
}
241+
}
242+
226243
function getStringLiteralCompletionEntries(sourceFile: SourceFile, position: number, typeChecker: TypeChecker, compilerOptions: CompilerOptions, host: LanguageServiceHost, log: Log): CompletionInfo | undefined {
227244
const node = findPrecedingToken(position, sourceFile);
228245
if (!node || node.kind !== SyntaxKind.StringLiteral) {
@@ -358,6 +375,32 @@ namespace ts.Completions {
358375
return undefined;
359376
}
360377

378+
function getLabelStatementCompletions(node: Node): CompletionEntry[] {
379+
const entries: CompletionEntry[] = [];
380+
const uniques = createMap<true>();
381+
let current = node;
382+
383+
while (current) {
384+
if (isFunctionLike(current)) {
385+
break;
386+
}
387+
if (isLabeledStatement(current)) {
388+
const name = current.label.text;
389+
if (!uniques.has(name)) {
390+
uniques.set(name, true);
391+
entries.push({
392+
name,
393+
kindModifiers: ScriptElementKindModifier.none,
394+
kind: ScriptElementKind.label,
395+
sortText: "0"
396+
});
397+
}
398+
}
399+
current = current.parent;
400+
}
401+
return entries;
402+
}
403+
361404
function addStringLiteralCompletionsFromType(type: Type, result: Push<CompletionEntry>, typeChecker: TypeChecker, uniques = createMap<true>()): void {
362405
if (type && type.flags & TypeFlags.TypeParameter) {
363406
type = typeChecker.getBaseConstraintOfType(type);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
//// label: while (true) {
4+
//// break /*1*/
5+
//// continue /*2*/
6+
//// testlabel: while (true) {
7+
//// break /*3*/
8+
//// continue /*4*/
9+
//// break tes/*5*/
10+
//// continue tes/*6*/
11+
//// }
12+
//// break /*7*/
13+
//// break; /*8*/
14+
////}
15+
16+
goTo.marker("1");
17+
verify.completionListContains("label");
18+
19+
goTo.marker("2");
20+
verify.completionListContains("label");
21+
verify.not.completionListContains("testlabel");
22+
23+
goTo.marker("3");
24+
verify.completionListContains("label");
25+
verify.completionListContains("testlabel");
26+
27+
goTo.marker("4");
28+
verify.completionListContains("label");
29+
verify.completionListContains("testlabel");
30+
31+
goTo.marker("5");
32+
verify.completionListContains("testlabel");
33+
verify.completionListContains("label");
34+
35+
goTo.marker("6");
36+
verify.completionListContains("testlabel");
37+
verify.completionListContains("label");
38+
39+
goTo.marker("7");
40+
verify.completionListContains("label");
41+
verify.not.completionListContains("testlabel");
42+
43+
goTo.marker("8");
44+
verify.not.completionListContains("label");

0 commit comments

Comments
 (0)