diff --git a/src/server/session.ts b/src/server/session.ts index 8d6defa1562..6cd3a7df560 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -665,9 +665,8 @@ namespace ts.server { if (simplifiedResult) { return this.mapDefinitionInfo(definitions, project); } - else { - return definitions; - } + + return definitions.map(Session.mapToOriginalLocation); } private getDefinitionAndBoundSpan(args: protocol.FileLocationRequestArgs, simplifiedResult: boolean): protocol.DefinitionInfoAndBoundSpan | DefinitionInfoAndBoundSpan { @@ -691,13 +690,30 @@ namespace ts.server { }; } - return definitionAndBoundSpan; + return { + ...definitionAndBoundSpan, + definitions: definitionAndBoundSpan.definitions.map(Session.mapToOriginalLocation) + }; } private mapDefinitionInfo(definitions: ReadonlyArray, project: Project): ReadonlyArray { return definitions.map(def => this.toFileSpan(def.fileName, def.textSpan, project)); } + private static mapToOriginalLocation(def: T): T { + if (def.originalFileName) { + Debug.assert(def.originalTextSpan !== undefined, "originalTextSpan should be present if originalFileName is"); + return { + ...def, + fileName: def.originalFileName, + textSpan: def.originalTextSpan, + targetFileName: def.fileName, + targetTextSpan: def.textSpan + }; + } + return def; + } + private toFileSpan(fileName: string, textSpan: TextSpan, project: Project): protocol.FileSpan { const ls = project.getLanguageService(); const start = ls.toLineColumnOffset(fileName, textSpan.start); @@ -732,9 +748,8 @@ namespace ts.server { if (simplifiedResult) { return implementations.map(({ fileName, textSpan }) => this.toFileSpan(fileName, textSpan, project)); } - else { - return implementations; - } + + return implementations.map(Session.mapToOriginalLocation); } private getOccurrences(args: protocol.FileLocationRequestArgs): ReadonlyArray { diff --git a/src/services/services.ts b/src/services/services.ts index 700b07085a0..401576c57b8 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1634,7 +1634,9 @@ namespace ts { textSpan: { start: newLoc.position, length: info.textSpan.length - } + }, + originalFileName: info.fileName, + originalTextSpan: info.textSpan }) ); @@ -1678,7 +1680,9 @@ namespace ts { textSpan: { start: newLoc.position, length: info.textSpan.length - } + }, + originalFileName: info.fileName, + originalTextSpan: info.textSpan }) ); diff --git a/src/services/types.ts b/src/services/types.ts index cf2e58a3712..0d15213cb12 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -542,6 +542,13 @@ namespace ts { export interface DocumentSpan { textSpan: TextSpan; fileName: string; + + /** + * If the span represents a location that was remapped (e.g. via a .d.ts.map file), + * then the original filename and span will be specified here + */ + originalTextSpan?: TextSpan; + originalFileName?: string; } export interface RenameLocation extends DocumentSpan { @@ -654,9 +661,7 @@ namespace ts { indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean; } - export interface DefinitionInfo { - fileName: string; - textSpan: TextSpan; + export interface DefinitionInfo extends DocumentSpan { kind: ScriptElementKind; name: string; containerKind: ScriptElementKind;