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

180 lines
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';
2020-02-21 14:59:46 +00:00
import { log } 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);
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;
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
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
});
const parameterHintDecorationType = vscode.window.createTextEditorDecorationType({
before: {
color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
2020-02-17 20:09:44 +00:00
},
2020-01-15 10:47:56 +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
.filter(hint => hint.kind === 'TypeHint')
.map(hint => ({
range: hint.range,
renderOptions: {
after: {
contentText: `: ${hint.label}`,
},
2019-12-09 18:57:55 +00:00
},
}));
this.setTypeDecorations(editor, newTypeDecorations);
2020-02-16 23:47:14 +00:00
const newParameterDecorations = newHints
.filter(hint => hint.kind === 'ParameterHint')
.map(hint => ({
range: hint.range,
renderOptions: {
before: {
contentText: `${hint.label}: `,
},
},
}));
this.setParameterDecorations(editor, newParameterDecorations);
2019-12-30 20:28:38 +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
}
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> {
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
};
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
}
}