Skip to content

Commit 5fe88ea

Browse files
authored
[Interactive inlay hints] Get source file from parameter node (microsoft#55476)
1 parent 12d9f04 commit 5fe88ea

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

src/server/session.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,14 +1875,24 @@ export class Session<TMessage = string> implements EventSender {
18751875
return {
18761876
...hint,
18771877
position: scriptInfo.positionToLineOffset(position),
1878-
displayParts: displayParts?.map(({ text, span, file }) => ({
1879-
text,
1880-
span: span && {
1881-
start: scriptInfo.positionToLineOffset(span.start),
1882-
end: scriptInfo.positionToLineOffset(span.start + span.length),
1883-
file: file!,
1884-
},
1885-
})),
1878+
displayParts: displayParts?.map(({ text, span, file }) => {
1879+
if (span) {
1880+
Debug.assertIsDefined(file, "Target file should be defined together with its span.");
1881+
const scriptInfo = this.projectService.getScriptInfo(file)!;
1882+
1883+
return {
1884+
text,
1885+
span: {
1886+
start: scriptInfo.positionToLineOffset(span.start),
1887+
end: scriptInfo.positionToLineOffset(span.start + span.length),
1888+
file,
1889+
},
1890+
};
1891+
}
1892+
else {
1893+
return { text };
1894+
}
1895+
}),
18861896
};
18871897
});
18881898
}

src/services/inlayHints.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import {
6262
Signature,
6363
skipParentheses,
6464
some,
65-
SourceFile,
6665
Symbol,
6766
SymbolFlags,
6867
SyntaxKind,
@@ -157,11 +156,11 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
157156
return isArrowFunction(node) || isFunctionExpression(node) || isFunctionDeclaration(node) || isMethodDeclaration(node) || isGetAccessorDeclaration(node);
158157
}
159158

160-
function addParameterHints(text: string, parameter: Identifier, position: number, isFirstVariadicArgument: boolean, sourceFile: SourceFile | undefined) {
159+
function addParameterHints(text: string, parameter: Identifier, position: number, isFirstVariadicArgument: boolean) {
161160
let hintText = `${isFirstVariadicArgument ? "..." : ""}${text}`;
162161
let displayParts: InlayHintDisplayPart[] | undefined;
163162
if (shouldUseInteractiveInlayHints(preferences)) {
164-
displayParts = [getNodeDisplayPart(hintText, parameter, sourceFile!), { text: ":" }];
163+
displayParts = [getNodeDisplayPart(hintText, parameter), { text: ":" }];
165164
hintText = "";
166165
}
167166
else {
@@ -247,8 +246,6 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
247246
return;
248247
}
249248

250-
const sourceFile = shouldUseInteractiveInlayHints(preferences) ? expr.getSourceFile() : undefined;
251-
252249
let signatureParamPos = 0;
253250
for (const originalArg of args) {
254251
const arg = skipParentheses(originalArg);
@@ -287,7 +284,7 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
287284
continue;
288285
}
289286

290-
addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument, sourceFile);
287+
addParameterHints(name, parameter, originalArg.getStart(), isFirstVariadicArgument);
291288
}
292289
}
293290
}
@@ -437,7 +434,8 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
437434
return true;
438435
}
439436

440-
function getNodeDisplayPart(text: string, node: Node, sourceFile: SourceFile): InlayHintDisplayPart {
437+
function getNodeDisplayPart(text: string, node: Node): InlayHintDisplayPart {
438+
const sourceFile = node.getSourceFile();
441439
return {
442440
text,
443441
span: createTextSpanFromNode(node, sourceFile),
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
helperB("hello, world!");
2+
^
3+
{
4+
"text": "",
5+
"position": 45,
6+
"kind": "Parameter",
7+
"whitespaceAfter": true,
8+
"displayParts": [
9+
{
10+
"text": "bParam",
11+
"span": {
12+
"start": 61,
13+
"length": 6
14+
},
15+
"file": "/tests/cases/fourslash/bbb.mts"
16+
},
17+
{
18+
"text": ":"
19+
}
20+
]
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Target: esnext
4+
// @module: nodenext
5+
6+
// @Filename: aaa.mts
7+
////import { helperB } from "./bbb.mjs";
8+
////helperB("hello, world!");
9+
10+
// @Filename: bbb.mts
11+
////import { helperC } from "./ccc.mjs";
12+
13+
////export function helperB(bParam: string) {
14+
//// helperC(bParam);
15+
////}
16+
17+
// @Filename: ccc.mts
18+
////export function helperC(cParam: string) {}
19+
20+
goTo.file("./aaa.mts");
21+
verify.baselineInlayHints(undefined, {
22+
includeInlayParameterNameHints: "all",
23+
interactiveInlayHints: true
24+
});

0 commit comments

Comments
 (0)