diff --git a/src/server/session.ts b/src/server/session.ts index 0cf89ad746a23..5a95d12e26dec 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -1875,14 +1875,24 @@ export class Session 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 }; + } + }), }; }); } diff --git a/src/services/inlayHints.ts b/src/services/inlayHints.ts index 6b741dcf9506e..a2381d491ea58 100644 --- a/src/services/inlayHints.ts +++ b/src/services/inlayHints.ts @@ -62,7 +62,6 @@ import { Signature, skipParentheses, some, - SourceFile, Symbol, SymbolFlags, SyntaxKind, @@ -157,11 +156,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 { @@ -247,8 +246,6 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] { return; } - const sourceFile = shouldUseInteractiveInlayHints(preferences) ? expr.getSourceFile() : undefined; - let signatureParamPos = 0; for (const originalArg of args) { const arg = skipParentheses(originalArg); @@ -287,7 +284,7 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] { continue; } - addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument, sourceFile); + addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument); } } } @@ -437,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), diff --git a/tests/baselines/reference/inlayHintsInteractiveMultifileFunctionCalls.baseline b/tests/baselines/reference/inlayHintsInteractiveMultifileFunctionCalls.baseline new file mode 100644 index 0000000000000..47ac8e6d4ad00 --- /dev/null +++ b/tests/baselines/reference/inlayHintsInteractiveMultifileFunctionCalls.baseline @@ -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": ":" + } + ] +} \ No newline at end of file diff --git a/tests/cases/fourslash/inlayHintsInteractiveMultifileFunctionCalls.ts b/tests/cases/fourslash/inlayHintsInteractiveMultifileFunctionCalls.ts new file mode 100644 index 0000000000000..4d68c05bde68b --- /dev/null +++ b/tests/cases/fourslash/inlayHintsInteractiveMultifileFunctionCalls.ts @@ -0,0 +1,24 @@ +/// + +// @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 +});