interface InlayHint {
range: vscode.Range;
- kind: string;
+ kind: "TypeHint" | "ParameterHint";
label: string;
}
});
class HintsUpdater {
- private pending: Map<string, vscode.CancellationTokenSource> = new Map();
+ private pending = new Map<string, vscode.CancellationTokenSource>();
private ctx: Ctx;
private enabled: boolean;
this.enabled = ctx.config.displayInlayHints;
}
- async setEnabled(enabled: boolean) {
+ async setEnabled(enabled: boolean): Promise<void> {
if (this.enabled == enabled) return;
this.enabled = enabled;
if (this.enabled) {
- await this.refresh();
- } else {
- this.allEditors.forEach(it => {
- this.setTypeDecorations(it, []);
- this.setParameterDecorations(it, []);
- });
+ return await this.refresh();
}
+ this.allEditors.forEach(it => {
+ this.setTypeDecorations(it, []);
+ this.setParameterDecorations(it, []);
+ });
}
async refresh() {
if (!this.enabled) return;
- const promises = this.allEditors.map(it => this.refreshEditor(it));
- await Promise.all(promises);
+ await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
+ }
+
+ private get allEditors(): vscode.TextEditor[] {
+ return vscode.window.visibleTextEditors.filter(
+ editor => editor.document.languageId === 'rust',
+ );
}
private async refreshEditor(editor: vscode.TextEditor): Promise<void> {
const newHints = await this.queryHints(editor.document.uri.toString());
if (newHints == null) return;
- const newTypeDecorations = newHints.filter(hint => hint.kind === 'TypeHint')
+
+ const newTypeDecorations = newHints
+ .filter(hint => hint.kind === 'TypeHint')
.map(hint => ({
range: hint.range,
renderOptions: {
}));
this.setTypeDecorations(editor, newTypeDecorations);
- const newParameterDecorations = newHints.filter(hint => hint.kind === 'ParameterHint')
+ const newParameterDecorations = newHints
+ .filter(hint => hint.kind === 'ParameterHint')
.map(hint => ({
range: hint.range,
renderOptions: {
this.setParameterDecorations(editor, newParameterDecorations);
}
- private get allEditors(): vscode.TextEditor[] {
- return vscode.window.visibleTextEditors.filter(
- editor => editor.document.languageId === 'rust',
- );
- }
-
private setTypeDecorations(
editor: vscode.TextEditor,
decorations: vscode.DecorationOptions[],
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
const client = this.ctx.client;
if (!client) return null;
+
const request: InlayHintsParams = {
textDocument: { uri: documentUri },
};
const tokenSource = new vscode.CancellationTokenSource();
- const prev = this.pending.get(documentUri);
- if (prev) prev.cancel();
+ const prevHintsRequest = this.pending.get(documentUri);
+ prevHintsRequest?.cancel();
+
this.pending.set(documentUri, tokenSource);
try {
return await sendRequestWithRetry<InlayHint[] | null>(