Skip to content

Commit 92a47ee

Browse files
author
王文璐
committed
add filter of function like body
1 parent 7a89c96 commit 92a47ee

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

src/services/completions.ts

+43
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace ts.Completions {
1818
None,
1919
ClassElementKeywords, // Keywords at class keyword
2020
ConstructorParameterKeywords, // Keywords at constructor parameter
21+
FunctionLikeBodyKeywords // Keywords at function like body
2122
}
2223

2324
export function getCompletionsAtPosition(
@@ -1032,6 +1033,10 @@ namespace ts.Completions {
10321033
return true;
10331034
}
10341035

1036+
if (tryGetFunctionLikeBodyCompletionContainer(contextToken)) {
1037+
keywordFilters = KeywordCompletionFilters.FunctionLikeBodyKeywords;
1038+
}
1039+
10351040
if (classLikeContainer = tryGetClassLikeCompletionContainer(contextToken)) {
10361041
// cursor inside class declaration
10371042
getGetClassLikeCompletionSymbols(classLikeContainer);
@@ -1648,6 +1653,22 @@ namespace ts.Completions {
16481653
return undefined;
16491654
}
16501655

1656+
function tryGetFunctionLikeBodyCompletionContainer(contextToken: Node): FunctionLikeDeclaration {
1657+
if (contextToken) {
1658+
let prev: Node;
1659+
const container = findAncestor(contextToken.parent, (node: Node) => {
1660+
if (isClassLike(node)) {
1661+
return "quit";
1662+
}
1663+
if (isFunctionLikeDeclaration(node) && prev === node.body) {
1664+
return true;
1665+
}
1666+
prev = node;
1667+
});
1668+
return container && container as FunctionLikeDeclaration;
1669+
}
1670+
}
1671+
16511672
function tryGetContainingJsxElement(contextToken: Node): JsxOpeningLikeElement {
16521673
if (contextToken) {
16531674
const parent = contextToken.parent;
@@ -2086,6 +2107,8 @@ namespace ts.Completions {
20862107
return getFilteredKeywordCompletions(isClassMemberCompletionKeywordText);
20872108
case KeywordCompletionFilters.ConstructorParameterKeywords:
20882109
return getFilteredKeywordCompletions(isConstructorParameterCompletionKeywordText);
2110+
case KeywordCompletionFilters.FunctionLikeBodyKeywords:
2111+
return getFilteredKeywordCompletions(isFunctionLikeBodyCompletionKeywordText);
20892112
default:
20902113
Debug.assertNever(keywordFilter);
20912114
}
@@ -2148,6 +2171,26 @@ namespace ts.Completions {
21482171
return isConstructorParameterCompletionKeyword(stringToToken(text));
21492172
}
21502173

2174+
function isFunctionLikeBodyCompletionKeyword(kind: SyntaxKind) {
2175+
switch (kind) {
2176+
case SyntaxKind.PublicKeyword:
2177+
case SyntaxKind.PrivateKeyword:
2178+
case SyntaxKind.ProtectedKeyword:
2179+
case SyntaxKind.ReadonlyKeyword:
2180+
case SyntaxKind.ConstructorKeyword:
2181+
case SyntaxKind.StaticKeyword:
2182+
case SyntaxKind.AbstractKeyword:
2183+
case SyntaxKind.GetKeyword:
2184+
case SyntaxKind.SetKeyword:
2185+
return false;
2186+
}
2187+
return true;
2188+
}
2189+
2190+
function isFunctionLikeBodyCompletionKeywordText(text: string) {
2191+
return isFunctionLikeBodyCompletionKeyword(stringToToken(text));
2192+
}
2193+
21512194
function isEqualityOperatorKind(kind: ts.SyntaxKind): kind is EqualityOperator {
21522195
switch (kind) {
21532196
case ts.SyntaxKind.EqualsEqualsEqualsToken:
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

+1-1
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

+1-1
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)