mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Add inlay parameter name hints for function calls
Signed-off-by: imtsuki <me@qjx.app>
This commit is contained in:
parent
d8d8c20077
commit
c390e92fdd
6 changed files with 205 additions and 13 deletions
|
@ -38,6 +38,12 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
|
|||
},
|
||||
});
|
||||
|
||||
const parameterHintDecorationType = vscode.window.createTextEditorDecorationType({
|
||||
before: {
|
||||
color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
|
||||
}
|
||||
})
|
||||
|
||||
class HintsUpdater {
|
||||
private pending: Map<string, vscode.CancellationTokenSource> = new Map();
|
||||
private ctx: Ctx;
|
||||
|
@ -55,7 +61,10 @@ class HintsUpdater {
|
|||
if (this.enabled) {
|
||||
await this.refresh();
|
||||
} else {
|
||||
this.allEditors.forEach(it => this.setDecorations(it, []));
|
||||
this.allEditors.forEach(it => {
|
||||
this.setTypeDecorations(it, []);
|
||||
this.setParameterDecorations(it, []);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,15 +77,27 @@ class HintsUpdater {
|
|||
private async refreshEditor(editor: vscode.TextEditor): Promise<void> {
|
||||
const newHints = await this.queryHints(editor.document.uri.toString());
|
||||
if (newHints == null) return;
|
||||
const newDecorations = newHints.map(hint => ({
|
||||
range: hint.range,
|
||||
renderOptions: {
|
||||
after: {
|
||||
contentText: `: ${hint.label}`,
|
||||
const newTypeDecorations = newHints.filter(hint => hint.kind === 'TypeHint')
|
||||
.map(hint => ({
|
||||
range: hint.range,
|
||||
renderOptions: {
|
||||
after: {
|
||||
contentText: `: ${hint.label}`,
|
||||
},
|
||||
},
|
||||
},
|
||||
}));
|
||||
this.setDecorations(editor, newDecorations);
|
||||
}));
|
||||
this.setTypeDecorations(editor, newTypeDecorations);
|
||||
|
||||
const newParameterDecorations = newHints.filter(hint => hint.kind === 'ParameterHint')
|
||||
.map(hint => ({
|
||||
range: hint.range,
|
||||
renderOptions: {
|
||||
before: {
|
||||
contentText: `${hint.label}: `,
|
||||
},
|
||||
},
|
||||
}));
|
||||
this.setParameterDecorations(editor, newParameterDecorations);
|
||||
}
|
||||
|
||||
private get allEditors(): vscode.TextEditor[] {
|
||||
|
@ -85,7 +106,7 @@ class HintsUpdater {
|
|||
);
|
||||
}
|
||||
|
||||
private setDecorations(
|
||||
private setTypeDecorations(
|
||||
editor: vscode.TextEditor,
|
||||
decorations: vscode.DecorationOptions[],
|
||||
) {
|
||||
|
@ -95,6 +116,16 @@ class HintsUpdater {
|
|||
);
|
||||
}
|
||||
|
||||
private setParameterDecorations(
|
||||
editor: vscode.TextEditor,
|
||||
decorations: vscode.DecorationOptions[],
|
||||
) {
|
||||
editor.setDecorations(
|
||||
parameterHintDecorationType,
|
||||
this.enabled ? decorations : [],
|
||||
);
|
||||
}
|
||||
|
||||
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
|
||||
let client = this.ctx.client;
|
||||
if (!client) return null;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue