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);
|
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-08 19:38:29 +00:00
|
|
|
// We pass async function though it will not be awaited when called,
|
|
|
|
// thus Promise rejections won't be handled, but this should never throw in fact...
|
|
|
|
ctx.onDidRestart(async _ => 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;
|
2020-02-16 23:47:14 +00:00
|
|
|
kind: "TypeHint" | "ParameterHint";
|
2019-07-24 16:52:26 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-01-14 17:02:01 +00:00
|
|
|
const parameterHintDecorationType = vscode.window.createTextEditorDecorationType({
|
|
|
|
before: {
|
|
|
|
color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
|
|
|
|
}
|
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-01-01 16:04:37 +00:00
|
|
|
this.enabled = ctx.config.displayInlayHints;
|
2019-08-05 19:31:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 23:47:14 +00:00
|
|
|
async setEnabled(enabled: boolean): Promise<void> {
|
2019-12-30 20:28:38 +00:00
|
|
|
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) {
|
2020-02-16 23:47:14 +00:00
|
|
|
return await this.refresh();
|
2019-08-05 19:31:12 +00:00
|
|
|
}
|
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
|
|
|
|
.filter(hint => hint.kind === 'TypeHint')
|
2020-01-14 17:02:01 +00:00
|
|
|
.map(hint => ({
|
|
|
|
range: hint.range,
|
|
|
|
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
|
|
|
|
.filter(hint => hint.kind === 'ParameterHint')
|
2020-01-14 17:02:01 +00:00
|
|
|
.map(hint => ({
|
|
|
|
range: hint.range,
|
|
|
|
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 : [],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-23 13:38:21 +00:00
|
|
|
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
|
2020-02-02 19:12:59 +00:00
|
|
|
const client = this.ctx.client;
|
2019-12-31 17:55:34 +00:00
|
|
|
if (!client) return null;
|
2020-02-16 23:47:14 +00:00
|
|
|
|
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
|
|
|
};
|
2020-02-02 19:12:59 +00:00
|
|
|
const tokenSource = new vscode.CancellationTokenSource();
|
2020-02-16 23:47:14 +00:00
|
|
|
const prevHintsRequest = this.pending.get(documentUri);
|
|
|
|
prevHintsRequest?.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
|
|
|
}
|
|
|
|
}
|