Skip to content

Commit d1ff12e

Browse files
Kingwlmhegazy
authored andcommitted
add completion filter for function like body (microsoft#21257)
1 parent 1edd500 commit d1ff12e

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

src/services/completions.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace ts.Completions {
2020
None,
2121
ClassElementKeywords, // Keywords at class keyword
2222
ConstructorParameterKeywords, // Keywords at constructor parameter
23+
FunctionLikeBodyKeywords // Keywords at function like body
2324
}
2425

2526
export function getCompletionsAtPosition(
@@ -1060,6 +1061,10 @@ namespace ts.Completions {
10601061
return true;
10611062
}
10621063

1064+
if (tryGetFunctionLikeBodyCompletionContainer(contextToken)) {
1065+
keywordFilters = KeywordCompletionFilters.FunctionLikeBodyKeywords;
1066+
}
1067+
10631068
if (classLikeContainer = tryGetClassLikeCompletionContainer(contextToken)) {
10641069
// cursor inside class declaration
10651070
getGetClassLikeCompletionSymbols(classLikeContainer);
@@ -1688,6 +1693,22 @@ namespace ts.Completions {
16881693
return undefined;
16891694
}
16901695

1696+
function tryGetFunctionLikeBodyCompletionContainer(contextToken: Node): FunctionLikeDeclaration {
1697+
if (contextToken) {
1698+
let prev: Node;
1699+
const container = findAncestor(contextToken.parent, (node: Node) => {
1700+
if (isClassLike(node)) {
1701+
return "quit";
1702+
}
1703+
if (isFunctionLikeDeclaration(node) && prev === node.body) {
1704+
return true;
1705+
}
1706+
prev = node;
1707+
});
1708+
return container && container as FunctionLikeDeclaration;
1709+
}
1710+
}
1711+
16911712
function tryGetContainingJsxElement(contextToken: Node): JsxOpeningLikeElement {
16921713
if (contextToken) {
16931714
const parent = contextToken.parent;
@@ -2126,6 +2147,8 @@ namespace ts.Completions {
21262147
return getFilteredKeywordCompletions(isClassMemberCompletionKeywordText);
21272148
case KeywordCompletionFilters.ConstructorParameterKeywords:
21282149
return getFilteredKeywordCompletions(isConstructorParameterCompletionKeywordText);
2150+
case KeywordCompletionFilters.FunctionLikeBodyKeywords:
2151+
return getFilteredKeywordCompletions(isFunctionLikeBodyCompletionKeywordText);
21292152
default:
21302153
Debug.assertNever(keywordFilter);
21312154
}
@@ -2188,6 +2211,26 @@ namespace ts.Completions {
21882211
return isConstructorParameterCompletionKeyword(stringToToken(text));
21892212
}
21902213

2214+
function isFunctionLikeBodyCompletionKeyword(kind: SyntaxKind) {
2215+
switch (kind) {
2216+
case SyntaxKind.PublicKeyword:
2217+
case SyntaxKind.PrivateKeyword:
2218+
case SyntaxKind.ProtectedKeyword:
2219+
case SyntaxKind.ReadonlyKeyword:
2220+
case SyntaxKind.ConstructorKeyword:
2221+
case SyntaxKind.StaticKeyword:
2222+
case SyntaxKind.AbstractKeyword:
2223+
case SyntaxKind.GetKeyword:
2224+
case SyntaxKind.SetKeyword:
2225+
return false;
2226+
}
2227+
return true;
2228+
}
2229+
2230+
function isFunctionLikeBodyCompletionKeywordText(text: string) {
2231+
return isFunctionLikeBodyCompletionKeyword(stringToToken(text));
2232+
}
2233+
21912234
function isEqualityOperatorKind(kind: ts.SyntaxKind): kind is EqualityOperator {
21922235
switch (kind) {
21932236
case ts.SyntaxKind.EqualsEqualsEqualsToken:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
//// class Foo {
4+
//// bar () {
5+
//// /*1*/
6+
//// class Foo1 {
7+
//// bar1 () {
8+
//// /*2*/
9+
//// }
10+
//// /*3*/
11+
//// }
12+
//// }
13+
//// /*4*/
14+
//// }
15+
16+
17+
goTo.marker("1");
18+
verify.not.completionListContains("public", "public", /*documentation*/ undefined, "keyword");
19+
verify.not.completionListContains("private", "private", /*documentation*/ undefined, "keyword");
20+
verify.not.completionListContains("protected", "protected", /*documentation*/ undefined, "keyword");
21+
verify.not.completionListContains("constructor", "constructor", /*documentation*/ undefined, "keyword");
22+
verify.not.completionListContains("readonly", "readonly", /*documentation*/ undefined, "keyword");
23+
verify.not.completionListContains("static", "static", /*documentation*/ undefined, "keyword");
24+
verify.not.completionListContains("abstract", "abstract", /*documentation*/ undefined, "keyword");
25+
verify.not.completionListContains("get", "get", /*documentation*/ undefined, "keyword");
26+
verify.not.completionListContains("set", "set", /*documentation*/ undefined, "keyword");
27+
28+
goTo.marker("2");
29+
verify.not.completionListContains("public", "public", /*documentation*/ undefined, "keyword");
30+
verify.not.completionListContains("private", "private", /*documentation*/ undefined, "keyword");
31+
verify.not.completionListContains("protected", "protected", /*documentation*/ undefined, "keyword");
32+
verify.not.completionListContains("constructor", "constructor", /*documentation*/ undefined, "keyword");
33+
verify.not.completionListContains("readonly", "readonly", /*documentation*/ undefined, "keyword");
34+
verify.not.completionListContains("static", "static", /*documentation*/ undefined, "keyword");
35+
verify.not.completionListContains("abstract", "abstract", /*documentation*/ undefined, "keyword");
36+
verify.not.completionListContains("get", "get", /*documentation*/ undefined, "keyword");
37+
verify.not.completionListContains("set", "set", /*documentation*/ undefined, "keyword");
38+
39+
goTo.marker("3");
40+
verify.completionListContainsClassElementKeywords();
41+
42+
goTo.marker("4");
43+
verify.completionListContainsClassElementKeywords();

tests/cases/fourslash/completionInJSDocFunctionNew.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
////var f = function () { return new/**/; }
77

88
goTo.marker();
9-
verify.completionListCount(116);
9+
verify.completionListCount(107);
1010
verify.completionListContains('new');

tests/cases/fourslash/completionInJSDocFunctionThis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
////var f = function (s) { return this/**/; }
66

77
goTo.marker();
8-
verify.completionListCount(117);
8+
verify.completionListCount(108);
99
verify.completionListContains('this')

0 commit comments

Comments
 (0)