add completion filter for function like body (#21257)

This commit is contained in:
Wenlu Wang
2018-01-20 09:26:58 +08:00
committed by Mohamed Hegazy
parent 1edd500ae3
commit d1ff12e0a6
4 changed files with 88 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ namespace ts.Completions {
None,
ClassElementKeywords, // Keywords at class keyword
ConstructorParameterKeywords, // Keywords at constructor parameter
FunctionLikeBodyKeywords // Keywords at function like body
}
export function getCompletionsAtPosition(
@@ -1060,6 +1061,10 @@ namespace ts.Completions {
return true;
}
if (tryGetFunctionLikeBodyCompletionContainer(contextToken)) {
keywordFilters = KeywordCompletionFilters.FunctionLikeBodyKeywords;
}
if (classLikeContainer = tryGetClassLikeCompletionContainer(contextToken)) {
// cursor inside class declaration
getGetClassLikeCompletionSymbols(classLikeContainer);
@@ -1688,6 +1693,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;
@@ -2126,6 +2147,8 @@ namespace ts.Completions {
return getFilteredKeywordCompletions(isClassMemberCompletionKeywordText);
case KeywordCompletionFilters.ConstructorParameterKeywords:
return getFilteredKeywordCompletions(isConstructorParameterCompletionKeywordText);
case KeywordCompletionFilters.FunctionLikeBodyKeywords:
return getFilteredKeywordCompletions(isFunctionLikeBodyCompletionKeywordText);
default:
Debug.assertNever(keywordFilter);
}
@@ -2188,6 +2211,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));
}
function isEqualityOperatorKind(kind: ts.SyntaxKind): kind is EqualityOperator {
switch (kind) {
case ts.SyntaxKind.EqualsEqualsEqualsToken: