vscode: refactor analyzer status

This commit is contained in:
veetaha 2020-03-25 21:00:04 +02:00
parent eff1b3fe4d
commit d1721021ef

View file

@ -5,7 +5,7 @@ import { Ctx, Cmd } from '../ctx';
// Shows status of rust-analyzer (for debugging) // Shows status of rust-analyzer (for debugging)
export function analyzerStatus(ctx: Ctx): Cmd { export function analyzerStatus(ctx: Ctx): Cmd {
let poller: NodeJS.Timer | null = null; let poller: NodeJS.Timer | undefined = undefined;
const tdcp = new TextDocumentContentProvider(ctx); const tdcp = new TextDocumentContentProvider(ctx);
ctx.pushCleanup( ctx.pushCleanup(
@ -17,41 +17,32 @@ export function analyzerStatus(ctx: Ctx): Cmd {
ctx.pushCleanup({ ctx.pushCleanup({
dispose() { dispose() {
if (poller != null) { if (poller !== undefined) {
clearInterval(poller); clearInterval(poller);
} }
}, },
}); });
return async function handle() { return async () => {
if (poller == null) { if (poller === undefined) {
poller = setInterval(() => tdcp.eventEmitter.fire(tdcp.uri), 1000); poller = setInterval(() => tdcp.eventEmitter.fire(tdcp.uri), 1000);
} }
const document = await vscode.workspace.openTextDocument(tdcp.uri); const document = await vscode.workspace.openTextDocument(tdcp.uri);
return vscode.window.showTextDocument( return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true);
document,
vscode.ViewColumn.Two,
true,
);
}; };
} }
class TextDocumentContentProvider class TextDocumentContentProvider implements vscode.TextDocumentContentProvider {
implements vscode.TextDocumentContentProvider { readonly uri = vscode.Uri.parse('rust-analyzer-status://status');
uri = vscode.Uri.parse('rust-analyzer-status://status'); readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
constructor(private readonly ctx: Ctx) { constructor(private readonly ctx: Ctx) {
} }
provideTextDocumentContent( provideTextDocumentContent(_uri: vscode.Uri): vscode.ProviderResult<string> {
_uri: vscode.Uri, if (!vscode.window.activeTextEditor) return '';
): vscode.ProviderResult<string> {
const editor = vscode.window.activeTextEditor;
const client = this.ctx.client;
if (!editor || !client) return '';
return client.sendRequest(ra.analyzerStatus, null); return this.ctx.client.sendRequest(ra.analyzerStatus, null);
} }
get onDidChange(): vscode.Event<vscode.Uri> { get onDidChange(): vscode.Event<vscode.Uri> {