rust-analyzer/editors/code/src/inlay_hints.ts

121 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-07-23 13:38:21 +00:00
import * as vscode from 'vscode';
2019-12-30 19:00:46 +00:00
import * as lc from 'vscode-languageclient';
2019-12-30 20:28:38 +00:00
2019-12-31 17:14:00 +00:00
import { Ctx, sendRequestWithRetry } from './ctx';
2019-12-30 19:21:25 +00:00
export function activateInlayHints(ctx: Ctx) {
2019-12-30 20:28:38 +00:00
const hintsUpdater = new HintsUpdater(ctx);
vscode.window.onDidChangeVisibleTextEditors(async _ => {
await hintsUpdater.refresh();
}, ctx.subscriptions);
vscode.workspace.onDidChangeTextDocument(async e => {
if (e.contentChanges.length === 0) return;
if (e.document.languageId !== 'rust') return;
await hintsUpdater.refresh();
}, ctx.subscriptions);
vscode.workspace.onDidChangeConfiguration(_ => {
hintsUpdater.setEnabled(ctx.config.displayInlayHints);
}, ctx.subscriptions);
2019-12-31 17:55:34 +00:00
ctx.onDidRestart(_ => hintsUpdater.setEnabled(ctx.config.displayInlayHints));
2019-12-30 19:21:25 +00:00
}
2019-07-23 13:38:21 +00:00
interface InlayHintsParams {
2019-12-30 19:00:46 +00:00
textDocument: lc.TextDocumentIdentifier;
2019-07-23 13:38:21 +00:00
}
interface InlayHint {
2019-12-30 19:00:46 +00:00
range: vscode.Range;
2019-07-24 16:52:26 +00:00
kind: string;
label: string;
2019-07-23 13:38:21 +00:00
}
const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
after: {
2019-12-31 14:42:05 +00:00
color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
2019-12-09 18:57:55 +00:00
},
2019-07-23 13:38:21 +00:00
});
2019-12-30 19:21:25 +00:00
class HintsUpdater {
2019-12-30 21:53:21 +00:00
private pending: Map<string, vscode.CancellationTokenSource> = new Map();
2019-12-30 20:28:38 +00:00
private ctx: Ctx;
private enabled = true;
2019-07-23 13:38:21 +00:00
2019-12-30 20:28:38 +00:00
constructor(ctx: Ctx) {
this.ctx = ctx;
2019-08-05 19:31:12 +00:00
}
2019-12-30 20:28:38 +00:00
async setEnabled(enabled: boolean) {
if (this.enabled == enabled) return;
this.enabled = enabled;
2019-07-23 13:38:21 +00:00
2019-12-30 20:28:38 +00:00
if (this.enabled) {
await this.refresh();
} else {
this.allEditors.forEach(it => this.setDecorations(it, []));
2019-08-05 19:31:12 +00:00
}
2019-07-23 13:38:21 +00:00
}
2019-12-30 20:28:38 +00:00
async refresh() {
if (!this.enabled) return;
const promises = this.allEditors.map(it => this.refreshEditor(it));
await Promise.all(promises);
2019-07-25 12:17:37 +00:00
}
2019-12-30 20:28:38 +00:00
private async refreshEditor(editor: vscode.TextEditor): Promise<void> {
2019-08-05 19:31:12 +00:00
const newHints = await this.queryHints(editor.document.uri.toString());
2019-12-30 21:53:21 +00:00
if (newHints == null) return;
const newDecorations = newHints.map(hint => ({
2019-12-30 20:28:38 +00:00
range: hint.range,
renderOptions: {
after: {
contentText: `: ${hint.label}`,
2019-12-09 18:57:55 +00:00
},
2019-12-30 20:28:38 +00:00
},
}));
this.setDecorations(editor, newDecorations);
}
private get allEditors(): vscode.TextEditor[] {
return vscode.window.visibleTextEditors.filter(
editor => editor.document.languageId === 'rust',
);
}
private setDecorations(
editor: vscode.TextEditor,
decorations: vscode.DecorationOptions[],
) {
editor.setDecorations(
typeHintDecorationType,
this.enabled ? decorations : [],
);
2019-07-23 13:38:21 +00:00
}
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
2019-12-31 17:14:00 +00:00
let client = this.ctx.client;
2019-12-31 17:55:34 +00:00
if (!client) return null;
2019-07-24 16:52:26 +00:00
const request: InlayHintsParams = {
2019-12-09 18:57:55 +00:00
textDocument: { uri: documentUri },
2019-07-24 16:52:26 +00:00
};
2019-12-30 21:53:21 +00:00
let tokenSource = new vscode.CancellationTokenSource();
let prev = this.pending.get(documentUri);
2019-12-30 22:12:33 +00:00
if (prev) prev.cancel();
2019-12-30 21:53:21 +00:00
this.pending.set(documentUri, tokenSource);
try {
2019-12-31 17:14:00 +00:00
return await sendRequestWithRetry<InlayHint[] | null>(
client,
2019-12-30 21:53:21 +00:00
'rust-analyzer/inlayHints',
request,
tokenSource.token,
);
} finally {
if (!tokenSource.token.isCancellationRequested) {
2019-12-30 22:12:33 +00:00
this.pending.delete(documentUri);
2019-12-30 21:53:21 +00:00
}
}
2019-07-23 13:38:21 +00:00
}
}