2019-07-23 16:38:21 +03:00
|
|
|
import * as vscode from 'vscode';
|
2020-05-25 14:56:26 +02:00
|
|
|
import * as ra from './lsp_ext';
|
2019-12-30 21:28:38 +01:00
|
|
|
|
2020-03-07 14:08:08 +02:00
|
|
|
import { Ctx, Disposable } from './ctx';
|
2021-02-09 12:39:40 +02:00
|
|
|
import { sendRequestWithRetry, isRustDocument } from './util';
|
2020-02-29 19:28:26 +02:00
|
|
|
|
2020-03-07 14:08:08 +02:00
|
|
|
export function activateInlayHints(ctx: Ctx) {
|
|
|
|
|
const maybeUpdater = {
|
2021-02-09 12:39:40 +02:00
|
|
|
hintsProvider: null as Disposable | null,
|
|
|
|
|
updateHintsEventEmitter: new vscode.EventEmitter<void>(),
|
|
|
|
|
|
2020-04-11 18:22:13 +03:00
|
|
|
async onConfigChange() {
|
2021-02-09 12:39:40 +02:00
|
|
|
this.dispose();
|
|
|
|
|
|
2020-05-08 09:28:15 +02:00
|
|
|
const anyEnabled = ctx.config.inlayHints.typeHints
|
|
|
|
|
|| ctx.config.inlayHints.parameterHints
|
|
|
|
|
|| ctx.config.inlayHints.chainingHints;
|
|
|
|
|
const enabled = ctx.config.inlayHints.enable && anyEnabled;
|
2021-02-09 12:39:40 +02:00
|
|
|
if (!enabled) return;
|
|
|
|
|
|
|
|
|
|
const event = this.updateHintsEventEmitter.event;
|
|
|
|
|
this.hintsProvider = vscode.languages.registerInlayHintsProvider({ scheme: 'file', language: 'rust' }, new class implements vscode.InlayHintsProvider {
|
|
|
|
|
onDidChangeInlayHints = event;
|
|
|
|
|
async provideInlayHints(document: vscode.TextDocument, range: vscode.Range, token: vscode.CancellationToken): Promise<vscode.InlayHint[]> {
|
|
|
|
|
const request = { textDocument: { uri: document.uri.toString() }, range: { start: range.start, end: range.end } };
|
|
|
|
|
const hints = await sendRequestWithRetry(ctx.client, ra.inlayHints, request, token).catch(_ => null)
|
|
|
|
|
if (hints == null) {
|
|
|
|
|
return [];
|
|
|
|
|
} else {
|
|
|
|
|
return hints;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
2020-05-08 09:28:15 +02:00
|
|
|
|
2021-02-09 12:39:40 +02:00
|
|
|
onDidChangeTextDocument({ contentChanges, document }: vscode.TextDocumentChangeEvent) {
|
|
|
|
|
if (contentChanges.length === 0 || !isRustDocument(document)) return;
|
|
|
|
|
this.updateHintsEventEmitter.fire();
|
2020-02-05 22:39:47 +02:00
|
|
|
},
|
2021-02-09 12:39:40 +02:00
|
|
|
|
2020-03-07 14:08:08 +02:00
|
|
|
dispose() {
|
2021-02-09 12:39:40 +02:00
|
|
|
this.hintsProvider?.dispose();
|
|
|
|
|
this.hintsProvider = null;
|
|
|
|
|
this.updateHintsEventEmitter.dispose();
|
|
|
|
|
},
|
|
|
|
|
}
|
2020-03-07 14:08:08 +02:00
|
|
|
|
|
|
|
|
ctx.pushCleanup(maybeUpdater);
|
2020-02-05 22:39:47 +02:00
|
|
|
|
2021-02-09 12:39:40 +02:00
|
|
|
vscode.workspace.onDidChangeConfiguration(maybeUpdater.onConfigChange, maybeUpdater, ctx.subscriptions);
|
|
|
|
|
vscode.workspace.onDidChangeTextDocument(maybeUpdater.onDidChangeTextDocument, maybeUpdater, ctx.subscriptions);
|
2019-12-30 21:28:38 +01:00
|
|
|
|
2021-02-07 21:27:21 +03:30
|
|
|
maybeUpdater.onConfigChange().catch(console.error);
|
2019-12-30 20:21:25 +01:00
|
|
|
}
|