"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.activate = activate;
exports.deactivate = deactivate;
const vscode = __importStar(require("vscode"));
const model_1 = require("./model");
const provider_1 = require("./provider");
const fileSystemProvider_1 = require("./fileSystemProvider");
const commands_1 = require("./commands");
const functionExtractor_1 = require("./functionExtractor");
const completionProvider_1 = require("./providers/completionProvider");
const hoverProvider_1 = require("./providers/hoverProvider");
const definitionProvider_1 = require("./providers/definitionProvider");
const signatureHelpProvider_1 = require("./providers/signatureHelpProvider");
const onTypeFormattingProvider_1 = require("./providers/onTypeFormattingProvider");
const documentFormattingProvider_1 = require("./providers/documentFormattingProvider");
const codeErrorProvider_1 = require("./providers/codeErrorProvider");
const apiParser_1 = require("./providers/apiParser");
function activate(context) {
    console.log('Squirrel NUT Explorer 正在激活...');
    // 初始化API解析器
    const apiParser = apiParser_1.ApiParser.getInstance();
    // 创建模型和提供者
    const model = new model_1.FileModel();
    const provider = new provider_1.FileProvider(model);
    const functionExtractor = new functionExtractor_1.FunctionExtractor();
    const output = vscode.window.createOutputChannel('Squirrel');
    // 创建自动完成提供者
    const completionProvider = new completionProvider_1.CompletionProvider(functionExtractor.getCacheManager());
    const dotCompletionProvider = new completionProvider_1.DotCompletionProvider();
    // 创建悬停信息提供者
    const hoverProvider = new hoverProvider_1.HoverProvider(functionExtractor.getCacheManager());
    // 创建定义跳转提供者
    const definitionProvider = new definitionProvider_1.DefinitionProvider(functionExtractor.getCacheManager());
    // 创建签名帮助提供者
    const signatureHelpProvider = new signatureHelpProvider_1.SignatureHelpProvider(functionExtractor.getCacheManager());
    // 创建输入时格式化提供者
    const onTypeFormattingProvider = new onTypeFormattingProvider_1.OnTypeFormattingProvider();
    // 创建文档格式化提供者
    const documentFormattingProvider = new documentFormattingProvider_1.DocumentFormattingProvider();
    // 创建代码错误检测提供者
    const codeErrorProvider = new codeErrorProvider_1.CodeErrorProvider();
    const diagnosticCollection = vscode.languages.createDiagnosticCollection('squirrel');
    // 注册树形数据提供者
    vscode.window.registerTreeDataProvider('squirrelExplorerView', provider);
    // 注册自动完成提供者
    context.subscriptions.push(vscode.languages.registerCompletionItemProvider('squirrel', completionProvider, ''), vscode.languages.registerCompletionItemProvider('squirrel', dotCompletionProvider, '.'));
    // 注册悬停信息提供者
    context.subscriptions.push(vscode.languages.registerHoverProvider('squirrel', hoverProvider));
    // 注册定义跳转提供者
    context.subscriptions.push(vscode.languages.registerDefinitionProvider('squirrel', definitionProvider));
    // 注册签名帮助提供者
    context.subscriptions.push(vscode.languages.registerSignatureHelpProvider('squirrel', signatureHelpProvider, '(', ','));
    // 注册输入时格式化提供者
    context.subscriptions.push(vscode.languages.registerOnTypeFormattingEditProvider('squirrel', onTypeFormattingProvider, ')', ';', '}'));
    // 注册文档格式化提供者
    context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider('squirrel', documentFormattingProvider));
    // 注册代码错误检测提供者
    context.subscriptions.push(vscode.languages.registerCodeActionsProvider('squirrel', codeErrorProvider), diagnosticCollection);
    // 注册文档诊断监听器
    context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(event => {
        if (event.document.languageId === 'squirrel') {
            codeErrorProvider_1.CodeErrorProvider.provideDiagnostics(event.document, diagnosticCollection);
        }
    }), vscode.window.onDidChangeActiveTextEditor(editor => {
        if (editor && editor.document.languageId === 'squirrel') {
            codeErrorProvider_1.CodeErrorProvider.provideDiagnostics(editor.document, diagnosticCollection);
        }
    }), vscode.workspace.onDidOpenTextDocument(document => {
        if (document.languageId === 'squirrel') {
            codeErrorProvider_1.CodeErrorProvider.provideDiagnostics(document, diagnosticCollection);
        }
    }));
    // 对所有已打开的squirrel文档进行初始诊断
    vscode.workspace.textDocuments.forEach(document => {
        if (document.languageId === 'squirrel') {
            codeErrorProvider_1.CodeErrorProvider.provideDiagnostics(document, diagnosticCollection);
        }
    });
    // 注册文件系统提供者
    const fileSystemProvider = new fileSystemProvider_1.SquirrelFileSystemProvider(model);
    vscode.workspace.registerFileSystemProvider('squirrel', fileSystemProvider, {
        isCaseSensitive: true
    });
    // 将 fileSystemProvider 添加到 context 以便在 commands 中使用
    global.fileSystemProvider = fileSystemProvider;
    // 注册所有命令
    console.log('正在注册命令...');
    (0, commands_1.registerCommands)(context, model, provider, functionExtractor, output);
    console.log('命令注册完成');
    // 注册工作区配置变更监听
    const configChangeListener = vscode.workspace.onDidChangeConfiguration((e) => {
        if (e.affectsConfiguration('squirrel')) {
            model.updateConfig();
            output.appendLine('[Squirrel] 配置已更新');
        }
    });
    // 注册文件保存事件
    const saveListener = vscode.workspace.onDidSaveTextDocument(async (doc) => {
        if (doc.uri.scheme === 'squirrel') {
            const filePath = doc.uri.path.substring(1); // 去掉开头的 '/'
            // 当文件保存成功后执行UI更新和缓存更新
            output.appendLine(`[Squirrel] 文件 ${filePath} 已保存到服务器`);
            provider.refresh();
            // 更新函数缓存
            await functionExtractor.updateFileCache(model, filePath);
        }
    });
    // 注册文件关闭事件以清理缓存
    const closeListener = vscode.workspace.onDidCloseTextDocument((doc) => {
        if (doc.uri.scheme === 'squirrel') {
            fileSystemProvider.clearFileCache(doc.uri);
        }
    });
    context.subscriptions.push(configChangeListener, saveListener, closeListener);
    output.appendLine('[Squirrel] 扩展激活完成');
    output.show();
    // 显示欢迎信息
    vscode.window.showInformationMessage('Squirrel NUT Explorer 已激活，请点击"连接到 pvfUtility API"开始使用');
}
function deactivate() {
    console.log('Squirrel NUT Explorer 正在停用...');
}
//# sourceMappingURL=extension.js.map