Skip to content

Commit 8004fec

Browse files
author
Armando Aguirre
committed
Addressed PR comments: added simplified/full version, changed design
1 parent b86153d commit 8004fec

8 files changed

Lines changed: 75 additions & 32 deletions

File tree

‎src/harness/harnessLanguageService.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ namespace Harness.LanguageService {
432432
getDefinitionAtPosition(fileName: string, position: number): ts.DefinitionInfo[] {
433433
return unwrapJSONCallResult(this.shim.getDefinitionAtPosition(fileName, position));
434434
}
435-
getDefinitionAndBoundSpan(): ts.DefinitionInfoAndBoundSpan {
436-
throw new Error("Not supported on the shim.");
435+
getDefinitionAndBoundSpan(fileName: string, position: number): ts.DefinitionInfoAndBoundSpan {
436+
return unwrapJSONCallResult(this.shim.getDefinitionAndBoundSpan(fileName, position));
437437
}
438438
getTypeDefinitionAtPosition(fileName: string, position: number): ts.DefinitionInfo[] {
439439
return unwrapJSONCallResult(this.shim.getTypeDefinitionAtPosition(fileName, position));

‎src/server/client.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,23 @@ namespace ts.server {
268268
}));
269269
}
270270

271-
getDefinitionAndBoundSpan(_fileName: string, _position: number): DefinitionInfoAndBoundSpan {
272-
return notImplemented();
271+
getDefinitionAndBoundSpan(fileName: string, position: number): DefinitionInfoAndBoundSpan {
272+
const args: protocol.FileLocationRequestArgs = this.createFileLocationRequestArgs(fileName, position);
273+
274+
const request = this.processRequest<protocol.DefinitionRequest>(CommandNames.DefinitionAndBoundSpan, args);
275+
const response = this.processResponse<protocol.DefinitionInfoAndBoundSpanReponse>(request);
276+
277+
return {
278+
definitions: response.body.definitions.map(entry => ({
279+
containerKind: ScriptElementKind.unknown,
280+
containerName: "",
281+
fileName: entry.file,
282+
textSpan: this.decodeSpan(entry),
283+
kind: ScriptElementKind.unknown,
284+
name: ""
285+
})),
286+
textSpan: this.decodeSpan(response.body.textSpan, request.arguments.file)
287+
};
273288
}
274289

275290
getTypeDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[] {

‎src/server/protocol.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,10 @@ namespace ts.server.protocol {
703703
body?: FileSpan[];
704704
}
705705

706+
export interface DefinitionInfoAndBoundSpanReponse extends Response {
707+
body?: DefinitionInfoAndBoundSpan;
708+
}
709+
706710
/**
707711
* Definition response message. Gives text range for definition.
708712
*/

‎src/server/session.ts‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ namespace ts.server {
601601
}
602602

603603
if (simplifiedResult) {
604-
return this.getSimplifiedDefinition(definitions, project);
604+
return this.getSimplifiedDefinitions(definitions, project);
605605
}
606606
else {
607607
return definitions;
@@ -624,28 +624,29 @@ namespace ts.server {
624624

625625
if (simplifiedResult) {
626626
return {
627-
definitions: this.getSimplifiedDefinition(definitionAndBoundSpan.definitions, project),
628-
textSpan: this.getSimplifiedTextSpan(definitionAndBoundSpan.textSpan, scriptInfo)
627+
definitions: this.getSimplifiedDefinitions(definitionAndBoundSpan.definitions, project),
628+
textSpan: this.getSimplifiedTextSpan(scriptInfo, definitionAndBoundSpan.textSpan)
629629
};
630630
}
631631

632632
return definitionAndBoundSpan;
633633
}
634634

635-
private getSimplifiedDefinition(definitions: ReadonlyArray<DefinitionInfo>, project: Project): ReadonlyArray<protocol.FileSpan> {
636-
return definitions.map(def => {
637-
const defScriptInfo = project.getScriptInfo(def.fileName);
638-
const simplifiedTextSpan = this.getSimplifiedTextSpan(def.textSpan, defScriptInfo);
635+
private getSimplifiedDefinitions(definitions: ReadonlyArray<DefinitionInfo>, project: Project): ReadonlyArray<protocol.FileSpan> {
636+
return definitions.map(def => this.getSimplifiedFileSpan(def.fileName, def.textSpan, project));
637+
}
639638

640-
return {
641-
file: def.fileName,
642-
start: simplifiedTextSpan.start,
643-
end: simplifiedTextSpan.end
644-
};
645-
});
639+
private getSimplifiedFileSpan(fileName: string, textSpan: TextSpan, project: Project): protocol.FileSpan {
640+
const scriptInfo = project.getScriptInfo(fileName);
641+
const simplifiedTextSpan = this.getSimplifiedTextSpan(scriptInfo, textSpan);
642+
643+
return {
644+
file: fileName,
645+
...simplifiedTextSpan
646+
};
646647
}
647648

648-
private getSimplifiedTextSpan(textSpan: TextSpan, scriptInfo: ScriptInfo): protocol.TextSpan {
649+
private getSimplifiedTextSpan(scriptInfo: ScriptInfo, textSpan: TextSpan): protocol.TextSpan {
649650
return {
650651
start: scriptInfo.positionToLineOffset(textSpan.start),
651652
end: scriptInfo.positionToLineOffset(textSpanEnd(textSpan))

‎src/services/goToDefinition.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace ts.GoToDefinition {
8888
// }
8989
// bar<Test>(({pr/*goto*/op1})=>{});
9090
if (isPropertyName(node) && isBindingElement(node.parent) && isObjectBindingPattern(node.parent.parent) &&
91-
(node === (node.parent.propertyName || node.parent.name))) {
91+
(node === (node.parent.propertyName || node.parent.name))) {
9292
const type = typeChecker.getTypeAtLocation(node.parent.parent);
9393
if (type) {
9494
const propSymbols = getPropertySymbolsFromType(type, node);
@@ -149,6 +149,21 @@ namespace ts.GoToDefinition {
149149
return getDefinitionFromSymbol(typeChecker, type.symbol, node);
150150
}
151151

152+
export function getDefinitionAndBoundSpan(program: Program, sourceFile: SourceFile, position: number): DefinitionInfoAndBoundSpan {
153+
const definitions = getDefinitionAtPosition(program, sourceFile, position);
154+
155+
if (!definitions || definitions.length === 0) {
156+
return undefined;
157+
}
158+
159+
// TODO: Add textSpan for triple slash references (file and type).
160+
161+
const node = getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true);
162+
const textSpan = createTextSpan(node.getStart(), node.getWidth());
163+
164+
return { definitions, textSpan };
165+
}
166+
152167
// Go to the original declaration for cases:
153168
//
154169
// (1) when the aliased symbol was declared in the location(parent).

‎src/services/services.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,17 +1412,8 @@ namespace ts {
14121412
}
14131413

14141414
function getDefinitionAndBoundSpan(fileName: string, position: number): DefinitionInfoAndBoundSpan {
1415-
const definitions = getDefinitionAtPosition(fileName, position);
1416-
1417-
if (!definitions) {
1418-
return undefined;
1419-
}
1420-
1421-
const sourceFile = getValidSourceFile(fileName);
1422-
const node = getTouchingPropertyName(sourceFile, position, /*includeJsDocComment*/ true);
1423-
const textSpan = createTextSpan(node.getStart(), node.getWidth());
1424-
1425-
return { definitions, textSpan };
1415+
synchronizeHostData();
1416+
return GoToDefinition.getDefinitionAndBoundSpan(program, getValidSourceFile(fileName), position);
14261417
}
14271418

14281419
function getTypeDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[] {

‎src/services/shims.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ namespace ts {
170170
*/
171171
getDefinitionAtPosition(fileName: string, position: number): string;
172172

173+
getDefinitionAndBoundSpan(fileName: string, position: number): string;
174+
173175
/**
174176
* Returns a JSON-encoded value of the type:
175177
* { fileName: string; textSpan: { start: number; length: number}; kind: string; name: string; containerKind: string; containerName: string }
@@ -772,6 +774,17 @@ namespace ts {
772774
);
773775
}
774776

777+
/**
778+
* Computes the definition location and file for the symbol
779+
* at the requested position.
780+
*/
781+
public getDefinitionAndBoundSpan(fileName: string, position: number): string {
782+
return this.forwardJSONCall(
783+
`getDefinitionAndBoundSpan('${fileName}', ${position})`,
784+
() => this.languageService.getDefinitionAndBoundSpan(fileName, position)
785+
);
786+
}
787+
775788
/// GOTO Type
776789

777790
/**

‎tests/baselines/reference/api/tsserverlibrary.d.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5313,6 +5313,9 @@ declare namespace ts.server.protocol {
53135313
interface DefinitionResponse extends Response {
53145314
body?: FileSpan[];
53155315
}
5316+
interface DefinitionInfoAndBoundSpanReponse extends Response {
5317+
body?: DefinitionInfoAndBoundSpan;
5318+
}
53165319
/**
53175320
* Definition response message. Gives text range for definition.
53185321
*/
@@ -6865,8 +6868,9 @@ declare namespace ts.server {
68656868
private getDiagnosticsWorker(args, isSemantic, selector, includeLinePosition);
68666869
private getDefinition(args, simplifiedResult);
68676870
private getDefinitionAndBoundSpan(args, simplifiedResult);
6868-
private getSimplifiedDefinition(definitions, project);
6869-
private getSimplifiedTextSpan(textSpan, scriptInfo);
6871+
private getSimplifiedDefinitions(definitions, project);
6872+
private getSimplifiedFileSpan(fileName, textSpan, project);
6873+
private getSimplifiedTextSpan(scriptInfo, textSpan);
68706874
private getTypeDefinition(args);
68716875
private getImplementation(args, simplifiedResult);
68726876
private getOccurrences(args);

0 commit comments

Comments
 (0)