Skip to content

Modify interactive inlay hints API to be more backwards compatible #55274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/harness/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,21 +760,20 @@ export class SessionClient implements LanguageService {
const response = this.processResponse<protocol.InlayHintsResponse>(request);

return response.body!.map(item => {
const { text, position } = item;
const hint = typeof text === "string" ? text : text.map(({ text, span }) => ({
text,
span: span && {
start: this.lineOffsetToPosition(span.file, span.start),
length: this.lineOffsetToPosition(span.file, span.end) - this.lineOffsetToPosition(span.file, span.start),
},
file: span && span.file
}));
const { position, displayParts } = item;

return ({
...item,
position: this.lineOffsetToPosition(file, position),
text: hint,
kind: item.kind as InlayHintKind
kind: item.kind as InlayHintKind,
displayParts: displayParts?.map(({ text, span }) => ({
text,
span: span && {
start: this.lineOffsetToPosition(span.file, span.start),
length: this.lineOffsetToPosition(span.file, span.end) - this.lineOffsetToPosition(span.file, span.start),
},
file: span && span.file
})),
});
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2675,11 +2675,13 @@ export interface InlayHintsRequest extends Request {
}

export interface InlayHintItem {
text: string | InlayHintItemDisplayPart[];
/** This property will be the empty string when displayParts is set. */
text: string;
position: Location;
kind: InlayHintKind;
whitespaceBefore?: boolean;
whitespaceAfter?: boolean;
displayParts?: InlayHintItemDisplayPart[];
}

export interface InlayHintItemDisplayPart {
Expand Down
19 changes: 9 additions & 10 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1845,20 +1845,19 @@ export class Session<TMessage = string> implements EventSender {
const hints = project.getLanguageService().provideInlayHints(file, args, this.getPreferences(file));

return hints.map(hint => {
const { text, position } = hint;
const hintText = typeof text === "string" ? text : text.map(({ text, span, file }) => ({
text,
span: span && {
start: scriptInfo.positionToLineOffset(span.start),
end: scriptInfo.positionToLineOffset(span.start + span.length),
file: file!
}
}));
const { position, displayParts } = hint;

return {
...hint,
position: scriptInfo.positionToLineOffset(position),
text: hintText
displayParts: displayParts?.map(({ text, span, file }) => ({
text,
span: span && {
start: scriptInfo.positionToLineOffset(span.start),
end: scriptInfo.positionToLineOffset(span.start + span.length),
file: file!
}
})),
};
});
}
Expand Down
7 changes: 5 additions & 2 deletions src/services/inlayHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
}

function addParameterHints(text: string, parameter: Identifier, position: number, isFirstVariadicArgument: boolean, sourceFile: SourceFile | undefined) {
let hintText: string | InlayHintDisplayPart[] = `${isFirstVariadicArgument ? "..." : ""}${text}`;
let hintText = `${isFirstVariadicArgument ? "..." : ""}${text}`;
let displayParts: InlayHintDisplayPart[] | undefined;
if (shouldUseInteractiveInlayHints(preferences)) {
hintText = [getNodeDisplayPart(hintText, parameter, sourceFile!), { text: ":" }];
displayParts = [getNodeDisplayPart(hintText, parameter, sourceFile!), { text: ":" }];
hintText = "";
}
else {
hintText += ":";
Comment on lines +166 to 169
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't want to leave this blank, we'd need to drop the reassignment in the first branch, and then pull the code out of the second branch to make it unconditional. I.e., add the prop only when requested and everything else stays the same.

Happy to do whatever. Not sure which is better.

Expand All @@ -172,6 +174,7 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
position,
kind: InlayHintKind.Parameter,
whitespaceAfter: true,
displayParts,
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,11 +866,13 @@ export const enum InlayHintKind {
}

export interface InlayHint {
text: string | InlayHintDisplayPart[];
/** This property will be the empty string when displayParts is set. */
text: string;
position: number;
kind: InlayHintKind;
whitespaceBefore?: boolean;
whitespaceAfter?: boolean;
displayParts?: InlayHintDisplayPart[];
}

export interface InlayHintDisplayPart {
Expand Down
8 changes: 6 additions & 2 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2124,11 +2124,13 @@ declare namespace ts {
arguments: InlayHintsRequestArgs;
}
interface InlayHintItem {
text: string | InlayHintItemDisplayPart[];
/** This property will be the empty string when displayParts is set. */
text: string;
position: Location;
kind: InlayHintKind;
whitespaceBefore?: boolean;
whitespaceAfter?: boolean;
displayParts?: InlayHintItemDisplayPart[];
}
interface InlayHintItemDisplayPart {
text: string;
Expand Down Expand Up @@ -10390,11 +10392,13 @@ declare namespace ts {
Enum = "Enum"
}
interface InlayHint {
text: string | InlayHintDisplayPart[];
/** This property will be the empty string when displayParts is set. */
text: string;
position: number;
kind: InlayHintKind;
whitespaceBefore?: boolean;
whitespaceAfter?: boolean;
displayParts?: InlayHintDisplayPart[];
}
interface InlayHintDisplayPart {
text: string;
Expand Down
4 changes: 3 additions & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6416,11 +6416,13 @@ declare namespace ts {
Enum = "Enum"
}
interface InlayHint {
text: string | InlayHintDisplayPart[];
/** This property will be the empty string when displayParts is set. */
text: string;
position: number;
kind: InlayHintKind;
whitespaceBefore?: boolean;
whitespaceAfter?: boolean;
displayParts?: InlayHintDisplayPart[];
}
interface InlayHintDisplayPart {
text: string;
Expand Down
22 changes: 12 additions & 10 deletions tests/baselines/reference/inlayHintsShouldWork1.baseline
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
foo(1, 2);
^
{
"text": [
"text": "",
"position": 43,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "a",
"span": {
Expand All @@ -13,16 +17,17 @@ foo(1, 2);
{
"text": ":"
}
],
"position": 43,
"kind": "Parameter",
"whitespaceAfter": true
]
}

foo(1, 2);
^
{
"text": [
"text": "",
"position": 46,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "b",
"span": {
Expand All @@ -34,8 +39,5 @@ foo(1, 2);
{
"text": ":"
}
],
"position": 46,
"kind": "Parameter",
"whitespaceAfter": true
]
}
22 changes: 12 additions & 10 deletions tests/baselines/reference/inlayHintsShouldWork11.baseline
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
foo(1)(2);
^
{
"text": [
"text": "",
"position": 87,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "a",
"span": {
Expand All @@ -13,16 +17,17 @@ foo(1)(2);
{
"text": ":"
}
],
"position": 87,
"kind": "Parameter",
"whitespaceAfter": true
]
}

foo(1)(2);
^
{
"text": [
"text": "",
"position": 90,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "b",
"span": {
Expand All @@ -34,8 +39,5 @@ foo(1)(2);
{
"text": ":"
}
],
"position": 90,
"kind": "Parameter",
"whitespaceAfter": true
]
}
22 changes: 12 additions & 10 deletions tests/baselines/reference/inlayHintsShouldWork12.baseline
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
return a(1) + 2
^
{
"text": [
"text": "",
"position": 54,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "b",
"span": {
Expand All @@ -13,16 +17,17 @@
{
"text": ":"
}
],
"position": 54,
"kind": "Parameter",
"whitespaceAfter": true
]
}

foo((c: number) => c + 1);
^
{
"text": [
"text": "",
"position": 67,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "a",
"span": {
Expand All @@ -34,8 +39,5 @@ foo((c: number) => c + 1);
{
"text": ":"
}
],
"position": 67,
"kind": "Parameter",
"whitespaceAfter": true
]
}
11 changes: 6 additions & 5 deletions tests/baselines/reference/inlayHintsShouldWork13.baseline
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
foo(a, 2);
^
{
"text": [
"text": "",
"position": 66,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "b",
"span": {
Expand All @@ -13,8 +17,5 @@ foo(a, 2);
{
"text": ":"
}
],
"position": 66,
"kind": "Parameter",
"whitespaceAfter": true
]
}
11 changes: 6 additions & 5 deletions tests/baselines/reference/inlayHintsShouldWork2.baseline
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
foo(1, { c: 1 });
^
{
"text": [
"text": "",
"position": 44,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "a",
"span": {
Expand All @@ -13,8 +17,5 @@ foo(1, { c: 1 });
{
"text": ":"
}
],
"position": 44,
"kind": "Parameter",
"whitespaceAfter": true
]
}
22 changes: 12 additions & 10 deletions tests/baselines/reference/inlayHintsShouldWork3.baseline
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
foo(1, 1, 1, 1);
^
{
"text": [
"text": "",
"position": 48,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "a",
"span": {
Expand All @@ -13,16 +17,17 @@ foo(1, 1, 1, 1);
{
"text": ":"
}
],
"position": 48,
"kind": "Parameter",
"whitespaceAfter": true
]
}

foo(1, 1, 1, 1);
^
{
"text": [
"text": "",
"position": 51,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "...b",
"span": {
Expand All @@ -34,8 +39,5 @@ foo(1, 1, 1, 1);
{
"text": ":"
}
],
"position": 51,
"kind": "Parameter",
"whitespaceAfter": true
]
}
Loading