const vscode = require('vscode');
const path = require('path');
const { LanguageClient } = require('vscode-languageclient/node');

let client;

function activate(context) {
    // 寻找 Vort 编译器的打包文件位置
    let serverModule = context.asAbsolutePath(path.join('..', '..', 'dist', 'vort.js'));
    
    // LSP 运行参数，启动 dist/vort.js 并传入 'lsp' 子命令对接
    let serverOptions = {
        run: { module: serverModule, args: ['lsp'] },
        debug: { module: serverModule, args: ['lsp'] }
    };

    let clientOptions = {
        // 绑定该 LSP 服务至 .vort 文件类型
        documentSelector: [{ scheme: 'file', language: 'vort' }],
        synchronize: {
            fileEvents: vscode.workspace.createFileSystemWatcher('**/*.vort')
        }
    };

    client = new LanguageClient(
        'vortLanguageServer',
        'Vort Language Server',
        serverOptions,
        clientOptions
    );

    client.start();
}

function deactivate() {
    if (!client) {
        return undefined;
    }
    return client.stop();
}

module.exports = {
    activate,
    deactivate
};
