Skip to content

Commit 5258167

Browse files
Refactor completion code for object literals/bindings and import clauses.
1 parent 03444a4 commit 5258167

File tree

1 file changed

+64
-50
lines changed

1 file changed

+64
-50
lines changed

src/services/services.ts

+64-50
Original file line numberDiff line numberDiff line change
@@ -3010,59 +3010,21 @@ namespace ts {
30103010
}
30113011

30123012
function tryGetGlobalSymbols(): boolean {
3013-
let objectLikeContainer = tryGetObjectLikeCompletionContainer(contextToken);
3014-
let jsxContainer = tryGetContainingJsxElement(contextToken);
3015-
if (objectLikeContainer) {
3016-
// Object literal expression, look up possible property names from contextual type
3017-
isMemberCompletion = true;
3018-
isNewIdentifierLocation = true;
3013+
let objectLikeContainer: ObjectLiteralExpression | BindingPattern;
3014+
let importClause: ImportClause;
3015+
let jsxContainer: JsxOpeningLikeElement;
30193016

3020-
let typeForObject: Type;
3021-
let existingMembers: Declaration[];
3022-
3023-
if (objectLikeContainer.kind === SyntaxKind.ObjectLiteralExpression) {
3024-
typeForObject = typeChecker.getContextualType(<ObjectLiteralExpression>objectLikeContainer);
3025-
existingMembers = (<ObjectLiteralExpression>objectLikeContainer).properties;
3026-
}
3027-
else {
3028-
typeForObject = typeChecker.getTypeAtLocation(objectLikeContainer);
3029-
existingMembers = (<BindingPattern>objectLikeContainer).elements;
3030-
}
3031-
3032-
if (!typeForObject) {
3033-
return false;
3034-
}
3035-
3036-
let typeMembers = typeChecker.getPropertiesOfType(typeForObject);
3037-
if (typeMembers && typeMembers.length > 0) {
3038-
// Add filtered items to the completion list
3039-
symbols = filterObjectMembersList(typeMembers, existingMembers);
3040-
}
3041-
return true;
3017+
if (objectLikeContainer = tryGetObjectLikeCompletionContainer(contextToken)) {
3018+
return tryGetObjectLikeCompletionSymbols(objectLikeContainer);
30423019
}
3043-
else if (getAncestor(contextToken, SyntaxKind.ImportClause)) {
3044-
// cursor is in import clause
3045-
// try to show exported member for imported module
3046-
isMemberCompletion = true;
3047-
isNewIdentifierLocation = true;
3048-
if (showCompletionsInImportsClause(contextToken)) {
3049-
let importDeclaration = <ImportDeclaration>getAncestor(contextToken, SyntaxKind.ImportDeclaration);
3050-
Debug.assert(importDeclaration !== undefined);
3051-
3052-
let exports: Symbol[];
3053-
if (importDeclaration.moduleSpecifier) {
3054-
let moduleSpecifierSymbol = typeChecker.getSymbolAtLocation(importDeclaration.moduleSpecifier);
3055-
if (moduleSpecifierSymbol) {
3056-
exports = typeChecker.getExportsOfModule(moduleSpecifierSymbol);
3057-
}
3058-
}
30593020

3060-
//let exports = typeInfoResolver.getExportsOfImportDeclaration(importDeclaration);
3061-
symbols = exports ? filterModuleExports(exports, importDeclaration) : emptyArray;
3062-
}
3063-
return true;
3021+
if (importClause = <ImportClause>getAncestor(contextToken, SyntaxKind.ImportClause)) {
3022+
// cursor is in an import clause
3023+
// try to show exported member for imported module
3024+
return tryGetImportClauseCompletionSymbols(importClause);
30643025
}
3065-
else if (jsxContainer) {
3026+
3027+
if (jsxContainer = tryGetContainingJsxElement(contextToken)) {
30663028
let attrsType: Type;
30673029
if ((jsxContainer.kind === SyntaxKind.JsxSelfClosingElement) || (jsxContainer.kind === SyntaxKind.JsxOpeningElement)) {
30683030
// Cursor is inside a JSX self-closing element or opening element
@@ -3144,7 +3106,7 @@ namespace ts {
31443106
return result;
31453107
}
31463108

3147-
function showCompletionsInImportsClause(node: Node): boolean {
3109+
function shouldShowCompletionsInImportsClause(node: Node): boolean {
31483110
if (node) {
31493111
// import {|
31503112
// import {a,|
@@ -3242,6 +3204,58 @@ namespace ts {
32423204
return false;
32433205
}
32443206

3207+
function tryGetObjectLikeCompletionSymbols(objectLikeContainer: ObjectLiteralExpression | BindingPattern): boolean {
3208+
// Object literal expression, look up possible property names from contextual type
3209+
isMemberCompletion = true;
3210+
isNewIdentifierLocation = true;
3211+
3212+
let typeForObject: Type;
3213+
let existingMembers: Declaration[];
3214+
3215+
if (objectLikeContainer.kind === SyntaxKind.ObjectLiteralExpression) {
3216+
typeForObject = typeChecker.getContextualType(<ObjectLiteralExpression>objectLikeContainer);
3217+
existingMembers = (<ObjectLiteralExpression>objectLikeContainer).properties;
3218+
}
3219+
else {
3220+
typeForObject = typeChecker.getTypeAtLocation(objectLikeContainer);
3221+
existingMembers = (<BindingPattern>objectLikeContainer).elements;
3222+
}
3223+
3224+
if (!typeForObject) {
3225+
return false;
3226+
}
3227+
3228+
let typeMembers = typeChecker.getPropertiesOfType(typeForObject);
3229+
if (typeMembers && typeMembers.length > 0) {
3230+
// Add filtered items to the completion list
3231+
symbols = filterObjectMembersList(typeMembers, existingMembers);
3232+
}
3233+
return true;
3234+
}
3235+
3236+
function tryGetImportClauseCompletionSymbols(importClause: ImportClause): boolean {
3237+
// cursor is in import clause
3238+
// try to show exported member for imported module
3239+
isMemberCompletion = true;
3240+
isNewIdentifierLocation = true;
3241+
if (shouldShowCompletionsInImportsClause(contextToken)) {
3242+
let importDeclaration = <ImportDeclaration>getAncestor(contextToken, SyntaxKind.ImportDeclaration);
3243+
Debug.assert(importDeclaration !== undefined);
3244+
3245+
let exports: Symbol[];
3246+
if (importDeclaration.moduleSpecifier) {
3247+
let moduleSpecifierSymbol = typeChecker.getSymbolAtLocation(importDeclaration.moduleSpecifier);
3248+
if (moduleSpecifierSymbol) {
3249+
exports = typeChecker.getExportsOfModule(moduleSpecifierSymbol);
3250+
}
3251+
}
3252+
3253+
//let exports = typeInfoResolver.getExportsOfImportDeclaration(importDeclaration);
3254+
symbols = exports ? filterModuleExports(exports, importDeclaration) : emptyArray;
3255+
}
3256+
return true;
3257+
}
3258+
32453259
/**
32463260
* Returns the immediate owning object literal or binding pattern of a context token,
32473261
* on the condition that one exists and that the context implies completion should be given.

0 commit comments

Comments
 (0)