Skip to content

Commit 87c986c

Browse files
authored
Modify interactive inlay hints API to be more backwards compatible (#55274)
1 parent ad01270 commit 87c986c

31 files changed

+573
-473
lines changed

src/harness/client.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -760,21 +760,20 @@ export class SessionClient implements LanguageService {
760760
const response = this.processResponse<protocol.InlayHintsResponse>(request);
761761

762762
return response.body!.map(item => {
763-
const { text, position } = item;
764-
const hint = typeof text === "string" ? text : text.map(({ text, span }) => ({
765-
text,
766-
span: span && {
767-
start: this.lineOffsetToPosition(span.file, span.start),
768-
length: this.lineOffsetToPosition(span.file, span.end) - this.lineOffsetToPosition(span.file, span.start),
769-
},
770-
file: span && span.file
771-
}));
763+
const { position, displayParts } = item;
772764

773765
return ({
774766
...item,
775767
position: this.lineOffsetToPosition(file, position),
776-
text: hint,
777-
kind: item.kind as InlayHintKind
768+
kind: item.kind as InlayHintKind,
769+
displayParts: displayParts?.map(({ text, span }) => ({
770+
text,
771+
span: span && {
772+
start: this.lineOffsetToPosition(span.file, span.start),
773+
length: this.lineOffsetToPosition(span.file, span.end) - this.lineOffsetToPosition(span.file, span.start),
774+
},
775+
file: span && span.file
776+
})),
778777
});
779778
});
780779
}

src/server/protocol.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2675,11 +2675,13 @@ export interface InlayHintsRequest extends Request {
26752675
}
26762676

26772677
export interface InlayHintItem {
2678-
text: string | InlayHintItemDisplayPart[];
2678+
/** This property will be the empty string when displayParts is set. */
2679+
text: string;
26792680
position: Location;
26802681
kind: InlayHintKind;
26812682
whitespaceBefore?: boolean;
26822683
whitespaceAfter?: boolean;
2684+
displayParts?: InlayHintItemDisplayPart[];
26832685
}
26842686

26852687
export interface InlayHintItemDisplayPart {

src/server/session.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1845,20 +1845,19 @@ export class Session<TMessage = string> implements EventSender {
18451845
const hints = project.getLanguageService().provideInlayHints(file, args, this.getPreferences(file));
18461846

18471847
return hints.map(hint => {
1848-
const { text, position } = hint;
1849-
const hintText = typeof text === "string" ? text : text.map(({ text, span, file }) => ({
1850-
text,
1851-
span: span && {
1852-
start: scriptInfo.positionToLineOffset(span.start),
1853-
end: scriptInfo.positionToLineOffset(span.start + span.length),
1854-
file: file!
1855-
}
1856-
}));
1848+
const { position, displayParts } = hint;
18571849

18581850
return {
18591851
...hint,
18601852
position: scriptInfo.positionToLineOffset(position),
1861-
text: hintText
1853+
displayParts: displayParts?.map(({ text, span, file }) => ({
1854+
text,
1855+
span: span && {
1856+
start: scriptInfo.positionToLineOffset(span.start),
1857+
end: scriptInfo.positionToLineOffset(span.start + span.length),
1858+
file: file!
1859+
}
1860+
})),
18621861
};
18631862
});
18641863
}

src/services/inlayHints.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,11 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
159159
}
160160

161161
function addParameterHints(text: string, parameter: Identifier, position: number, isFirstVariadicArgument: boolean, sourceFile: SourceFile | undefined) {
162-
let hintText: string | InlayHintDisplayPart[] = `${isFirstVariadicArgument ? "..." : ""}${text}`;
162+
let hintText = `${isFirstVariadicArgument ? "..." : ""}${text}`;
163+
let displayParts: InlayHintDisplayPart[] | undefined;
163164
if (shouldUseInteractiveInlayHints(preferences)) {
164-
hintText = [getNodeDisplayPart(hintText, parameter, sourceFile!), { text: ":" }];
165+
displayParts = [getNodeDisplayPart(hintText, parameter, sourceFile!), { text: ":" }];
166+
hintText = "";
165167
}
166168
else {
167169
hintText += ":";
@@ -172,6 +174,7 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
172174
position,
173175
kind: InlayHintKind.Parameter,
174176
whitespaceAfter: true,
177+
displayParts,
175178
});
176179
}
177180

src/services/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -866,11 +866,13 @@ export const enum InlayHintKind {
866866
}
867867

868868
export interface InlayHint {
869-
text: string | InlayHintDisplayPart[];
869+
/** This property will be the empty string when displayParts is set. */
870+
text: string;
870871
position: number;
871872
kind: InlayHintKind;
872873
whitespaceBefore?: boolean;
873874
whitespaceAfter?: boolean;
875+
displayParts?: InlayHintDisplayPart[];
874876
}
875877

876878
export interface InlayHintDisplayPart {

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -2124,11 +2124,13 @@ declare namespace ts {
21242124
arguments: InlayHintsRequestArgs;
21252125
}
21262126
interface InlayHintItem {
2127-
text: string | InlayHintItemDisplayPart[];
2127+
/** This property will be the empty string when displayParts is set. */
2128+
text: string;
21282129
position: Location;
21292130
kind: InlayHintKind;
21302131
whitespaceBefore?: boolean;
21312132
whitespaceAfter?: boolean;
2133+
displayParts?: InlayHintItemDisplayPart[];
21322134
}
21332135
interface InlayHintItemDisplayPart {
21342136
text: string;
@@ -10390,11 +10392,13 @@ declare namespace ts {
1039010392
Enum = "Enum"
1039110393
}
1039210394
interface InlayHint {
10393-
text: string | InlayHintDisplayPart[];
10395+
/** This property will be the empty string when displayParts is set. */
10396+
text: string;
1039410397
position: number;
1039510398
kind: InlayHintKind;
1039610399
whitespaceBefore?: boolean;
1039710400
whitespaceAfter?: boolean;
10401+
displayParts?: InlayHintDisplayPart[];
1039810402
}
1039910403
interface InlayHintDisplayPart {
1040010404
text: string;

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -6416,11 +6416,13 @@ declare namespace ts {
64166416
Enum = "Enum"
64176417
}
64186418
interface InlayHint {
6419-
text: string | InlayHintDisplayPart[];
6419+
/** This property will be the empty string when displayParts is set. */
6420+
text: string;
64206421
position: number;
64216422
kind: InlayHintKind;
64226423
whitespaceBefore?: boolean;
64236424
whitespaceAfter?: boolean;
6425+
displayParts?: InlayHintDisplayPart[];
64246426
}
64256427
interface InlayHintDisplayPart {
64266428
text: string;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
foo(1, 2);
22
^
33
{
4-
"text": [
4+
"text": "",
5+
"position": 43,
6+
"kind": "Parameter",
7+
"whitespaceAfter": true,
8+
"displayParts": [
59
{
610
"text": "a",
711
"span": {
@@ -13,16 +17,17 @@ foo(1, 2);
1317
{
1418
"text": ":"
1519
}
16-
],
17-
"position": 43,
18-
"kind": "Parameter",
19-
"whitespaceAfter": true
20+
]
2021
}
2122

2223
foo(1, 2);
2324
^
2425
{
25-
"text": [
26+
"text": "",
27+
"position": 46,
28+
"kind": "Parameter",
29+
"whitespaceAfter": true,
30+
"displayParts": [
2631
{
2732
"text": "b",
2833
"span": {
@@ -34,8 +39,5 @@ foo(1, 2);
3439
{
3540
"text": ":"
3641
}
37-
],
38-
"position": 46,
39-
"kind": "Parameter",
40-
"whitespaceAfter": true
42+
]
4143
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
foo(1)(2);
22
^
33
{
4-
"text": [
4+
"text": "",
5+
"position": 87,
6+
"kind": "Parameter",
7+
"whitespaceAfter": true,
8+
"displayParts": [
59
{
610
"text": "a",
711
"span": {
@@ -13,16 +17,17 @@ foo(1)(2);
1317
{
1418
"text": ":"
1519
}
16-
],
17-
"position": 87,
18-
"kind": "Parameter",
19-
"whitespaceAfter": true
20+
]
2021
}
2122

2223
foo(1)(2);
2324
^
2425
{
25-
"text": [
26+
"text": "",
27+
"position": 90,
28+
"kind": "Parameter",
29+
"whitespaceAfter": true,
30+
"displayParts": [
2631
{
2732
"text": "b",
2833
"span": {
@@ -34,8 +39,5 @@ foo(1)(2);
3439
{
3540
"text": ":"
3641
}
37-
],
38-
"position": 90,
39-
"kind": "Parameter",
40-
"whitespaceAfter": true
42+
]
4143
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
return a(1) + 2
22
^
33
{
4-
"text": [
4+
"text": "",
5+
"position": 54,
6+
"kind": "Parameter",
7+
"whitespaceAfter": true,
8+
"displayParts": [
59
{
610
"text": "b",
711
"span": {
@@ -13,16 +17,17 @@
1317
{
1418
"text": ":"
1519
}
16-
],
17-
"position": 54,
18-
"kind": "Parameter",
19-
"whitespaceAfter": true
20+
]
2021
}
2122

2223
foo((c: number) => c + 1);
2324
^
2425
{
25-
"text": [
26+
"text": "",
27+
"position": 67,
28+
"kind": "Parameter",
29+
"whitespaceAfter": true,
30+
"displayParts": [
2631
{
2732
"text": "a",
2833
"span": {
@@ -34,8 +39,5 @@ foo((c: number) => c + 1);
3439
{
3540
"text": ":"
3641
}
37-
],
38-
"position": 67,
39-
"kind": "Parameter",
40-
"whitespaceAfter": true
42+
]
4143
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
foo(a, 2);
22
^
33
{
4-
"text": [
4+
"text": "",
5+
"position": 66,
6+
"kind": "Parameter",
7+
"whitespaceAfter": true,
8+
"displayParts": [
59
{
610
"text": "b",
711
"span": {
@@ -13,8 +17,5 @@ foo(a, 2);
1317
{
1418
"text": ":"
1519
}
16-
],
17-
"position": 66,
18-
"kind": "Parameter",
19-
"whitespaceAfter": true
20+
]
2021
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
foo(1, { c: 1 });
22
^
33
{
4-
"text": [
4+
"text": "",
5+
"position": 44,
6+
"kind": "Parameter",
7+
"whitespaceAfter": true,
8+
"displayParts": [
59
{
610
"text": "a",
711
"span": {
@@ -13,8 +17,5 @@ foo(1, { c: 1 });
1317
{
1418
"text": ":"
1519
}
16-
],
17-
"position": 44,
18-
"kind": "Parameter",
19-
"whitespaceAfter": true
20+
]
2021
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
foo(1, 1, 1, 1);
22
^
33
{
4-
"text": [
4+
"text": "",
5+
"position": 48,
6+
"kind": "Parameter",
7+
"whitespaceAfter": true,
8+
"displayParts": [
59
{
610
"text": "a",
711
"span": {
@@ -13,16 +17,17 @@ foo(1, 1, 1, 1);
1317
{
1418
"text": ":"
1519
}
16-
],
17-
"position": 48,
18-
"kind": "Parameter",
19-
"whitespaceAfter": true
20+
]
2021
}
2122

2223
foo(1, 1, 1, 1);
2324
^
2425
{
25-
"text": [
26+
"text": "",
27+
"position": 51,
28+
"kind": "Parameter",
29+
"whitespaceAfter": true,
30+
"displayParts": [
2631
{
2732
"text": "...b",
2833
"span": {
@@ -34,8 +39,5 @@ foo(1, 1, 1, 1);
3439
{
3540
"text": ":"
3641
}
37-
],
38-
"position": 51,
39-
"kind": "Parameter",
40-
"whitespaceAfter": true
42+
]
4143
}

0 commit comments

Comments
 (0)