Skip to content

Commit b86153d

Browse files
author
Armando Aguirre
committed
Changed command designed based on review input
1 parent c6a8a32 commit b86153d

9 files changed

Lines changed: 103 additions & 44 deletions

File tree

src/harness/harnessLanguageService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ 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.");
437+
}
435438
getTypeDefinitionAtPosition(fileName: string, position: number): ts.DefinitionInfo[] {
436439
return unwrapJSONCallResult(this.shim.getTypeDefinitionAtPosition(fileName, position));
437440
}
@@ -490,9 +493,6 @@ namespace Harness.LanguageService {
490493
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): ts.TextSpan {
491494
return unwrapJSONCallResult(this.shim.getSpanOfEnclosingComment(fileName, position, onlyMultiLine));
492495
}
493-
getSpanForPosition(): ts.TextSpan {
494-
throw new Error("Not supportred on the shim.");
495-
}
496496
getCodeFixesAtPosition(): ts.CodeAction[] {
497497
throw new Error("Not supported on the shim.");
498498
}

src/harness/unittests/session.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ namespace ts.server {
176176
CommandNames.Definition,
177177
CommandNames.DefinitionFull,
178178
CommandNames.DefinitionAndBoundSpan,
179+
CommandNames.DefinitionAndBoundSpanFull,
179180
CommandNames.Implementation,
180181
CommandNames.ImplementationFull,
181182
CommandNames.Exit,

src/server/client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ namespace ts.server {
268268
}));
269269
}
270270

271+
getDefinitionAndBoundSpan(_fileName: string, _position: number): DefinitionInfoAndBoundSpan {
272+
return notImplemented();
273+
}
274+
271275
getTypeDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[] {
272276
const args: protocol.FileLocationRequestArgs = this.createFileLocationRequestArgs(fileName, position);
273277

@@ -531,10 +535,6 @@ namespace ts.server {
531535
return notImplemented();
532536
}
533537

534-
getSpanForPosition(_fileName: string, _position: number): TextSpan {
535-
return notImplemented();
536-
}
537-
538538
getCodeFixesAtPosition(file: string, start: number, end: number, errorCodes: number[]): CodeAction[] {
539539
const args: protocol.CodeFixRequestArgs = { ...this.createFileRangeRequestArgs(file, start, end), errorCodes };
540540

src/server/protocol.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ namespace ts.server.protocol {
2121
Definition = "definition",
2222
/* @internal */
2323
DefinitionFull = "definition-full",
24-
/* @internal */
2524
DefinitionAndBoundSpan = "definitionAndBoundSpan",
25+
/* @internal */
26+
DefinitionAndBoundSpanFull = "definitionAndBoundSpan-full",
2627
Implementation = "implementation",
2728
/* @internal */
2829
ImplementationFull = "implementation-full",
@@ -690,6 +691,11 @@ namespace ts.server.protocol {
690691
file: string;
691692
}
692693

694+
export interface DefinitionInfoAndBoundSpan {
695+
definitions: ReadonlyArray<FileSpan>;
696+
textSpan: TextSpan;
697+
}
698+
693699
/**
694700
* Definition response message. Gives text range for definition.
695701
*/

src/server/session.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -601,20 +601,57 @@ namespace ts.server {
601601
}
602602

603603
if (simplifiedResult) {
604-
return definitions.map(def => {
605-
const defScriptInfo = project.getScriptInfo(def.fileName);
606-
return {
607-
file: def.fileName,
608-
start: defScriptInfo.positionToLineOffset(def.textSpan.start),
609-
end: defScriptInfo.positionToLineOffset(textSpanEnd(def.textSpan))
610-
};
611-
});
604+
return this.getSimplifiedDefinition(definitions, project);
612605
}
613606
else {
614607
return definitions;
615608
}
616609
}
617610

611+
private getDefinitionAndBoundSpan(args: protocol.FileLocationRequestArgs, simplifiedResult: boolean): protocol.DefinitionInfoAndBoundSpan | DefinitionInfoAndBoundSpan {
612+
const { file, project } = this.getFileAndProject(args);
613+
const position = this.getPositionInFile(args, file);
614+
const scriptInfo = project.getScriptInfo(file);
615+
616+
const definitionAndBoundSpan = project.getLanguageService().getDefinitionAndBoundSpan(file, position);
617+
618+
if (!definitionAndBoundSpan || !definitionAndBoundSpan.definitions) {
619+
return {
620+
definitions: emptyArray,
621+
textSpan: undefined
622+
};
623+
}
624+
625+
if (simplifiedResult) {
626+
return {
627+
definitions: this.getSimplifiedDefinition(definitionAndBoundSpan.definitions, project),
628+
textSpan: this.getSimplifiedTextSpan(definitionAndBoundSpan.textSpan, scriptInfo)
629+
};
630+
}
631+
632+
return definitionAndBoundSpan;
633+
}
634+
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);
639+
640+
return {
641+
file: def.fileName,
642+
start: simplifiedTextSpan.start,
643+
end: simplifiedTextSpan.end
644+
};
645+
});
646+
}
647+
648+
private getSimplifiedTextSpan(textSpan: TextSpan, scriptInfo: ScriptInfo): protocol.TextSpan {
649+
return {
650+
start: scriptInfo.positionToLineOffset(textSpan.start),
651+
end: scriptInfo.positionToLineOffset(textSpanEnd(textSpan))
652+
};
653+
}
654+
618655
private getTypeDefinition(args: protocol.FileLocationRequestArgs): ReadonlyArray<protocol.FileSpan> {
619656
const { file, project } = this.getFileAndProject(args);
620657
const position = this.getPositionInFile(args, file);
@@ -1081,13 +1118,6 @@ namespace ts.server {
10811118
}
10821119
}
10831120

1084-
private getSpanForLocation(args: protocol.FileLocationRequestArgs): TextSpan {
1085-
const { file, project } = this.getFileAndProject(args);
1086-
const scriptInfo = project.getScriptInfoForNormalizedPath(file);
1087-
1088-
return project.getLanguageService().getSpanForPosition(file, this.getPosition(args, scriptInfo));
1089-
}
1090-
10911121
private getFormattingEditsForRange(args: protocol.FormatRequestArgs): protocol.CodeEdit[] {
10921122
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
10931123
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file);
@@ -1715,13 +1745,10 @@ namespace ts.server {
17151745
return this.requiredResponse(this.getDefinition(request.arguments, /*simplifiedResult*/ false));
17161746
},
17171747
[CommandNames.DefinitionAndBoundSpan]: (request: protocol.DefinitionRequest) => {
1718-
const definitions = this.getDefinition(request.arguments, /*simplifiedResult*/ false);
1719-
const textSpan = definitions.length !== 0 ? this.getSpanForLocation(request.arguments) : {};
1720-
1721-
return this.requiredResponse({
1722-
definitions,
1723-
textSpan
1724-
});
1748+
return this.requiredResponse(this.getDefinitionAndBoundSpan(request.arguments, /*simplifiedResult*/ true));
1749+
},
1750+
[CommandNames.DefinitionAndBoundSpanFull]: (request: protocol.DefinitionRequest) => {
1751+
return this.requiredResponse(this.getDefinitionAndBoundSpan(request.arguments, /*simplifiedResult*/ false));
17251752
},
17261753
[CommandNames.TypeDefinition]: (request: protocol.FileLocationRequest) => {
17271754
return this.requiredResponse(this.getTypeDefinition(request.arguments));

src/services/services.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,20 @@ namespace ts {
14111411
return GoToDefinition.getDefinitionAtPosition(program, getValidSourceFile(fileName), position);
14121412
}
14131413

1414+
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 };
1426+
}
1427+
14141428
function getTypeDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[] {
14151429
synchronizeHostData();
14161430
return GoToDefinition.getTypeDefinitionAtPosition(program.getTypeChecker(), getValidSourceFile(fileName), position);
@@ -1807,15 +1821,6 @@ namespace ts {
18071821
return range && createTextSpanFromRange(range);
18081822
}
18091823

1810-
function getSpanForPosition(fileName: string, position: number): TextSpan {
1811-
synchronizeHostData();
1812-
1813-
const sourceFile = getValidSourceFile(fileName);
1814-
const node = getTouchingPropertyName(sourceFile, position, /*includeJsDocCcomment*/ false);
1815-
1816-
return createTextSpan(node.getStart(), node.getWidth());
1817-
}
1818-
18191824
function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {
18201825
// Note: while getting todo comments seems like a syntactic operation, we actually
18211826
// treat it as a semantic operation here. This is because we expect our host to call
@@ -2018,6 +2023,7 @@ namespace ts {
20182023
getSignatureHelpItems,
20192024
getQuickInfoAtPosition,
20202025
getDefinitionAtPosition,
2026+
getDefinitionAndBoundSpan,
20212027
getImplementationAtPosition,
20222028
getTypeDefinitionAtPosition,
20232029
getReferencesAtPosition,
@@ -2041,7 +2047,6 @@ namespace ts {
20412047
getDocCommentTemplateAtPosition,
20422048
isValidBraceCompletionAtPosition,
20432049
getSpanOfEnclosingComment,
2044-
getSpanForPosition,
20452050
getCodeFixesAtPosition,
20462051
getEmitOutput,
20472052
getNonBoundSourceFile,

src/services/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ namespace ts {
245245
findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[];
246246

247247
getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
248+
getDefinitionAndBoundSpan(fileName: string, position: number): DefinitionInfoAndBoundSpan;
248249
getTypeDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
249250
getImplementationAtPosition(fileName: string, position: number): ImplementationLocation[];
250251

@@ -273,7 +274,6 @@ namespace ts {
273274
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
274275

275276
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): TextSpan;
276-
getSpanForPosition(fileName: string, position: number): TextSpan;
277277

278278
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
279279
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
@@ -549,6 +549,11 @@ namespace ts {
549549
containerName: string;
550550
}
551551

552+
export interface DefinitionInfoAndBoundSpan {
553+
definitions: ReadonlyArray<DefinitionInfo>;
554+
textSpan: TextSpan;
555+
}
556+
552557
export interface ReferencedSymbolDefinitionInfo extends DefinitionInfo {
553558
displayParts: SymbolDisplayPart[];
554559
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,6 +3922,7 @@ declare namespace ts {
39223922
getRenameInfo(fileName: string, position: number): RenameInfo;
39233923
findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[];
39243924
getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
3925+
getDefinitionAndBoundSpan(fileName: string, position: number): DefinitionInfoAndBoundSpan;
39253926
getTypeDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
39263927
getImplementationAtPosition(fileName: string, position: number): ImplementationLocation[];
39273928
getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[];
@@ -3942,7 +3943,6 @@ declare namespace ts {
39423943
getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion;
39433944
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
39443945
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): TextSpan;
3945-
getSpanForPosition(fileName: string, position: number): TextSpan;
39463946
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
39473947
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
39483948
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string): RefactorEditInfo | undefined;
@@ -4174,6 +4174,10 @@ declare namespace ts {
41744174
containerKind: ScriptElementKind;
41754175
containerName: string;
41764176
}
4177+
interface DefinitionInfoAndBoundSpan {
4178+
definitions: ReadonlyArray<DefinitionInfo>;
4179+
textSpan: TextSpan;
4180+
}
41774181
interface ReferencedSymbolDefinitionInfo extends DefinitionInfo {
41784182
displayParts: SymbolDisplayPart[];
41794183
}
@@ -4793,6 +4797,7 @@ declare namespace ts.server.protocol {
47934797
CompileOnSaveEmitFile = "compileOnSaveEmitFile",
47944798
Configure = "configure",
47954799
Definition = "definition",
4800+
DefinitionAndBoundSpan = "definitionAndBoundSpan",
47964801
Implementation = "implementation",
47974802
Exit = "exit",
47984803
Format = "format",
@@ -5298,6 +5303,10 @@ declare namespace ts.server.protocol {
52985303
*/
52995304
file: string;
53005305
}
5306+
interface DefinitionInfoAndBoundSpan {
5307+
definitions: ReadonlyArray<FileSpan>;
5308+
textSpan: TextSpan;
5309+
}
53015310
/**
53025311
* Definition response message. Gives text range for definition.
53035312
*/
@@ -6855,6 +6864,9 @@ declare namespace ts.server {
68556864
private convertToDiagnosticsWithLinePosition(diagnostics, scriptInfo);
68566865
private getDiagnosticsWorker(args, isSemantic, selector, includeLinePosition);
68576866
private getDefinition(args, simplifiedResult);
6867+
private getDefinitionAndBoundSpan(args, simplifiedResult);
6868+
private getSimplifiedDefinition(definitions, project);
6869+
private getSimplifiedTextSpan(textSpan, scriptInfo);
68586870
private getTypeDefinition(args);
68596871
private getImplementation(args, simplifiedResult);
68606872
private getOccurrences(args);
@@ -6888,7 +6900,6 @@ declare namespace ts.server {
68886900
private getNameOrDottedNameSpan(args);
68896901
private isValidBraceCompletion(args);
68906902
private getQuickInfoWorker(args, simplifiedResult);
6891-
private getSpanForLocation(args);
68926903
private getFormattingEditsForRange(args);
68936904
private getFormattingEditsForRangeFull(args);
68946905
private getFormattingEditsForDocumentFull(args);

tests/baselines/reference/api/typescript.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3922,6 +3922,7 @@ declare namespace ts {
39223922
getRenameInfo(fileName: string, position: number): RenameInfo;
39233923
findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[];
39243924
getDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
3925+
getDefinitionAndBoundSpan(fileName: string, position: number): DefinitionInfoAndBoundSpan;
39253926
getTypeDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[];
39263927
getImplementationAtPosition(fileName: string, position: number): ImplementationLocation[];
39273928
getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[];
@@ -3942,7 +3943,6 @@ declare namespace ts {
39423943
getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion;
39433944
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
39443945
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): TextSpan;
3945-
getSpanForPosition(fileName: string, position: number): TextSpan;
39463946
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
39473947
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
39483948
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string): RefactorEditInfo | undefined;
@@ -4174,6 +4174,10 @@ declare namespace ts {
41744174
containerKind: ScriptElementKind;
41754175
containerName: string;
41764176
}
4177+
interface DefinitionInfoAndBoundSpan {
4178+
definitions: ReadonlyArray<DefinitionInfo>;
4179+
textSpan: TextSpan;
4180+
}
41774181
interface ReferencedSymbolDefinitionInfo extends DefinitionInfo {
41784182
displayParts: SymbolDisplayPart[];
41794183
}

0 commit comments

Comments
 (0)