Skip to content

Commit b2c555a

Browse files
committed
Added new keword compeltion filter for assertions
1 parent 74805c2 commit b2c555a

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/services/completions.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace ts.Completions {
3838
InterfaceElementKeywords, // Keywords inside interface body
3939
ConstructorParameterKeywords, // Keywords at constructor parameter
4040
FunctionLikeBodyKeywords, // Keywords at function like body
41+
TypeAssertionKeywords,
4142
TypeKeywords,
4243
Last = TypeKeywords
4344
}
@@ -441,7 +442,7 @@ namespace ts.Completions {
441442
(symbol.escapedName === InternalSymbolName.ExportEquals))
442443
// Name of "export default foo;" is "foo". Name of "export default 0" is the filename converted to camelCase.
443444
? firstDefined(symbol.declarations, d => isExportAssignment(d) && isIdentifier(d.expression) ? d.expression.text : undefined)
444-
|| codefix.moduleSymbolToValidIdentifier(origin.moduleSymbol, target)
445+
|| codefix.moduleSymbolToValidIdentifier(origin.moduleSymbol, target)
445446
: symbol.name;
446447
}
447448

@@ -632,9 +633,9 @@ namespace ts.Completions {
632633
// At `,`, treat this as the next argument after the comma.
633634
? checker.getContextualTypeForArgumentAtIndex(argInfo.invocation, argInfo.argumentIndex + (previousToken.kind === SyntaxKind.CommaToken ? 1 : 0))
634635
: isEqualityOperatorKind(previousToken.kind) && isBinaryExpression(parent) && isEqualityOperatorKind(parent.operatorToken.kind)
635-
// completion at `x ===/**/` should be for the right side
636-
? checker.getTypeAtLocation(parent.left)
637-
: checker.getContextualType(previousToken as Expression);
636+
// completion at `x ===/**/` should be for the right side
637+
? checker.getTypeAtLocation(parent.left)
638+
: checker.getContextualType(previousToken as Expression);
638639
}
639640
}
640641

@@ -1181,7 +1182,11 @@ namespace ts.Completions {
11811182
function filterGlobalCompletion(symbols: Symbol[]): void {
11821183
const isTypeOnly = isTypeOnlyCompletion();
11831184
const allowTypes = isTypeOnly || !isContextTokenValueLocation(contextToken) && isPossiblyTypeArgumentPosition(contextToken, sourceFile, typeChecker);
1184-
if (isTypeOnly) keywordFilters = KeywordCompletionFilters.TypeKeywords;
1185+
if (isTypeOnly) {
1186+
keywordFilters = isInsideTypeAssertion()
1187+
? KeywordCompletionFilters.TypeAssertionKeywords
1188+
: KeywordCompletionFilters.TypeKeywords;
1189+
}
11851190

11861191
filterMutate(symbols, symbol => {
11871192
if (!isSourceFile(location)) {
@@ -1211,6 +1216,10 @@ namespace ts.Completions {
12111216
});
12121217
}
12131218

1219+
function isInsideTypeAssertion(): boolean {
1220+
return isAsExpression(contextToken.parent);
1221+
}
1222+
12141223
function isTypeOnlyCompletion(): boolean {
12151224
return insideJsDocTagTypeExpression || !isContextTokenValueLocation(contextToken) && (isPartOfTypeNode(location) || isContextTokenTypeLocation(contextToken));
12161225
}
@@ -1434,7 +1443,7 @@ namespace ts.Completions {
14341443
// 3. at the end of a regular expression (due to trailing flags like '/foo/g').
14351444
return (isRegularExpressionLiteral(contextToken) || isStringTextContainingNode(contextToken)) && (
14361445
rangeContainsPositionExclusive(createTextRangeFromSpan(createTextSpanFromNode(contextToken)), position) ||
1437-
position === contextToken.end && (!!contextToken.isUnterminated || isRegularExpressionLiteral(contextToken)));
1446+
position === contextToken.end && (!!contextToken.isUnterminated || isRegularExpressionLiteral(contextToken)));
14381447
}
14391448

14401449
/**
@@ -1944,8 +1953,8 @@ namespace ts.Completions {
19441953

19451954
return baseSymbols.filter(propertySymbol =>
19461955
!existingMemberNames.has(propertySymbol.escapedName) &&
1947-
!!propertySymbol.declarations &&
1948-
!(getDeclarationModifierFlagsFromSymbol(propertySymbol) & ModifierFlags.Private));
1956+
!!propertySymbol.declarations &&
1957+
!(getDeclarationModifierFlagsFromSymbol(propertySymbol) & ModifierFlags.Private));
19491958
}
19501959

19511960
/**
@@ -2057,6 +2066,8 @@ namespace ts.Completions {
20572066
return isParameterPropertyModifier(kind);
20582067
case KeywordCompletionFilters.FunctionLikeBodyKeywords:
20592068
return isFunctionLikeBodyKeyword(kind);
2069+
case KeywordCompletionFilters.TypeAssertionKeywords:
2070+
return isTypeKeyword(kind) || kind === SyntaxKind.ConstKeyword;
20602071
case KeywordCompletionFilters.TypeKeywords:
20612072
return isTypeKeyword(kind);
20622073
default:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////const a = {
4+
//// b: 42 as con/*0*/
5+
////};
6+
////
7+
////1 as con/*1*/
8+
////
9+
////const b = 42 as /*2*/
10+
11+
verify.completions({ marker: test.markers(), includes: [{ name: "const", sortText: completion.SortText.GlobalsOrKeywords }] });

0 commit comments

Comments
 (0)