Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace ts.Completions {
None,
ClassElementKeywords, // Keywords at class keyword
ConstructorParameterKeywords, // Keywords at constructor parameter
FunctionLikeBodyKeywords // Keywords at function like body
}

export function getCompletionsAtPosition(
Expand Down Expand Up @@ -1032,6 +1033,10 @@ namespace ts.Completions {
return true;
}

if (tryGetFunctionLikeBodyCompletionContainer(contextToken)) {
keywordFilters = KeywordCompletionFilters.FunctionLikeBodyKeywords;
}

if (classLikeContainer = tryGetClassLikeCompletionContainer(contextToken)) {
// cursor inside class declaration
getGetClassLikeCompletionSymbols(classLikeContainer);
Expand Down Expand Up @@ -1648,6 +1653,22 @@ namespace ts.Completions {
return undefined;
}

function tryGetFunctionLikeBodyCompletionContainer(contextToken: Node): FunctionLikeDeclaration {
if (contextToken) {
let prev: Node;
const container = findAncestor(contextToken.parent, (node: Node) => {
if (isClassLike(node)) {
return "quit";
}
if (isFunctionLikeDeclaration(node) && prev === node.body) {
return true;
}
prev = node;
});
return container && container as FunctionLikeDeclaration;
}
}

function tryGetContainingJsxElement(contextToken: Node): JsxOpeningLikeElement {
if (contextToken) {
const parent = contextToken.parent;
Expand Down Expand Up @@ -2086,6 +2107,8 @@ namespace ts.Completions {
return getFilteredKeywordCompletions(isClassMemberCompletionKeywordText);
case KeywordCompletionFilters.ConstructorParameterKeywords:
return getFilteredKeywordCompletions(isConstructorParameterCompletionKeywordText);
case KeywordCompletionFilters.FunctionLikeBodyKeywords:
return getFilteredKeywordCompletions(isFunctionLikeBodyCompletionKeywordText);
default:
Debug.assertNever(keywordFilter);
}
Expand Down Expand Up @@ -2148,6 +2171,26 @@ namespace ts.Completions {
return isConstructorParameterCompletionKeyword(stringToToken(text));
}

function isFunctionLikeBodyCompletionKeyword(kind: SyntaxKind) {
switch (kind) {
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.ReadonlyKeyword:
case SyntaxKind.ConstructorKeyword:
case SyntaxKind.StaticKeyword:
case SyntaxKind.AbstractKeyword:
case SyntaxKind.GetKeyword:
case SyntaxKind.SetKeyword:
return false;
}
return true;
}

function isFunctionLikeBodyCompletionKeywordText(text: string) {
return isFunctionLikeBodyCompletionKeyword(stringToToken(text));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is not that just !isClassMemberCompletionKeywordText(text) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'async' keyword is not included

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and in my opinion they should be orthogonal

}

function isEqualityOperatorKind(kind: ts.SyntaxKind): kind is EqualityOperator {
switch (kind) {
case ts.SyntaxKind.EqualsEqualsEqualsToken:
Expand Down
43 changes: 43 additions & 0 deletions tests/cases/fourslash/completionInFunctionLikeBody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/// <reference path='fourslash.ts'/>

//// class Foo {
//// bar () {
//// /*1*/
//// class Foo1 {
//// bar1 () {
//// /*2*/
//// }
//// /*3*/
//// }
//// }
//// /*4*/
//// }


goTo.marker("1");
verify.not.completionListContains("public", "public", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("private", "private", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("protected", "protected", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("constructor", "constructor", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("readonly", "readonly", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("static", "static", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("abstract", "abstract", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("get", "get", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("set", "set", /*documentation*/ undefined, "keyword");

goTo.marker("2");
verify.not.completionListContains("public", "public", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("private", "private", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("protected", "protected", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("constructor", "constructor", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("readonly", "readonly", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("static", "static", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("abstract", "abstract", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("get", "get", /*documentation*/ undefined, "keyword");
verify.not.completionListContains("set", "set", /*documentation*/ undefined, "keyword");

goTo.marker("3");
verify.completionListContainsClassElementKeywords();

goTo.marker("4");
verify.completionListContainsClassElementKeywords();
2 changes: 1 addition & 1 deletion tests/cases/fourslash/completionInJSDocFunctionNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
////var f = function () { return new/**/; }

goTo.marker();
verify.completionListCount(116);
verify.completionListCount(107);
verify.completionListContains('new');
2 changes: 1 addition & 1 deletion tests/cases/fourslash/completionInJSDocFunctionThis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
////var f = function (s) { return this/**/; }

goTo.marker();
verify.completionListCount(117);
verify.completionListCount(108);
verify.completionListContains('this')