Skip to content

Commit

Permalink
Cherry-pick fix for cross-file inlay hints (#55476) to release-5.2
Browse files Browse the repository at this point in the history
…and LKG (#55487)

Co-authored-by: Maria José Solano <majosolano99@gmail.com>
  • Loading branch information
DanielRosenwasser and MariaSolOs committed Aug 24, 2023
1 parent 555ef99 commit 9684ba6
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 42 deletions.
32 changes: 20 additions & 12 deletions lib/tsserver.js
Expand Up @@ -164760,11 +164760,11 @@ function provideInlayHints(context) {
function isSignatureSupportingReturnAnnotation(node) {
return isArrowFunction(node) || isFunctionExpression(node) || isFunctionDeclaration(node) || isMethodDeclaration(node) || isGetAccessorDeclaration(node);
}
function addParameterHints(text, parameter, position, isFirstVariadicArgument, sourceFile) {
function addParameterHints(text, parameter, position, isFirstVariadicArgument) {
let hintText = `${isFirstVariadicArgument ? "..." : ""}${text}`;
let displayParts;
if (shouldUseInteractiveInlayHints(preferences)) {
displayParts = [getNodeDisplayPart(hintText, parameter, sourceFile), { text: ":" }];
displayParts = [getNodeDisplayPart(hintText, parameter), { text: ":" }];
hintText = "";
} else {
hintText += ":";
Expand Down Expand Up @@ -164837,7 +164837,6 @@ function provideInlayHints(context) {
return;
}
let signatureParamPos = 0;
const sourceFile = shouldUseInteractiveInlayHints(preferences) ? expr.getSourceFile() : void 0;
for (const originalArg of args) {
const arg = skipParentheses(originalArg);
if (shouldShowLiteralParameterNameHintsOnly(preferences) && !isHintableLiteral(arg)) {
Expand Down Expand Up @@ -164870,7 +164869,7 @@ function provideInlayHints(context) {
if (leadingCommentsContainsParameterName(arg, name)) {
continue;
}
addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument, sourceFile);
addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument);
}
}
}
Expand Down Expand Up @@ -165006,7 +165005,8 @@ function provideInlayHints(context) {
}
return true;
}
function getNodeDisplayPart(text, node, sourceFile) {
function getNodeDisplayPart(text, node) {
const sourceFile = node.getSourceFile();
return {
text,
span: createTextSpanFromNode(node, sourceFile),
Expand Down Expand Up @@ -183666,14 +183666,22 @@ Project '${project.projectName}' (${ProjectKind[project.projectKind]}) ${counter
return {
...hint,
position: scriptInfo.positionToLineOffset(position),
displayParts: displayParts == null ? void 0 : displayParts.map(({ text, span, file: file2 }) => ({
text,
span: span && {
start: scriptInfo.positionToLineOffset(span.start),
end: scriptInfo.positionToLineOffset(span.start + span.length),
file: file2
displayParts: displayParts == null ? void 0 : displayParts.map(({ text, span, file: file2 }) => {
if (span) {
Debug.assertIsDefined(file2, "Target file should be defined together with its span.");
const scriptInfo2 = this.projectService.getScriptInfo(file2);
return {
text,
span: {
start: scriptInfo2.positionToLineOffset(span.start),
end: scriptInfo2.positionToLineOffset(span.start + span.length),
file: file2
}
};
} else {
return { text };
}
}))
})
};
});
}
Expand Down
32 changes: 20 additions & 12 deletions lib/tsserverlibrary.js
Expand Up @@ -164108,11 +164108,11 @@ ${newComment.split("\n").map((c) => ` * ${c}`).join("\n")}
function isSignatureSupportingReturnAnnotation(node) {
return isArrowFunction(node) || isFunctionExpression(node) || isFunctionDeclaration(node) || isMethodDeclaration(node) || isGetAccessorDeclaration(node);
}
function addParameterHints(text, parameter, position, isFirstVariadicArgument, sourceFile) {
function addParameterHints(text, parameter, position, isFirstVariadicArgument) {
let hintText = `${isFirstVariadicArgument ? "..." : ""}${text}`;
let displayParts;
if (shouldUseInteractiveInlayHints(preferences)) {
displayParts = [getNodeDisplayPart(hintText, parameter, sourceFile), { text: ":" }];
displayParts = [getNodeDisplayPart(hintText, parameter), { text: ":" }];
hintText = "";
} else {
hintText += ":";
Expand Down Expand Up @@ -164185,7 +164185,6 @@ ${newComment.split("\n").map((c) => ` * ${c}`).join("\n")}
return;
}
let signatureParamPos = 0;
const sourceFile = shouldUseInteractiveInlayHints(preferences) ? expr.getSourceFile() : void 0;
for (const originalArg of args) {
const arg = skipParentheses(originalArg);
if (shouldShowLiteralParameterNameHintsOnly(preferences) && !isHintableLiteral(arg)) {
Expand Down Expand Up @@ -164218,7 +164217,7 @@ ${newComment.split("\n").map((c) => ` * ${c}`).join("\n")}
if (leadingCommentsContainsParameterName(arg, name)) {
continue;
}
addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument, sourceFile);
addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument);
}
}
}
Expand Down Expand Up @@ -164354,7 +164353,8 @@ ${newComment.split("\n").map((c) => ` * ${c}`).join("\n")}
}
return true;
}
function getNodeDisplayPart(text, node, sourceFile) {
function getNodeDisplayPart(text, node) {
const sourceFile = node.getSourceFile();
return {
text,
span: createTextSpanFromNode(node, sourceFile),
Expand Down Expand Up @@ -181073,14 +181073,22 @@ Project '${project.projectName}' (${ProjectKind[project.projectKind]}) ${counter
return {
...hint,
position: scriptInfo.positionToLineOffset(position),
displayParts: displayParts == null ? void 0 : displayParts.map(({ text, span, file: file2 }) => ({
text,
span: span && {
start: scriptInfo.positionToLineOffset(span.start),
end: scriptInfo.positionToLineOffset(span.start + span.length),
file: file2
displayParts: displayParts == null ? void 0 : displayParts.map(({ text, span, file: file2 }) => {
if (span) {
Debug.assertIsDefined(file2, "Target file should be defined together with its span.");
const scriptInfo2 = this.projectService.getScriptInfo(file2);
return {
text,
span: {
start: scriptInfo2.positionToLineOffset(span.start),
end: scriptInfo2.positionToLineOffset(span.start + span.length),
file: file2
}
};
} else {
return { text };
}
}))
})
};
});
}
Expand Down
10 changes: 5 additions & 5 deletions lib/typescript.js
Expand Up @@ -164123,11 +164123,11 @@ ${newComment.split("\n").map((c) => ` * ${c}`).join("\n")}
function isSignatureSupportingReturnAnnotation(node) {
return isArrowFunction(node) || isFunctionExpression(node) || isFunctionDeclaration(node) || isMethodDeclaration(node) || isGetAccessorDeclaration(node);
}
function addParameterHints(text, parameter, position, isFirstVariadicArgument, sourceFile) {
function addParameterHints(text, parameter, position, isFirstVariadicArgument) {
let hintText = `${isFirstVariadicArgument ? "..." : ""}${text}`;
let displayParts;
if (shouldUseInteractiveInlayHints(preferences)) {
displayParts = [getNodeDisplayPart(hintText, parameter, sourceFile), { text: ":" }];
displayParts = [getNodeDisplayPart(hintText, parameter), { text: ":" }];
hintText = "";
} else {
hintText += ":";
Expand Down Expand Up @@ -164200,7 +164200,6 @@ ${newComment.split("\n").map((c) => ` * ${c}`).join("\n")}
return;
}
let signatureParamPos = 0;
const sourceFile = shouldUseInteractiveInlayHints(preferences) ? expr.getSourceFile() : void 0;
for (const originalArg of args) {
const arg = skipParentheses(originalArg);
if (shouldShowLiteralParameterNameHintsOnly(preferences) && !isHintableLiteral(arg)) {
Expand Down Expand Up @@ -164233,7 +164232,7 @@ ${newComment.split("\n").map((c) => ` * ${c}`).join("\n")}
if (leadingCommentsContainsParameterName(arg, name)) {
continue;
}
addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument, sourceFile);
addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument);
}
}
}
Expand Down Expand Up @@ -164369,7 +164368,8 @@ ${newComment.split("\n").map((c) => ` * ${c}`).join("\n")}
}
return true;
}
function getNodeDisplayPart(text, node, sourceFile) {
function getNodeDisplayPart(text, node) {
const sourceFile = node.getSourceFile();
return {
text,
span: createTextSpanFromNode(node, sourceFile),
Expand Down
24 changes: 17 additions & 7 deletions src/server/session.ts
Expand Up @@ -1850,14 +1850,24 @@ export class Session<TMessage = string> implements EventSender {
return {
...hint,
position: scriptInfo.positionToLineOffset(position),
displayParts: displayParts?.map(({ text, span, file }) => ({
text,
span: span && {
start: scriptInfo.positionToLineOffset(span.start),
end: scriptInfo.positionToLineOffset(span.start + span.length),
file: file!
displayParts: displayParts?.map(({ text, span, file }) => {
if (span) {
Debug.assertIsDefined(file, "Target file should be defined together with its span.");
const scriptInfo = this.projectService.getScriptInfo(file)!;

return {
text,
span: {
start: scriptInfo.positionToLineOffset(span.start),
end: scriptInfo.positionToLineOffset(span.start + span.length),
file,
},
};
}
})),
else {
return { text };
}
}),
};
});
}
Expand Down
11 changes: 5 additions & 6 deletions src/services/inlayHints.ts
Expand Up @@ -62,7 +62,6 @@ import {
Signature,
skipParentheses,
some,
SourceFile,
Symbol,
SymbolFlags,
SyntaxKind,
Expand Down Expand Up @@ -158,11 +157,11 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
return isArrowFunction(node) || isFunctionExpression(node) || isFunctionDeclaration(node) || isMethodDeclaration(node) || isGetAccessorDeclaration(node);
}

function addParameterHints(text: string, parameter: Identifier, position: number, isFirstVariadicArgument: boolean, sourceFile: SourceFile | undefined) {
function addParameterHints(text: string, parameter: Identifier, position: number, isFirstVariadicArgument: boolean) {
let hintText = `${isFirstVariadicArgument ? "..." : ""}${text}`;
let displayParts: InlayHintDisplayPart[] | undefined;
if (shouldUseInteractiveInlayHints(preferences)) {
displayParts = [getNodeDisplayPart(hintText, parameter, sourceFile!), { text: ":" }];
displayParts = [getNodeDisplayPart(hintText, parameter), { text: ":" }];
hintText = "";
}
else {
Expand Down Expand Up @@ -249,7 +248,6 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
}

let signatureParamPos = 0;
const sourceFile = shouldUseInteractiveInlayHints(preferences) ? expr.getSourceFile() : undefined;
for (const originalArg of args) {
const arg = skipParentheses(originalArg);
if (shouldShowLiteralParameterNameHintsOnly(preferences) && !isHintableLiteral(arg)) {
Expand Down Expand Up @@ -286,7 +284,7 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
continue;
}

addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument, sourceFile);
addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument);
}
}
}
Expand Down Expand Up @@ -436,7 +434,8 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
return true;
}

function getNodeDisplayPart(text: string, node: Node, sourceFile: SourceFile): InlayHintDisplayPart {
function getNodeDisplayPart(text: string, node: Node): InlayHintDisplayPart {
const sourceFile = node.getSourceFile();
return {
text,
span: createTextSpanFromNode(node, sourceFile),
Expand Down
@@ -0,0 +1,21 @@
helperB("hello, world!");
^
{
"text": "",
"position": 45,
"kind": "Parameter",
"whitespaceAfter": true,
"displayParts": [
{
"text": "bParam",
"span": {
"start": 61,
"length": 6
},
"file": "/tests/cases/fourslash/bbb.mts"
},
{
"text": ":"
}
]
}
@@ -0,0 +1,24 @@
/// <reference path="fourslash.ts" />

// @Target: esnext
// @module: nodenext

// @Filename: aaa.mts
////import { helperB } from "./bbb.mjs";
////helperB("hello, world!");

// @Filename: bbb.mts
////import { helperC } from "./ccc.mjs";

////export function helperB(bParam: string) {
//// helperC(bParam);
////}

// @Filename: ccc.mts
////export function helperC(cParam: string) {}

goTo.file("./aaa.mts");
verify.baselineInlayHints(undefined, {
includeInlayParameterNameHints: "all",
interactiveInlayHints: true
});

0 comments on commit 9684ba6

Please sign in to comment.