mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-10 15:25:54 -06:00
add completion filter for function like body (#21257)
This commit is contained in:
parent
1edd500ae3
commit
d1ff12e0a6
@ -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:
|
||||
|
||||
43
tests/cases/fourslash/completionInFunctionLikeBody.ts
Normal file
43
tests/cases/fourslash/completionInFunctionLikeBody.ts
Normal file
@ -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();
|
||||
@ -6,5 +6,5 @@
|
||||
////var f = function () { return new/**/; }
|
||||
|
||||
goTo.marker();
|
||||
verify.completionListCount(116);
|
||||
verify.completionListCount(107);
|
||||
verify.completionListContains('new');
|
||||
|
||||
@ -5,5 +5,5 @@
|
||||
////var f = function (s) { return this/**/; }
|
||||
|
||||
goTo.marker();
|
||||
verify.completionListCount(117);
|
||||
verify.completionListCount(108);
|
||||
verify.completionListContains('this')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user