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

112 lines
3 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-30 19:21:25 +00:00
import { Ctx } from './ctx';
export function activateInlayHints(ctx: Ctx) {
2019-12-30 20:28:38 +00:00
const hintsUpdater = new HintsUpdater(ctx);
console.log('activateInlayHints');
2019-12-30 19:21:25 +00:00
2019-12-30 20:28:38 +00:00
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);
// XXX: don't await here;
// Who knows what happens if an exception is thrown here...
hintsUpdater.refresh();
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-09 18:57:55 +00:00
color: new vscode.ThemeColor('ralsp.inlayHint'),
},
2019-07-23 13:38:21 +00:00
});
2019-12-30 19:21:25 +00:00
class HintsUpdater {
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 20:28:38 +00:00
const newDecorations = (newHints ? newHints : []).map(hint => ({
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-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 20:28:38 +00:00
await this.ctx.client.onReady();
return this.ctx.client.sendRequest<InlayHint[] | null>(
'rust-analyzer/inlayHints',
request,
);
2019-07-23 13:38:21 +00:00
}
}