Merge pull request #22421 from Microsoft/typesInTypeArguments
Allow types as well as values in possibly type argument location
This commit is contained in:
commit
fa2b7ff6b3
5 changed files with 168 additions and 7 deletions
|
@ -435,7 +435,7 @@ namespace FourSlash {
|
|||
}
|
||||
}
|
||||
|
||||
private markerName(m: Marker): string {
|
||||
public markerName(m: Marker): string {
|
||||
return ts.forEachEntry(this.testData.markerPositions, (marker, name) => {
|
||||
if (marker === m) {
|
||||
return name;
|
||||
|
@ -3768,6 +3768,10 @@ namespace FourSlashInterface {
|
|||
return this.state.getMarkerByName(name);
|
||||
}
|
||||
|
||||
public markerName(m: FourSlash.Marker) {
|
||||
return this.state.markerName(m);
|
||||
}
|
||||
|
||||
public ranges(): FourSlash.Range[] {
|
||||
return this.state.getRanges();
|
||||
}
|
||||
|
@ -3810,6 +3814,7 @@ namespace FourSlashInterface {
|
|||
this.state.goToEachMarker(markers, typeof a === "function" ? a : b);
|
||||
}
|
||||
|
||||
|
||||
public rangeStart(range: FourSlash.Range) {
|
||||
this.state.goToRangeStart(range);
|
||||
}
|
||||
|
|
|
@ -994,6 +994,7 @@ namespace ts.Completions {
|
|||
// Since this is qualified name check its a type node location
|
||||
const isTypeLocation = insideJsDocTagTypeExpression || isPartOfTypeNode(node.parent);
|
||||
const isRhsOfImportDeclaration = isInRightSideOfInternalImportEqualsDeclaration(node);
|
||||
const allowTypeOrValue = isRhsOfImportDeclaration || (!isTypeLocation && isPossiblyTypeArgumentPosition(contextToken, sourceFile));
|
||||
if (isEntityName(node)) {
|
||||
let symbol = typeChecker.getSymbolAtLocation(node);
|
||||
if (symbol) {
|
||||
|
@ -1004,7 +1005,7 @@ namespace ts.Completions {
|
|||
const exportedSymbols = Debug.assertEachDefined(typeChecker.getExportsOfModule(symbol), "getExportsOfModule() should all be defined");
|
||||
const isValidValueAccess = (symbol: Symbol) => typeChecker.isValidPropertyAccess(<PropertyAccessExpression>(node.parent), symbol.name);
|
||||
const isValidTypeAccess = (symbol: Symbol) => symbolCanBeReferencedAtTypeLocation(symbol);
|
||||
const isValidAccess = isRhsOfImportDeclaration ?
|
||||
const isValidAccess = allowTypeOrValue ?
|
||||
// Any kind is allowed when dotting off namespace in internal import equals declaration
|
||||
(symbol: Symbol) => isValidTypeAccess(symbol) || isValidValueAccess(symbol) :
|
||||
isTypeLocation ? isValidTypeAccess : isValidValueAccess;
|
||||
|
@ -1173,8 +1174,9 @@ namespace ts.Completions {
|
|||
}
|
||||
|
||||
function filterGlobalCompletion(symbols: Symbol[]): void {
|
||||
const isTypeCompletion = insideJsDocTagTypeExpression || !isContextTokenValueLocation(contextToken) && (isPartOfTypeNode(location) || isContextTokenTypeLocation(contextToken));
|
||||
if (isTypeCompletion) keywordFilters = KeywordCompletionFilters.TypeKeywords;
|
||||
const isTypeOnlyCompletion = insideJsDocTagTypeExpression || !isContextTokenValueLocation(contextToken) && (isPartOfTypeNode(location) || isContextTokenTypeLocation(contextToken));
|
||||
const allowTypes = isTypeOnlyCompletion || !isContextTokenValueLocation(contextToken) && isPossiblyTypeArgumentPosition(contextToken, sourceFile);
|
||||
if (isTypeOnlyCompletion) keywordFilters = KeywordCompletionFilters.TypeKeywords;
|
||||
|
||||
filterMutate(symbols, symbol => {
|
||||
if (!isSourceFile(location)) {
|
||||
|
@ -1190,9 +1192,12 @@ namespace ts.Completions {
|
|||
return !!(symbol.flags & SymbolFlags.Namespace);
|
||||
}
|
||||
|
||||
if (isTypeCompletion) {
|
||||
if (allowTypes) {
|
||||
// Its a type, but you can reach it by namespace.type as well
|
||||
return symbolCanBeReferencedAtTypeLocation(symbol);
|
||||
const symbolAllowedAsType = symbolCanBeReferencedAtTypeLocation(symbol);
|
||||
if (symbolAllowedAsType || isTypeOnlyCompletion) {
|
||||
return symbolAllowedAsType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1204,7 +1209,7 @@ namespace ts.Completions {
|
|||
function isContextTokenValueLocation(contextToken: Node) {
|
||||
return contextToken &&
|
||||
contextToken.kind === SyntaxKind.TypeOfKeyword &&
|
||||
contextToken.parent.kind === SyntaxKind.TypeQuery;
|
||||
(contextToken.parent.kind === SyntaxKind.TypeQuery || isTypeOfExpression(contextToken.parent));
|
||||
}
|
||||
|
||||
function isContextTokenTypeLocation(contextToken: Node): boolean {
|
||||
|
|
|
@ -890,6 +890,114 @@ namespace ts {
|
|||
return isTemplateLiteralKind(token.kind) && position > token.getStart(sourceFile);
|
||||
}
|
||||
|
||||
export function findPrecedingMatchingToken(token: Node, matchingTokenKind: SyntaxKind, sourceFile: SourceFile) {
|
||||
const tokenKind = token.kind;
|
||||
let remainingMatchingTokens = 0;
|
||||
while (true) {
|
||||
token = findPrecedingToken(token.getFullStart(), sourceFile);
|
||||
if (!token) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (token.kind === matchingTokenKind) {
|
||||
if (remainingMatchingTokens === 0) {
|
||||
return token;
|
||||
}
|
||||
|
||||
remainingMatchingTokens--;
|
||||
}
|
||||
else if (token.kind === tokenKind) {
|
||||
remainingMatchingTokens++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function isPossiblyTypeArgumentPosition(token: Node, sourceFile: SourceFile) {
|
||||
// This function determines if the node could be type argument position
|
||||
// Since during editing, when type argument list is not complete,
|
||||
// the tree could be of any shape depending on the tokens parsed before current node,
|
||||
// scanning of the previous identifier followed by "<" before current node would give us better result
|
||||
// Note that we also balance out the already provided type arguments, arrays, object literals while doing so
|
||||
let remainingLessThanTokens = 0;
|
||||
while (token) {
|
||||
switch (token.kind) {
|
||||
case SyntaxKind.LessThanToken:
|
||||
// Found the beginning of the generic argument expression
|
||||
token = findPrecedingToken(token.getFullStart(), sourceFile);
|
||||
const tokenIsIdentifier = token && isIdentifier(token);
|
||||
if (!remainingLessThanTokens || !tokenIsIdentifier) {
|
||||
return tokenIsIdentifier;
|
||||
}
|
||||
remainingLessThanTokens--;
|
||||
break;
|
||||
|
||||
case SyntaxKind.GreaterThanGreaterThanGreaterThanToken:
|
||||
remainingLessThanTokens = + 3;
|
||||
break;
|
||||
|
||||
case SyntaxKind.GreaterThanGreaterThanToken:
|
||||
remainingLessThanTokens = + 2;
|
||||
break;
|
||||
|
||||
case SyntaxKind.GreaterThanToken:
|
||||
remainingLessThanTokens++;
|
||||
break;
|
||||
|
||||
case SyntaxKind.CloseBraceToken:
|
||||
// This can be object type, skip untill we find the matching open brace token
|
||||
// Skip untill the matching open brace token
|
||||
token = findPrecedingMatchingToken(token, SyntaxKind.OpenBraceToken, sourceFile);
|
||||
if (!token) return false;
|
||||
break;
|
||||
|
||||
case SyntaxKind.CloseParenToken:
|
||||
// This can be object type, skip untill we find the matching open brace token
|
||||
// Skip untill the matching open brace token
|
||||
token = findPrecedingMatchingToken(token, SyntaxKind.OpenParenToken, sourceFile);
|
||||
if (!token) return false;
|
||||
break;
|
||||
|
||||
case SyntaxKind.CloseBracketToken:
|
||||
// This can be object type, skip untill we find the matching open brace token
|
||||
// Skip untill the matching open brace token
|
||||
token = findPrecedingMatchingToken(token, SyntaxKind.OpenBracketToken, sourceFile);
|
||||
if (!token) return false;
|
||||
break;
|
||||
|
||||
// Valid tokens in a type name. Skip.
|
||||
case SyntaxKind.CommaToken:
|
||||
case SyntaxKind.EqualsGreaterThanToken:
|
||||
|
||||
case SyntaxKind.Identifier:
|
||||
case SyntaxKind.StringLiteral:
|
||||
case SyntaxKind.NumericLiteral:
|
||||
case SyntaxKind.TrueKeyword:
|
||||
case SyntaxKind.FalseKeyword:
|
||||
|
||||
case SyntaxKind.TypeOfKeyword:
|
||||
case SyntaxKind.ExtendsKeyword:
|
||||
case SyntaxKind.KeyOfKeyword:
|
||||
case SyntaxKind.DotToken:
|
||||
case SyntaxKind.BarToken:
|
||||
case SyntaxKind.QuestionToken:
|
||||
case SyntaxKind.ColonToken:
|
||||
break;
|
||||
|
||||
default:
|
||||
if (isTypeNode(token)) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Invalid token in type
|
||||
return false;
|
||||
}
|
||||
|
||||
token = findPrecedingToken(token.getFullStart(), sourceFile);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the cursor at position in sourceFile is within a comment.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
////let x = 10;
|
||||
////type Type = void;
|
||||
////declare function f<T>(): void;
|
||||
////declare function f2<T, U>(): void;
|
||||
////f</*1a*/T/*2a*/y/*3a*/
|
||||
////f</*1b*/T/*2b*/y/*3b*/;
|
||||
////f</*1c*/T/*2c*/y/*3c*/>
|
||||
////f</*1d*/T/*2d*/y/*3d*/>
|
||||
////f</*1eTypeOnly*/T/*2eTypeOnly*/y/*3eTypeOnly*/>();
|
||||
////
|
||||
////f2</*1k*/T/*2k*/y/*3k*/,
|
||||
////f2</*1l*/T/*2l*/y/*3l*/,/*4l*/T/*5l*/y/*6l*/
|
||||
////f2</*1m*/T/*2m*/y/*3m*/,/*4m*/T/*5m*/y/*6m*/;
|
||||
////f2</*1n*/T/*2n*/y/*3n*/,/*4n*/T/*5n*/y/*6n*/>
|
||||
////f2</*1o*/T/*2o*/y/*3o*/,/*4o*/T/*5o*/y/*6o*/>
|
||||
////f2</*1pTypeOnly*/T/*2pTypeOnly*/y/*3pTypeOnly*/,/*4pTypeOnly*/T/*5pTypeOnly*/y/*6pTypeOnly*/>();
|
||||
////
|
||||
////f2<typeof /*1uValueOnly*/x, /*4u*/T/*5u*/y/*6u*/
|
||||
////
|
||||
////f2</*1x*/T/*2x*/y/*3x*/, () =>/*4x*/T/*5x*/y/*6x*/
|
||||
////f2<() =>/*1y*/T/*2y*/y/*3y*/, () =>/*4y*/T/*5y*/y/*6y*/
|
||||
////f2<any, () =>/*1z*/T/*2z*/y/*3z*/
|
||||
|
||||
|
||||
goTo.eachMarker((marker) => {
|
||||
const markerName = test.markerName(marker);
|
||||
if (markerName.endsWith("TypeOnly")) {
|
||||
verify.not.completionListContains("x");
|
||||
}
|
||||
else {
|
||||
verify.completionListContains("x");
|
||||
}
|
||||
|
||||
if (markerName.endsWith("ValueOnly")) {
|
||||
verify.not.completionListContains("Type");
|
||||
}
|
||||
else {
|
||||
verify.completionListContains("Type");
|
||||
}
|
||||
});
|
|
@ -113,6 +113,7 @@ declare namespace FourSlashInterface {
|
|||
class test_ {
|
||||
markers(): Marker[];
|
||||
markerNames(): string[];
|
||||
markerName(m: Marker): string;
|
||||
marker(name?: string): Marker;
|
||||
ranges(): Range[];
|
||||
spans(): Array<{ start: number, length: number }>;
|
||||
|
|
Loading…
Reference in a new issue