2019-07-23 13:38:21 +00:00
|
|
|
import * as vscode from 'vscode';
|
2020-02-24 22:57:49 +00:00
|
|
|
import * as ra from './rust-analyzer-api';
|
2019-12-30 20:28:38 +00:00
|
|
|
|
2020-02-22 18:58:00 +00:00
|
|
|
import { Ctx } from './ctx';
|
|
|
|
import { log, sendRequestWithRetry } from './util';
|
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);
|
2020-02-05 20:39:47 +00:00
|
|
|
vscode.window.onDidChangeVisibleTextEditors(
|
|
|
|
async _ => hintsUpdater.refresh(),
|
|
|
|
null,
|
|
|
|
ctx.subscriptions
|
|
|
|
);
|
|
|
|
|
|
|
|
vscode.workspace.onDidChangeTextDocument(
|
|
|
|
async event => {
|
2020-02-16 17:30:13 +00:00
|
|
|
if (event.contentChanges.length === 0) return;
|
2020-02-05 20:39:47 +00:00
|
|
|
if (event.document.languageId !== 'rust') return;
|
|
|
|
await hintsUpdater.refresh();
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
ctx.subscriptions
|
|
|
|
);
|
|
|
|
|
|
|
|
vscode.workspace.onDidChangeConfiguration(
|
|
|
|
async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints),
|
|
|
|
null,
|
|
|
|
ctx.subscriptions
|
|
|
|
);
|
2019-12-30 20:28:38 +00:00
|
|
|
|
2020-02-17 11:17:01 +00:00
|
|
|
ctx.pushCleanup({
|
|
|
|
dispose() {
|
2020-02-17 20:09:44 +00:00
|
|
|
hintsUpdater.clear();
|
2020-02-17 11:17:01 +00:00
|
|
|
}
|
2020-02-17 20:09:44 +00:00
|
|
|
});
|
2020-02-17 11:17:01 +00:00
|
|
|
|
2020-02-17 12:40:20 +00:00
|
|
|
// XXX: we don't await this, thus Promise rejections won't be handled, but
|
|
|
|
// this should never throw in fact...
|
2020-02-17 20:09:44 +00:00
|
|
|
void hintsUpdater.setEnabled(ctx.config.displayInlayHints);
|
2019-12-30 19:21:25 +00:00
|
|
|
}
|
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'),
|
2020-02-28 10:30:46 +00:00
|
|
|
fontStyle: "normal",
|
2019-12-09 18:57:55 +00:00
|
|
|
},
|
2019-07-23 13:38:21 +00:00
|
|
|
});
|
|
|
|
|
2020-01-14 17:02:01 +00:00
|
|
|
const parameterHintDecorationType = vscode.window.createTextEditorDecorationType({
|
|
|
|
before: {
|
|
|
|
color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
|
2020-02-28 10:30:46 +00:00
|
|
|
fontStyle: "normal",
|
2020-02-17 20:09:44 +00:00
|
|
|
},
|
2020-01-15 10:47:56 +00:00
|
|
|
});
|
2020-01-14 17:02:01 +00:00
|
|
|
|
2019-12-30 19:21:25 +00:00
|
|
|
class HintsUpdater {
|
2020-02-16 23:47:14 +00:00
|
|
|
private pending = new Map<string, vscode.CancellationTokenSource>();
|
2019-12-30 20:28:38 +00:00
|
|
|
private ctx: Ctx;
|
2020-01-01 16:04:37 +00:00
|
|
|
private enabled: boolean;
|
2019-07-23 13:38:21 +00:00
|
|
|
|
2019-12-30 20:28:38 +00:00
|
|
|
constructor(ctx: Ctx) {
|
|
|
|
this.ctx = ctx;
|
2020-02-17 11:17:01 +00:00
|
|
|
this.enabled = false;
|
2019-08-05 19:31:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 23:47:14 +00:00
|
|
|
async setEnabled(enabled: boolean): Promise<void> {
|
2020-02-21 14:59:46 +00:00
|
|
|
log.debug({ enabled, prev: this.enabled });
|
2020-02-17 11:17:01 +00:00
|
|
|
|
2020-02-17 20:35:37 +00:00
|
|
|
if (this.enabled === enabled) return;
|
2019-12-30 20:28:38 +00:00
|
|
|
this.enabled = enabled;
|
2019-07-23 13:38:21 +00:00
|
|
|
|
2019-12-30 20:28:38 +00:00
|
|
|
if (this.enabled) {
|
2020-02-16 23:47:14 +00:00
|
|
|
return await this.refresh();
|
2020-02-17 11:17:01 +00:00
|
|
|
} else {
|
|
|
|
return this.clear();
|
2019-08-05 19:31:12 +00:00
|
|
|
}
|
2020-02-17 11:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
2020-02-16 23:47:14 +00:00
|
|
|
this.allEditors.forEach(it => {
|
|
|
|
this.setTypeDecorations(it, []);
|
|
|
|
this.setParameterDecorations(it, []);
|
|
|
|
});
|
2019-07-23 13:38:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 20:28:38 +00:00
|
|
|
async refresh() {
|
|
|
|
if (!this.enabled) return;
|
2020-02-16 23:47:14 +00:00
|
|
|
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',
|
|
|
|
);
|
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;
|
2020-02-16 23:47:14 +00:00
|
|
|
|
|
|
|
const newTypeDecorations = newHints
|
2020-02-24 22:57:49 +00:00
|
|
|
.filter(hint => hint.kind === ra.InlayKind.TypeHint)
|
2020-01-14 17:02:01 +00:00
|
|
|
.map(hint => ({
|
2020-02-24 22:57:49 +00:00
|
|
|
range: this.ctx.client.protocol2CodeConverter.asRange(hint.range),
|
2020-01-14 17:02:01 +00:00
|
|
|
renderOptions: {
|
|
|
|
after: {
|
|
|
|
contentText: `: ${hint.label}`,
|
|
|
|
},
|
2019-12-09 18:57:55 +00:00
|
|
|
},
|
2020-01-14 17:02:01 +00:00
|
|
|
}));
|
|
|
|
this.setTypeDecorations(editor, newTypeDecorations);
|
|
|
|
|
2020-02-16 23:47:14 +00:00
|
|
|
const newParameterDecorations = newHints
|
2020-02-24 22:57:49 +00:00
|
|
|
.filter(hint => hint.kind === ra.InlayKind.ParameterHint)
|
2020-01-14 17:02:01 +00:00
|
|
|
.map(hint => ({
|
2020-02-24 22:57:49 +00:00
|
|
|
range: this.ctx.client.protocol2CodeConverter.asRange(hint.range),
|
2020-01-14 17:02:01 +00:00
|
|
|
renderOptions: {
|
|
|
|
before: {
|
|
|
|
contentText: `${hint.label}: `,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
this.setParameterDecorations(editor, newParameterDecorations);
|
2019-12-30 20:28:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 17:02:01 +00:00
|
|
|
private setTypeDecorations(
|
2019-12-30 20:28:38 +00:00
|
|
|
editor: vscode.TextEditor,
|
|
|
|
decorations: vscode.DecorationOptions[],
|
|
|
|
) {
|
|
|
|
editor.setDecorations(
|
|
|
|
typeHintDecorationType,
|
|
|
|
this.enabled ? decorations : [],
|
|
|
|
);
|
2019-07-23 13:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 17:02:01 +00:00
|
|
|
private setParameterDecorations(
|
|
|
|
editor: vscode.TextEditor,
|
|
|
|
decorations: vscode.DecorationOptions[],
|
|
|
|
) {
|
|
|
|
editor.setDecorations(
|
|
|
|
parameterHintDecorationType,
|
|
|
|
this.enabled ? decorations : [],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-24 22:57:49 +00:00
|
|
|
private async queryHints(documentUri: string): Promise<ra.InlayHint[] | null> {
|
2020-02-22 18:58:00 +00:00
|
|
|
this.pending.get(documentUri)?.cancel();
|
2020-02-16 23:47:14 +00:00
|
|
|
|
2020-02-02 19:12:59 +00:00
|
|
|
const tokenSource = new vscode.CancellationTokenSource();
|
2019-12-30 21:53:21 +00:00
|
|
|
this.pending.set(documentUri, tokenSource);
|
2020-02-22 18:58:00 +00:00
|
|
|
|
2020-02-24 22:57:49 +00:00
|
|
|
const request = { textDocument: { uri: documentUri } };
|
2020-02-22 18:58:00 +00:00
|
|
|
|
2020-02-24 22:57:49 +00:00
|
|
|
return sendRequestWithRetry(this.ctx.client, ra.inlayHints, request, tokenSource.token)
|
2020-02-22 18:58:00 +00:00
|
|
|
.catch(_ => null)
|
|
|
|
.finally(() => {
|
|
|
|
if (!tokenSource.token.isCancellationRequested) {
|
|
|
|
this.pending.delete(documentUri);
|
|
|
|
}
|
|
|
|
});
|
2019-07-23 13:38:21 +00:00
|
|
|
}
|
|
|
|
}
|