aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/analyzer_status.ts
blob: 6631e8db777accf7cdff822ff3d834afd0bc58bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as vscode from 'vscode';

import { Ctx, Cmd } from '../ctx';

// Shows status of rust-analyzer (for debugging)
export function analyzerStatus(ctx: Ctx): Cmd {
    let poller: NodeJS.Timer | null = null;
    const tdcp = new TextDocumentContentProvider(ctx);

    ctx.pushCleanup(
        vscode.workspace.registerTextDocumentContentProvider(
            'rust-analyzer-status',
            tdcp,
        ),
    );

    ctx.pushCleanup({
        dispose() {
            if (poller != null) {
                clearInterval(poller);
            }
        },
    });

    return async function handle() {
        if (poller == null) {
            poller = setInterval(() => tdcp.eventEmitter.fire(tdcp.uri), 1000);
        }
        const document = await vscode.workspace.openTextDocument(tdcp.uri);
        return vscode.window.showTextDocument(
            document,
            vscode.ViewColumn.Two,
            true,
        );
    };
}

class TextDocumentContentProvider
    implements vscode.TextDocumentContentProvider {
    uri = vscode.Uri.parse('rust-analyzer-status://status');
    eventEmitter = new vscode.EventEmitter<vscode.Uri>();

    constructor(private readonly ctx: Ctx) {
    }

    provideTextDocumentContent(
        _uri: vscode.Uri,
    ): vscode.ProviderResult<string> {
        const editor = vscode.window.activeTextEditor;
        const client = this.ctx.client;
        if (!editor || !client) return '';

        return client.sendRequest<string>(
            'rust-analyzer/analyzerStatus',
            null,
        );
    }

    get onDidChange(): vscode.Event<vscode.Uri> {
        return this.eventEmitter.event;
    }
}