gitstack

grindlemire/go-tui code browser

3.5 KB TypeScript 123 lines 2026-01-28 · 4b41e7f raw
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import * as vscode from 'vscode';
import { exec, execSync } from 'child_process';
import {
    LanguageClient,
    LanguageClientOptions,
    ServerOptions,
} from 'vscode-languageclient/node';

let client: LanguageClient | undefined;

const INSTALL_COMMAND = 'go install github.com/grindlemire/go-tui/cmd/tui@latest';

function binaryExists(command: string): boolean {
    try {
        const whichCmd = process.platform === 'win32' ? 'where' : 'which';
        execSync(`${whichCmd} ${command}`, { stdio: 'ignore' });
        return true;
    } catch {
        return false;
    }
}

async function installBinary(): Promise<boolean> {
    return new Promise((resolve) => {
        vscode.window.withProgress(
            {
                location: vscode.ProgressLocation.Notification,
                title: 'Installing GSX language server...',
                cancellable: false,
            },
            async () => {
                return new Promise<void>((progressResolve) => {
                    exec(INSTALL_COMMAND, (error, stdout, stderr) => {
                        if (error) {
                            vscode.window.showErrorMessage(
                                `Failed to install tui: ${stderr || error.message}`
                            );
                            resolve(false);
                        } else {
                            vscode.window.showInformationMessage(
                                'GSX language server installed successfully. Reload window to activate.'
                            );
                            resolve(true);
                        }
                        progressResolve();
                    });
                });
            }
        );
    });
}

async function promptInstall(): Promise<boolean> {
    const selection = await vscode.window.showWarningMessage(
        'GSX language server not found.',
        'Install',
        'Disable LSP'
    );

    if (selection === 'Install') {
        return installBinary();
    } else if (selection === 'Disable LSP') {
        const config = vscode.workspace.getConfiguration('gsx.lsp');
        await config.update('enabled', false, vscode.ConfigurationTarget.Global);
        vscode.window.showInformationMessage(
            'GSX LSP disabled. Re-enable in settings with gsx.lsp.enabled'
        );
    }

    return false;
}

export async function activate(context: vscode.ExtensionContext) {
    const config = vscode.workspace.getConfiguration('gsx.lsp');
    const enabled = config.get<boolean>('enabled', true);

    if (!enabled) {
        return;
    }

    const tuiPath = config.get<string>('path', 'tui');

    if (!binaryExists(tuiPath)) {
        const installed = await promptInstall();
        if (!installed) {
            return;
        }
    }

    const logPath = config.get<string>('logPath', '');
    const args = ['lsp'];
    if (logPath) {
        args.push('-log', logPath);
    }

    const serverOptions: ServerOptions = {
        command: tuiPath,
        args: args,
    };

    const clientOptions: LanguageClientOptions = {
        documentSelector: [{ scheme: 'file', language: 'gsx' }],
        synchronize: {
            fileEvents: vscode.workspace.createFileSystemWatcher('**/*.gsx'),
        },
    };

    client = new LanguageClient(
        'gsxLanguageServer',
        'GSX Language Server',
        serverOptions,
        clientOptions
    );

    client.start();
}

export function deactivate(): Thenable<void> | undefined {
    if (!client) {
        return undefined;
    }
    return client.stop();
}