Skip to content

Commit dda598a

Browse files
committed
Refactor getDefinition code
1 parent 64f3798 commit dda598a

File tree

1 file changed

+67
-74
lines changed

1 file changed

+67
-74
lines changed

src/services/services.ts

Lines changed: 67 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3460,19 +3460,6 @@ module ts {
34603460
return ScriptElementKind.unknown;
34613461
}
34623462

3463-
function getTypeKind(type: Type): string {
3464-
let flags = type.getFlags();
3465-
3466-
if (flags & TypeFlags.Enum) return ScriptElementKind.enumElement;
3467-
if (flags & TypeFlags.Class) return ScriptElementKind.classElement;
3468-
if (flags & TypeFlags.Interface) return ScriptElementKind.interfaceElement;
3469-
if (flags & TypeFlags.TypeParameter) return ScriptElementKind.typeParameterElement;
3470-
if (flags & TypeFlags.Intrinsic) return ScriptElementKind.primitiveType;
3471-
if (flags & TypeFlags.StringLiteral) return ScriptElementKind.primitiveType;
3472-
3473-
return ScriptElementKind.unknown;
3474-
}
3475-
34763463
function getSymbolModifiers(symbol: Symbol): string {
34773464
return symbol && symbol.declarations && symbol.declarations.length > 0
34783465
? getNodeModifiers(symbol.declarations[0])
@@ -3887,6 +3874,72 @@ module ts {
38873874
};
38883875
}
38893876

3877+
function getDefintionFromSymbol(symbol: Symbol, node: Node): DefinitionInfo[]{
3878+
let typeChecker = program.getTypeChecker();
3879+
let result: DefinitionInfo[] = [];
3880+
let declarations = symbol.getDeclarations();
3881+
let symbolName = typeChecker.symbolToString(symbol); // Do not get scoped name, just the name of the symbol
3882+
let symbolKind = getSymbolKind(symbol, node);
3883+
let containerSymbol = symbol.parent;
3884+
let containerName = containerSymbol ? typeChecker.symbolToString(containerSymbol, node) : "";
3885+
3886+
if (!tryAddConstructSignature(symbol, node, symbolKind, symbolName, containerName, result) &&
3887+
!tryAddCallSignature(symbol, node, symbolKind, symbolName, containerName, result)) {
3888+
// Just add all the declarations.
3889+
forEach(declarations, declaration => {
3890+
result.push(createDefinitionInfo(declaration, symbolKind, symbolName, containerName));
3891+
});
3892+
}
3893+
3894+
return result;
3895+
3896+
function tryAddConstructSignature(symbol: Symbol, location: Node, symbolKind: string, symbolName: string, containerName: string, result: DefinitionInfo[]) {
3897+
// Applicable only if we are in a new expression, or we are on a constructor declaration
3898+
// and in either case the symbol has a construct signature definition, i.e. class
3899+
if (isNewExpressionTarget(location) || location.kind === SyntaxKind.ConstructorKeyword) {
3900+
if (symbol.flags & SymbolFlags.Class) {
3901+
let classDeclaration = <ClassDeclaration>symbol.getDeclarations()[0];
3902+
Debug.assert(classDeclaration && classDeclaration.kind === SyntaxKind.ClassDeclaration);
3903+
3904+
return tryAddSignature(classDeclaration.members, /*selectConstructors*/ true, symbolKind, symbolName, containerName, result);
3905+
}
3906+
}
3907+
return false;
3908+
}
3909+
3910+
function tryAddCallSignature(symbol: Symbol, location: Node, symbolKind: string, symbolName: string, containerName: string, result: DefinitionInfo[]) {
3911+
if (isCallExpressionTarget(location) || isNewExpressionTarget(location) || isNameOfFunctionDeclaration(location)) {
3912+
return tryAddSignature(symbol.declarations, /*selectConstructors*/ false, symbolKind, symbolName, containerName, result);
3913+
}
3914+
return false;
3915+
}
3916+
3917+
function tryAddSignature(signatureDeclarations: Declaration[], selectConstructors: boolean, symbolKind: string, symbolName: string, containerName: string, result: DefinitionInfo[]) {
3918+
let declarations: Declaration[] = [];
3919+
let definition: Declaration;
3920+
3921+
forEach(signatureDeclarations, d => {
3922+
if ((selectConstructors && d.kind === SyntaxKind.Constructor) ||
3923+
(!selectConstructors && (d.kind === SyntaxKind.FunctionDeclaration || d.kind === SyntaxKind.MethodDeclaration || d.kind === SyntaxKind.MethodSignature))) {
3924+
declarations.push(d);
3925+
if ((<FunctionLikeDeclaration>d).body) definition = d;
3926+
}
3927+
});
3928+
3929+
if (definition) {
3930+
result.push(createDefinitionInfo(definition, symbolKind, symbolName, containerName));
3931+
return true;
3932+
}
3933+
else if (declarations.length) {
3934+
result.push(createDefinitionInfo(declarations[declarations.length - 1], symbolKind, symbolName, containerName));
3935+
return true;
3936+
}
3937+
3938+
return false;
3939+
}
3940+
3941+
}
3942+
38903943
/// Goto definition
38913944
function getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[] {
38923945
synchronizeHostData();
@@ -3961,67 +4014,7 @@ module ts {
39614014
declaration => createDefinitionInfo(declaration, shorthandSymbolKind, shorthandSymbolName, shorthandContainerName));
39624015
}
39634016

3964-
let result: DefinitionInfo[] = [];
3965-
let declarations = symbol.getDeclarations();
3966-
let symbolName = typeChecker.symbolToString(symbol); // Do not get scoped name, just the name of the symbol
3967-
let symbolKind = getSymbolKind(symbol, node);
3968-
let containerSymbol = symbol.parent;
3969-
let containerName = containerSymbol ? typeChecker.symbolToString(containerSymbol, node) : "";
3970-
3971-
if (!tryAddConstructSignature(symbol, node, symbolKind, symbolName, containerName, result) &&
3972-
!tryAddCallSignature(symbol, node, symbolKind, symbolName, containerName, result)) {
3973-
// Just add all the declarations.
3974-
forEach(declarations, declaration => {
3975-
result.push(createDefinitionInfo(declaration, symbolKind, symbolName, containerName));
3976-
});
3977-
}
3978-
3979-
return result;
3980-
3981-
function tryAddConstructSignature(symbol: Symbol, location: Node, symbolKind: string, symbolName: string, containerName: string, result: DefinitionInfo[]) {
3982-
// Applicable only if we are in a new expression, or we are on a constructor declaration
3983-
// and in either case the symbol has a construct signature definition, i.e. class
3984-
if (isNewExpressionTarget(location) || location.kind === SyntaxKind.ConstructorKeyword) {
3985-
if (symbol.flags & SymbolFlags.Class) {
3986-
let classDeclaration = <ClassDeclaration>symbol.getDeclarations()[0];
3987-
Debug.assert(classDeclaration && classDeclaration.kind === SyntaxKind.ClassDeclaration);
3988-
3989-
return tryAddSignature(classDeclaration.members, /*selectConstructors*/ true, symbolKind, symbolName, containerName, result);
3990-
}
3991-
}
3992-
return false;
3993-
}
3994-
3995-
function tryAddCallSignature(symbol: Symbol, location: Node, symbolKind: string, symbolName: string, containerName: string, result: DefinitionInfo[]) {
3996-
if (isCallExpressionTarget(location) || isNewExpressionTarget(location) || isNameOfFunctionDeclaration(location)) {
3997-
return tryAddSignature(symbol.declarations, /*selectConstructors*/ false, symbolKind, symbolName, containerName, result);
3998-
}
3999-
return false;
4000-
}
4001-
4002-
function tryAddSignature(signatureDeclarations: Declaration[], selectConstructors: boolean, symbolKind: string, symbolName: string, containerName: string, result: DefinitionInfo[]) {
4003-
let declarations: Declaration[] = [];
4004-
let definition: Declaration;
4005-
4006-
forEach(signatureDeclarations, d => {
4007-
if ((selectConstructors && d.kind === SyntaxKind.Constructor) ||
4008-
(!selectConstructors && (d.kind === SyntaxKind.FunctionDeclaration || d.kind === SyntaxKind.MethodDeclaration || d.kind === SyntaxKind.MethodSignature))) {
4009-
declarations.push(d);
4010-
if ((<FunctionLikeDeclaration>d).body) definition = d;
4011-
}
4012-
});
4013-
4014-
if (definition) {
4015-
result.push(createDefinitionInfo(definition, symbolKind, symbolName, containerName));
4016-
return true;
4017-
}
4018-
else if (declarations.length) {
4019-
result.push(createDefinitionInfo(declarations[declarations.length - 1], symbolKind, symbolName, containerName));
4020-
return true;
4021-
}
4022-
4023-
return false;
4024-
}
4017+
return getDefintionFromSymbol(symbol, node);
40254018
}
40264019

40274020
function getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[] {

0 commit comments

Comments
 (0)