Skip to content

Commit

Permalink
Modify interactive inlay hints API to be more backwards compatible (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Aug 5, 2023
1 parent ad01270 commit 87c986c
Show file tree
Hide file tree
Showing 31 changed files with 573 additions and 473 deletions.
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 += ":";
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
]
}

0 comments on commit 87c986c

Please sign in to comment.