Add string literal completion for equality expression
This commit is contained in:
parent
cf20850e79
commit
3b1b5e241f
|
@ -173,6 +173,11 @@ namespace ts.Completions {
|
|||
// var y = require("/*completion position*/");
|
||||
return getStringLiteralCompletionEntriesFromModuleNames(<StringLiteral>node, compilerOptions, host, typeChecker);
|
||||
}
|
||||
else if (isEqualityExpression(node.parent)) {
|
||||
// Get all known external module names or complete a path to a module
|
||||
// i.e. x === '/*completion position'
|
||||
return getStringLiteralCompletionEntriesFromBinaryExpression(<StringLiteral>node, node.parent, typeChecker);
|
||||
}
|
||||
else {
|
||||
const argumentInfo = SignatureHelp.getImmediatelyContainingArgumentInfo(node, position, sourceFile);
|
||||
if (argumentInfo) {
|
||||
|
@ -240,6 +245,18 @@ namespace ts.Completions {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
function getStringLiteralCompletionEntriesFromBinaryExpression(node: StringLiteral, parent: BinaryExpression, typeChecker: TypeChecker): CompletionInfo | undefined {
|
||||
const type = typeChecker.getTypeAtLocation(parent.left === node ? parent.right : parent.left);
|
||||
if (type) {
|
||||
const entries: CompletionEntry[] = [];
|
||||
addStringLiteralCompletionsFromType(type, entries, typeChecker);
|
||||
if (entries.length) {
|
||||
return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries };
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function addStringLiteralCompletionsFromType(type: Type, result: Push<CompletionEntry>, typeChecker: TypeChecker): void {
|
||||
if (type && type.flags & TypeFlags.TypeParameter) {
|
||||
type = typeChecker.getApparentType(type);
|
||||
|
@ -1756,4 +1773,11 @@ namespace ts.Completions {
|
|||
catch (e) {}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function isEqualityExpression(node: Node): node is BinaryExpression {
|
||||
return isBinaryExpression(node) && (node.operatorToken.kind == SyntaxKind.EqualsEqualsToken ||
|
||||
node.operatorToken.kind === SyntaxKind.ExclamationEqualsToken ||
|
||||
node.operatorToken.kind === SyntaxKind.EqualsEqualsEqualsToken ||
|
||||
node.operatorToken.kind === SyntaxKind.ExclamationEqualsEqualsToken);
|
||||
}
|
||||
}
|
||||
|
|
12
tests/cases/fourslash/completionForStringLiteral10.ts
Normal file
12
tests/cases/fourslash/completionForStringLiteral10.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
////type As = 'arf' | 'abacus' | 'abaddon';
|
||||
////let a: As;
|
||||
////if ('/**/' != a
|
||||
|
||||
goTo.marker();
|
||||
verify.completionListContains("arf");
|
||||
verify.completionListContains("abacus");
|
||||
verify.completionListContains("abaddon");
|
||||
verify.completionListCount(3);
|
||||
|
12
tests/cases/fourslash/completionForStringLiteral8.ts
Normal file
12
tests/cases/fourslash/completionForStringLiteral8.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
/// <reference path='fourslash.ts'/>
|
||||
|
||||
////type As = 'arf' | 'abacus' | 'abaddon';
|
||||
////let a: As;
|
||||
////if (a === '/**/
|
||||
|
||||
goTo.marker();
|
||||
verify.completionListContains("arf");
|
||||
verify.completionListContains("abacus");
|
||||
verify.completionListContains("abaddon");
|
||||
verify.completionListCount(3);
|
||||
|
Loading…
Reference in a new issue