Skip to content
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

[Interactive inlay hints] Get source file from parameter node #55476

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1875,14 +1875,23 @@ 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) {
const scriptInfo = this.projectService.getScriptInfo(file!)!;

return {
text,
span: {
start: scriptInfo.positionToLineOffset(span.start),
end: scriptInfo.positionToLineOffset(span.start + span.length),
file: file!,
MariaSolOs marked this conversation as resolved.
Show resolved Hide resolved
},
};
}
else {
return { text };
}
}),
};
});
}
Expand Down
12 changes: 5 additions & 7 deletions src/services/inlayHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import {
Signature,
skipParentheses,
some,
SourceFile,
Symbol,
SymbolFlags,
SyntaxKind,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -287,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 @@ -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),
Expand Down