506 lines
18 KiB
JavaScript
506 lines
18 KiB
JavaScript
"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.ApiParser = void 0;
|
||
const vscode = __importStar(require("vscode"));
|
||
const fs = __importStar(require("fs"));
|
||
const path = __importStar(require("path"));
|
||
// API文档解析器类
|
||
class ApiParser {
|
||
constructor() {
|
||
this.functions = []; // 普通函数,不自动填写第一个参数
|
||
this.functionEx = []; // 扩展函数,自动填写所有参数
|
||
this.classes = [];
|
||
this.constants = [];
|
||
// 获取扩展路径并设置JSON文件路径
|
||
const extensionPath = vscode.extensions.getExtension('local.squirrel-nut-explorer')?.extensionPath || __dirname;
|
||
this.jsonFilePath = path.join(extensionPath, 'api-functions.json');
|
||
this.initializeApiDocumentation();
|
||
}
|
||
// 获取单例实例
|
||
static getInstance() {
|
||
if (!ApiParser.instance) {
|
||
ApiParser.instance = new ApiParser();
|
||
}
|
||
return ApiParser.instance;
|
||
}
|
||
// 初始化API文档
|
||
initializeApiDocumentation() {
|
||
try {
|
||
// 检查JSON文件是否存在
|
||
if (fs.existsSync(this.jsonFilePath)) {
|
||
// 读取JSON文件
|
||
const jsonData = fs.readFileSync(this.jsonFilePath, 'utf8');
|
||
const apiData = JSON.parse(jsonData);
|
||
// 加载函数数据
|
||
this.functions = apiData.functions || [];
|
||
this.functionEx = apiData.functionEx || [];
|
||
// 加载类数据
|
||
this.classes = apiData.classes || [];
|
||
// 加载常量数据
|
||
this.constants = apiData.constants || [];
|
||
}
|
||
else {
|
||
// 如果JSON文件不存在,使用默认数据并创建文件
|
||
this.initializeDefaultApiDocumentation();
|
||
//this.saveApiDocumentation();
|
||
}
|
||
}
|
||
catch (error) {
|
||
console.error('读取API函数JSON文件失败:', error);
|
||
// 如果读取失败,使用默认数据
|
||
this.initializeDefaultApiDocumentation();
|
||
}
|
||
}
|
||
// 初始化默认API文档
|
||
initializeDefaultApiDocumentation() {
|
||
// 初始化内置函数
|
||
this.functions = [
|
||
{
|
||
name: 'print',
|
||
description: '打印消息到控制台',
|
||
params: [
|
||
{
|
||
name: 'message',
|
||
type: 'string',
|
||
description: '要打印的消息'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'void',
|
||
description: '无返回值'
|
||
}
|
||
},
|
||
{
|
||
name: 'len',
|
||
description: '返回字符串、数组或表的长度',
|
||
params: [
|
||
{
|
||
name: 'obj',
|
||
type: 'string|table|array',
|
||
description: '要计算长度的对象'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'integer',
|
||
description: '对象的长度'
|
||
}
|
||
},
|
||
{
|
||
name: 'type',
|
||
description: '返回对象的类型',
|
||
params: [
|
||
{
|
||
name: 'obj',
|
||
type: 'any',
|
||
description: '要检查类型的对象'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'string',
|
||
description: '对象的类型字符串'
|
||
}
|
||
},
|
||
{
|
||
name: 'clone',
|
||
description: '创建对象的浅拷贝',
|
||
params: [
|
||
{
|
||
name: 'obj',
|
||
type: 'any',
|
||
description: '要克隆的对象'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'any',
|
||
description: '克隆的对象'
|
||
}
|
||
},
|
||
{
|
||
name: 'tostring',
|
||
description: '将对象转换为字符串',
|
||
params: [
|
||
{
|
||
name: 'obj',
|
||
type: 'any',
|
||
description: '要转换的对象'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'string',
|
||
description: '转换后的字符串'
|
||
}
|
||
},
|
||
{
|
||
name: 'tointeger',
|
||
description: '将对象转换为整数',
|
||
params: [
|
||
{
|
||
name: 'obj',
|
||
type: 'any',
|
||
description: '要转换的对象'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'integer',
|
||
description: '转换后的整数'
|
||
}
|
||
},
|
||
{
|
||
name: 'tofloat',
|
||
description: '将对象转换为浮点数',
|
||
params: [
|
||
{
|
||
name: 'obj',
|
||
type: 'any',
|
||
description: '要转换的对象'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'float',
|
||
description: '转换后的浮点数'
|
||
}
|
||
}
|
||
];
|
||
// 初始化内置类
|
||
this.classes = [
|
||
{
|
||
name: 'String',
|
||
description: '字符串类,提供字符串操作方法',
|
||
methods: [
|
||
{
|
||
name: 'len',
|
||
description: '返回字符串长度',
|
||
params: [],
|
||
returns: {
|
||
type: 'integer',
|
||
description: '字符串的长度'
|
||
}
|
||
},
|
||
{
|
||
name: 'slice',
|
||
description: '返回字符串的子串',
|
||
params: [
|
||
{
|
||
name: 'start',
|
||
type: 'integer',
|
||
description: '起始位置'
|
||
},
|
||
{
|
||
name: 'end',
|
||
type: 'integer',
|
||
description: '结束位置(可选)',
|
||
optional: true
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'string',
|
||
description: '子串'
|
||
}
|
||
},
|
||
{
|
||
name: 'find',
|
||
description: '查找子串在字符串中的位置',
|
||
params: [
|
||
{
|
||
name: 'substr',
|
||
type: 'string',
|
||
description: '要查找的子串'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'integer',
|
||
description: '子串的位置,未找到返回-1'
|
||
}
|
||
}
|
||
],
|
||
properties: [
|
||
{
|
||
name: 'length',
|
||
type: 'integer',
|
||
description: '字符串的长度'
|
||
}
|
||
]
|
||
},
|
||
{
|
||
name: 'Array',
|
||
description: '数组类,提供数组操作方法',
|
||
methods: [
|
||
{
|
||
name: 'len',
|
||
description: '返回数组长度',
|
||
params: [],
|
||
returns: {
|
||
type: 'integer',
|
||
description: '数组的长度'
|
||
}
|
||
},
|
||
{
|
||
name: 'append',
|
||
description: '向数组末尾添加元素',
|
||
params: [
|
||
{
|
||
name: 'value',
|
||
type: 'any',
|
||
description: '要添加的元素'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'void',
|
||
description: '无返回值'
|
||
}
|
||
},
|
||
{
|
||
name: 'pop',
|
||
description: '移除并返回数组最后一个元素',
|
||
params: [],
|
||
returns: {
|
||
type: 'any',
|
||
description: '被移除的元素'
|
||
}
|
||
}
|
||
],
|
||
properties: [
|
||
{
|
||
name: 'length',
|
||
type: 'integer',
|
||
description: '数组的长度'
|
||
}
|
||
]
|
||
},
|
||
{
|
||
name: 'Table',
|
||
description: '表类,提供表操作方法',
|
||
methods: [
|
||
{
|
||
name: 'len',
|
||
description: '返回表中键值对的数量',
|
||
params: [],
|
||
returns: {
|
||
type: 'integer',
|
||
description: '键值对的数量'
|
||
}
|
||
},
|
||
{
|
||
name: 'rawget',
|
||
description: '获取指定键的值',
|
||
params: [
|
||
{
|
||
name: 'key',
|
||
type: 'any',
|
||
description: '键'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'any',
|
||
description: '键对应的值'
|
||
}
|
||
},
|
||
{
|
||
name: 'rawset',
|
||
description: '设置指定键的值',
|
||
params: [
|
||
{
|
||
name: 'key',
|
||
type: 'any',
|
||
description: '键'
|
||
},
|
||
{
|
||
name: 'value',
|
||
type: 'any',
|
||
description: '值'
|
||
}
|
||
],
|
||
returns: {
|
||
type: 'void',
|
||
description: '无返回值'
|
||
}
|
||
}
|
||
],
|
||
properties: []
|
||
}
|
||
];
|
||
// 初始化常量
|
||
this.constants = [
|
||
{
|
||
name: 'PI',
|
||
value: '3.14159',
|
||
description: '圆周率',
|
||
category: 'math'
|
||
},
|
||
{
|
||
name: 'true',
|
||
value: 'true',
|
||
description: '布尔真值',
|
||
category: 'boolean'
|
||
},
|
||
{
|
||
name: 'false',
|
||
value: 'false',
|
||
description: '布尔假值',
|
||
category: 'boolean'
|
||
},
|
||
{
|
||
name: 'null',
|
||
value: 'null',
|
||
description: '空值',
|
||
category: 'general'
|
||
}
|
||
];
|
||
}
|
||
// 保存API文档到JSON文件
|
||
saveApiDocumentation() {
|
||
try {
|
||
const apiData = {
|
||
functions: this.functions,
|
||
functionEx: this.functionEx,
|
||
classes: this.classes,
|
||
constants: this.constants
|
||
};
|
||
fs.writeFileSync(this.jsonFilePath, JSON.stringify(apiData, null, 2), 'utf8');
|
||
}
|
||
catch (error) {
|
||
console.error('保存API函数JSON文件失败:', error);
|
||
}
|
||
}
|
||
// 重新加载API文档
|
||
reloadApiDocumentation() {
|
||
this.initializeApiDocumentation();
|
||
}
|
||
// 获取所有普通函数
|
||
getFunctions() {
|
||
return this.functions;
|
||
}
|
||
// 获取所有扩展函数
|
||
getFunctionEx() {
|
||
return this.functionEx;
|
||
}
|
||
// 根据名称获取函数(包括普通函数和扩展函数)
|
||
getFunctionByName(name) {
|
||
// 先在普通函数中查找
|
||
let func = this.functions.find(func => func.name === name);
|
||
if (func) {
|
||
return func;
|
||
}
|
||
// 再在扩展函数中查找
|
||
return this.functionEx.find(func => func.name === name);
|
||
}
|
||
// 检查是否是扩展函数
|
||
isFunctionEx(name) {
|
||
return this.functionEx.some(func => func.name === name);
|
||
}
|
||
// 获取所有类
|
||
getClasses() {
|
||
return this.classes;
|
||
}
|
||
// 根据名称获取类
|
||
getClassByName(name) {
|
||
return this.classes.find(cls => cls.name === name);
|
||
}
|
||
// 获取所有常量
|
||
getConstants() {
|
||
return this.constants;
|
||
}
|
||
// 根据类别获取常量
|
||
getConstantsByCategory(category) {
|
||
return this.constants.filter(constant => constant.category === category);
|
||
}
|
||
// 根据名称获取常量
|
||
getConstantByName(name) {
|
||
return this.constants.find(constant => constant.name === name);
|
||
}
|
||
// 生成函数签名
|
||
generateFunctionSignature(func, isFunctionEx = false) {
|
||
// 检查是否是普通函数且有参数
|
||
if (!isFunctionEx && func.params.length > 0) {
|
||
// 对于普通函数,将第一个参数移到函数名前面
|
||
const firstParam = func.params[0];
|
||
const remainingParams = func.params.slice(1);
|
||
const params = remainingParams.map(param => {
|
||
let paramStr = param.name;
|
||
if (param.optional) {
|
||
paramStr = `[${paramStr}`;
|
||
if (param.defaultValue) {
|
||
paramStr += `=${param.defaultValue}`;
|
||
}
|
||
paramStr += ']';
|
||
}
|
||
return paramStr;
|
||
}).join(', ');
|
||
const paramPart = params ? `(${params})` : '()';
|
||
return `function ${firstParam.name}.${func.name}${paramPart}${func.returns ? `: ${func.returns.type}` : ': void'}`;
|
||
}
|
||
else {
|
||
// 对于扩展函数或无参数的函数,保持原有格式
|
||
const params = func.params.map(param => {
|
||
let paramStr = param.name;
|
||
if (param.optional) {
|
||
paramStr = `[${paramStr}`;
|
||
if (param.defaultValue) {
|
||
paramStr += `=${param.defaultValue}`;
|
||
}
|
||
paramStr += ']';
|
||
}
|
||
return paramStr;
|
||
}).join(', ');
|
||
return `function ${func.name}(${params})${func.returns ? `: ${func.returns.type}` : ': void'}`;
|
||
}
|
||
}
|
||
// 生成类签名
|
||
generateClassSignature(cls) {
|
||
return `class ${cls.name}`;
|
||
}
|
||
// 生成属性签名
|
||
generatePropertySignature(prop) {
|
||
return `property ${prop.name}: ${prop.type}`;
|
||
}
|
||
// 生成方法签名
|
||
generateMethodSignature(method) {
|
||
const params = method.params.map(param => {
|
||
let paramStr = param.name;
|
||
if (param.optional) {
|
||
paramStr = `[${paramStr}`;
|
||
if (param.defaultValue) {
|
||
paramStr += `=${param.defaultValue}`;
|
||
}
|
||
paramStr += ']';
|
||
}
|
||
return paramStr;
|
||
}).join(', ');
|
||
return `function ${method.name}(${params})${method.returns ? `: ${method.returns.type}` : ': void'}`;
|
||
}
|
||
}
|
||
exports.ApiParser = ApiParser;
|
||
//# sourceMappingURL=apiParser.js.map
|