Skip to content

Commit 9412a6d

Browse files
Improve display of symbol definitions.
1 parent cbeeb51 commit 9412a6d

File tree

1 file changed

+78
-57
lines changed

1 file changed

+78
-57
lines changed

src/services/services.ts

Lines changed: 78 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3455,7 +3455,49 @@ module ts {
34553455
};
34563456
}
34573457

3458-
function getDefinitionInfos(node: Node, symbol: Symbol): DefinitionInfo[] {
3458+
/// Goto definition
3459+
function getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[] {
3460+
synchronizeHostData();
3461+
3462+
let sourceFile = getValidSourceFile(fileName);
3463+
3464+
let node = getTouchingPropertyName(sourceFile, position);
3465+
if (!node) {
3466+
return undefined;
3467+
}
3468+
3469+
// Labels
3470+
if (isJumpStatementTarget(node)) {
3471+
let labelName = (<Identifier>node).text;
3472+
let label = getTargetLabel((<BreakOrContinueStatement>node.parent), (<Identifier>node).text);
3473+
return label ? [createDefinitionInfo(label, ScriptElementKind.label, labelName, /*containerName*/ undefined)] : undefined;
3474+
}
3475+
3476+
/// Triple slash reference comments
3477+
let comment = forEach(sourceFile.referencedFiles, r => (r.pos <= position && position < r.end) ? r : undefined);
3478+
if (comment) {
3479+
let referenceFile = tryResolveScriptReference(program, sourceFile, comment);
3480+
if (referenceFile) {
3481+
return [{
3482+
fileName: referenceFile.fileName,
3483+
textSpan: createTextSpanFromBounds(0, 0),
3484+
kind: ScriptElementKind.scriptElement,
3485+
name: comment.fileName,
3486+
containerName: undefined,
3487+
containerKind: undefined
3488+
}];
3489+
}
3490+
return undefined;
3491+
}
3492+
3493+
let symbol = typeInfoResolver.getSymbolAtLocation(node);
3494+
3495+
// Could not find a symbol e.g. node is string or number keyword,
3496+
// or the symbol was an internal symbol and does not have a declaration e.g. undefined symbol
3497+
if (!symbol) {
3498+
return undefined;
3499+
}
3500+
34593501
// If this is an alias, and the request came at the declaration location
34603502
// get the aliased symbol instead. This allows for goto def on an import e.g.
34613503
// import {A, B} from "mod";
@@ -3549,52 +3591,6 @@ module ts {
35493591
}
35503592
}
35513593

3552-
/// Goto definition
3553-
function getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[] {
3554-
synchronizeHostData();
3555-
3556-
let sourceFile = getValidSourceFile(fileName);
3557-
3558-
let node = getTouchingPropertyName(sourceFile, position);
3559-
if (!node) {
3560-
return undefined;
3561-
}
3562-
3563-
// Labels
3564-
if (isJumpStatementTarget(node)) {
3565-
let labelName = (<Identifier>node).text;
3566-
let label = getTargetLabel((<BreakOrContinueStatement>node.parent), (<Identifier>node).text);
3567-
return label ? [createDefinitionInfo(label, ScriptElementKind.label, labelName, /*containerName*/ undefined)] : undefined;
3568-
}
3569-
3570-
/// Triple slash reference comments
3571-
let comment = forEach(sourceFile.referencedFiles, r => (r.pos <= position && position < r.end) ? r : undefined);
3572-
if (comment) {
3573-
let referenceFile = tryResolveScriptReference(program, sourceFile, comment);
3574-
if (referenceFile) {
3575-
return [{
3576-
fileName: referenceFile.fileName,
3577-
textSpan: createTextSpanFromBounds(0, 0),
3578-
kind: ScriptElementKind.scriptElement,
3579-
name: comment.fileName,
3580-
containerName: undefined,
3581-
containerKind: undefined
3582-
}];
3583-
}
3584-
return undefined;
3585-
}
3586-
3587-
let symbol = typeInfoResolver.getSymbolAtLocation(node);
3588-
3589-
// Could not find a symbol e.g. node is string or number keyword,
3590-
// or the symbol was an internal symbol and does not have a declaration e.g. undefined symbol
3591-
if (!symbol) {
3592-
return undefined;
3593-
}
3594-
3595-
return getDefinitionInfos(node, symbol);
3596-
}
3597-
35983594
/// References and Occurrences
35993595
function getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[] {
36003596
synchronizeHostData();
@@ -4149,20 +4145,23 @@ module ts {
41494145
}
41504146

41514147
function findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[]{
4152-
var referenceSymbols = findReferencesWorker(fileName, position, findInStrings, findInComments);
4153-
return convertReferences(referenceSymbols);
4148+
var referencedSymbols = findReferencedSymbols(fileName, position, findInStrings, findInComments);
4149+
return convertReferences(referencedSymbols);
41544150
}
41554151

41564152
function getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[] {
4157-
var referenceSymbols = findReferencesWorker(fileName, position, /*findInStrings:*/ false, /*findInComments:*/ false);
4158-
return convertReferences(referenceSymbols);
4153+
var referencedSymbols = findReferencedSymbols(fileName, position, /*findInStrings:*/ false, /*findInComments:*/ false);
4154+
return convertReferences(referencedSymbols);
41594155
}
41604156

4161-
function findReferences(fileName: string, position: number): ReferencedSymbol[] {
4162-
return filter(findReferencesWorker(fileName, position, /*findInStrings:*/ false, /*findInComments:*/ false), rs => !!rs.definition);
4157+
function findReferences(fileName: string, position: number): ReferencedSymbol[]{
4158+
var referencedSymbols = findReferencedSymbols(fileName, position, /*findInStrings:*/ false, /*findInComments:*/ false);
4159+
4160+
// Only include referenced symbols that have a valid definition.
4161+
return filter(referencedSymbols, rs => !!rs.definition);
41634162
}
41644163

4165-
function findReferencesWorker(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): ReferencedSymbol[] {
4164+
function findReferencedSymbols(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): ReferencedSymbol[] {
41664165
synchronizeHostData();
41674166

41684167
let sourceFile = getValidSourceFile(fileName);
@@ -4265,6 +4264,24 @@ module ts {
42654264

42664265
return result;
42674266

4267+
function getDefinition(symbol: Symbol): DefinitionInfo {
4268+
let info = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, node.getSourceFile(), getContainerNode(node), typeInfoResolver, node);
4269+
let name = map(info.displayParts, p => p.text).join("");
4270+
let declarations = symbol.declarations;
4271+
if (!declarations || declarations.length === 0) {
4272+
return undefined;
4273+
}
4274+
4275+
return {
4276+
containerKind: "",
4277+
containerName: "",
4278+
name,
4279+
kind: info.symbolKind,
4280+
fileName: declarations[0].getSourceFile().fileName,
4281+
textSpan: createTextSpan(declarations[0].getStart(), 0)
4282+
};
4283+
}
4284+
42684285
function isImportOrExportSpecifierName(location: Node): boolean {
42694286
return location.parent &&
42704287
(location.parent.kind === SyntaxKind.ImportSpecifier || location.parent.kind === SyntaxKind.ExportSpecifier) &&
@@ -4513,6 +4530,11 @@ module ts {
45134530
// for.
45144531
if ((findInStrings && isInString(position)) ||
45154532
(findInComments && isInComment(position))) {
4533+
4534+
// In the case where we're looking inside comments/strings, we don't have
4535+
// an actual definition. So just use 'undefined' here. Features like
4536+
// 'Rename' won't care (as they ignore the definitions), and features like
4537+
// 'FindReferences' will just filter out these results.
45164538
result.push({
45174539
definition: undefined,
45184540
references: [{
@@ -4563,7 +4585,7 @@ module ts {
45634585
symbolToIndex[symbolId] = index;
45644586

45654587
result.push({
4566-
definition: getDefinitionInfos(node, symbol)[0],
4588+
definition: getDefinition(symbol),
45674589
references: []
45684590
});
45694591
}
@@ -4644,8 +4666,7 @@ module ts {
46444666
}
46454667
});
46464668

4647-
var definitions = getDefinitionInfos(superKeyword, searchSpaceNode.symbol);
4648-
var definition = definitions[0];
4669+
var definition = getDefinition(searchSpaceNode.symbol);
46494670
return [{ definition, references }];
46504671
}
46514672

0 commit comments

Comments
 (0)