"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.OnTypeFormattingProvider = void 0;
const vscode = __importStar(require("vscode"));
const beautifyHelper_1 = require("../utils/beautifyHelper");
/**
 * 输入时格式化提供者类 - 使用 js-beautify.js
 */
class OnTypeFormattingProvider {
    /**
     * 在用户输入特定字符时自动格式化代码
     */
    async provideOnTypeFormattingEdits(document, position, ch, options, token) {
        const edits = [];
        try {
            // 获取文档全文
            const fullText = document.getText();
            // 检查是否是 Squirrel 代码
            if (!(0, beautifyHelper_1.isSquirrelCode)(fullText)) {
                return edits; // 如果不是 Squirrel 代码，不应用格式化
            }
            // 使用 js-beautify 格式化代码
            const formattedText = (0, beautifyHelper_1.beautifyCode)(fullText, options);
            // 如果格式化后的代码与原代码相同，不需要修改
            if (formattedText === fullText) {
                return edits;
            }
            // 创建编辑操作，替换整个文档
            const fullRange = new vscode.Range(document.positionAt(0), document.positionAt(fullText.length));
            edits.push(vscode.TextEdit.replace(fullRange, formattedText));
        }
        catch (error) {
            console.error('输入时格式化失败:', error);
            // 如果格式化失败，返回空编辑数组，保持原代码不变
        }
        return edits;
    }
}
exports.OnTypeFormattingProvider = OnTypeFormattingProvider;
//# sourceMappingURL=onTypeFormattingProvider.js.map