1.0.0 初始版
This commit is contained in:
264
dist/functionExtractor.js
vendored
Normal file
264
dist/functionExtractor.js
vendored
Normal file
@@ -0,0 +1,264 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.FunctionExtractor = exports.FunctionCacheManager = exports.PerformanceMonitor = void 0;
|
||||
// 性能监控类
|
||||
class PerformanceMonitor {
|
||||
constructor() {
|
||||
this.extractionTimes = [];
|
||||
this.fileCounts = [];
|
||||
this.functionCounts = [];
|
||||
}
|
||||
// 记录函数提取性能
|
||||
recordExtraction(timeMs, fileCount, functionCount) {
|
||||
this.extractionTimes.push(timeMs);
|
||||
this.fileCounts.push(fileCount);
|
||||
this.functionCounts.push(functionCount);
|
||||
}
|
||||
// 获取平均提取时间
|
||||
getAverageExtractionTime() {
|
||||
if (this.extractionTimes.length === 0)
|
||||
return 0;
|
||||
const sum = this.extractionTimes.reduce((a, b) => a + b, 0);
|
||||
return sum / this.extractionTimes.length;
|
||||
}
|
||||
// 获取平均每文件处理时间
|
||||
getAverageTimePerFile() {
|
||||
if (this.fileCounts.length === 0 || this.extractionTimes.length === 0)
|
||||
return 0;
|
||||
const totalTime = this.extractionTimes.reduce((a, b) => a + b, 0);
|
||||
const totalFiles = this.fileCounts.reduce((a, b) => a + b, 0);
|
||||
return totalFiles > 0 ? totalTime / totalFiles : 0;
|
||||
}
|
||||
// 获取性能统计信息
|
||||
getStats() {
|
||||
return {
|
||||
averageExtractionTime: this.getAverageExtractionTime(),
|
||||
averageTimePerFile: this.getAverageTimePerFile(),
|
||||
totalExtractions: this.extractionTimes.length
|
||||
};
|
||||
}
|
||||
// 清除统计数据
|
||||
clearStats() {
|
||||
this.extractionTimes = [];
|
||||
this.fileCounts = [];
|
||||
this.functionCounts = [];
|
||||
}
|
||||
}
|
||||
exports.PerformanceMonitor = PerformanceMonitor;
|
||||
// 函数缓存管理类
|
||||
class FunctionCacheManager {
|
||||
constructor() {
|
||||
this.functionCache = new Map();
|
||||
this.fileProcessed = new Set();
|
||||
}
|
||||
// 添加函数信息到缓存
|
||||
addFunctions(filePath, functions) {
|
||||
this.functionCache.set(filePath, functions);
|
||||
this.fileProcessed.add(filePath);
|
||||
}
|
||||
// 获取文件的函数信息
|
||||
getFunctions(filePath) {
|
||||
return this.functionCache.get(filePath);
|
||||
}
|
||||
// 检查文件是否已处理
|
||||
isFileProcessed(filePath) {
|
||||
return this.fileProcessed.has(filePath);
|
||||
}
|
||||
// 清除特定文件的缓存
|
||||
clearFileCache(filePath) {
|
||||
this.functionCache.delete(filePath);
|
||||
this.fileProcessed.delete(filePath);
|
||||
}
|
||||
// 清除所有缓存
|
||||
clearAllCache() {
|
||||
this.functionCache.clear();
|
||||
this.fileProcessed.clear();
|
||||
}
|
||||
// 获取缓存统计信息
|
||||
getCacheStats() {
|
||||
let functionCount = 0;
|
||||
for (const functions of this.functionCache.values()) {
|
||||
functionCount += functions.length;
|
||||
}
|
||||
return {
|
||||
fileCount: this.functionCache.size,
|
||||
functionCount: functionCount
|
||||
};
|
||||
}
|
||||
// 获取所有缓存的函数信息
|
||||
getAllFunctions() {
|
||||
const allFunctions = [];
|
||||
for (const functions of this.functionCache.values()) {
|
||||
allFunctions.push(...functions);
|
||||
}
|
||||
return allFunctions;
|
||||
}
|
||||
// 根据函数名称查找函数信息
|
||||
findFunctionsByName(functionName) {
|
||||
const allFunctions = this.getAllFunctions();
|
||||
return allFunctions.filter(func => func.name === functionName);
|
||||
}
|
||||
// 根据文件路径查找函数信息
|
||||
findFunctionsByFilePath(filePath) {
|
||||
return this.getFunctions(filePath) || [];
|
||||
}
|
||||
}
|
||||
exports.FunctionCacheManager = FunctionCacheManager;
|
||||
// 函数提取器类
|
||||
class FunctionExtractor {
|
||||
constructor() {
|
||||
this.isExtracting = false; // 防止重复提取
|
||||
this.cacheManager = new FunctionCacheManager();
|
||||
this.performanceMonitor = new PerformanceMonitor();
|
||||
}
|
||||
// 获取缓存管理器
|
||||
getCacheManager() {
|
||||
return this.cacheManager;
|
||||
}
|
||||
// 获取性能监控器
|
||||
getPerformanceMonitor() {
|
||||
return this.performanceMonitor;
|
||||
}
|
||||
// 检查是否正在提取函数
|
||||
isExtractingFunctions() {
|
||||
return this.isExtracting;
|
||||
}
|
||||
// 从文件内容中提取函数信息
|
||||
extractFunctionsFromFile(filePath, content) {
|
||||
const functions = [];
|
||||
const lines = content.split('\n');
|
||||
// Squirrel语言函数定义的正则表达式
|
||||
// 匹配类似: function functionName(param1, param2, ...)
|
||||
const functionRegex = /^\s*function\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)/;
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
const match = line.match(functionRegex);
|
||||
if (match) {
|
||||
const functionName = match[1];
|
||||
const paramsString = match[2];
|
||||
const parameters = this.parseParameters(paramsString);
|
||||
const functionInfo = {
|
||||
name: functionName,
|
||||
filePath: filePath,
|
||||
lineNumber: i + 1,
|
||||
parameters: parameters,
|
||||
signature: `function ${functionName}(${paramsString})`
|
||||
};
|
||||
functions.push(functionInfo);
|
||||
}
|
||||
}
|
||||
return functions;
|
||||
}
|
||||
// 解析参数字符串
|
||||
parseParameters(paramsString) {
|
||||
if (!paramsString.trim()) {
|
||||
return [];
|
||||
}
|
||||
// 简单参数解析,按逗号分割
|
||||
return paramsString.split(',').map(param => param.trim()).filter(param => param.length > 0);
|
||||
}
|
||||
// 批量提取函数信息
|
||||
async extractFunctionsFromFiles(model, filePaths) {
|
||||
// 防止重复提取
|
||||
if (this.isExtracting) {
|
||||
console.log('函数提取已在进行中,跳过本次请求');
|
||||
return;
|
||||
}
|
||||
this.isExtracting = true;
|
||||
console.log(`开始提取 ${filePaths.length} 个文件中的函数信息`);
|
||||
try {
|
||||
// 分批处理文件,避免一次性处理过多文件
|
||||
const batchSize = 50;
|
||||
for (let i = 0; i < filePaths.length; i += batchSize) {
|
||||
const batch = filePaths.slice(i, i + batchSize);
|
||||
console.log(`正在处理批次 ${Math.floor(i / batchSize) + 1}/${Math.ceil(filePaths.length / batchSize)}: ${batch.length} 个文件`);
|
||||
try {
|
||||
// 并行处理批次中的文件
|
||||
const promises = batch.map(async (filePath) => {
|
||||
// 检查文件是否已处理过
|
||||
if (this.cacheManager.isFileProcessed(filePath)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const entry = model.getFileEntry(filePath);
|
||||
if (!entry) {
|
||||
console.warn(`文件 ${filePath} 不存在于文件列表中`);
|
||||
return;
|
||||
}
|
||||
// 如果文件内容尚未加载,则加载内容
|
||||
let content = entry.content;
|
||||
if (!content) {
|
||||
content = await model.getFileContent(filePath);
|
||||
}
|
||||
if (content) {
|
||||
const functions = this.extractFunctionsFromFile(filePath, content);
|
||||
this.cacheManager.addFunctions(filePath, functions);
|
||||
console.log(`文件 ${filePath} 提取到 ${functions.length} 个函数`);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`处理文件 ${filePath} 时出错:`, error);
|
||||
}
|
||||
});
|
||||
// 等待批次处理完成
|
||||
await Promise.all(promises);
|
||||
console.log(`批次 ${Math.floor(i / batchSize) + 1} 处理完成`);
|
||||
}
|
||||
catch (batchError) {
|
||||
console.error(`处理批次 ${Math.floor(i / batchSize) + 1} 时出错:`, batchError);
|
||||
}
|
||||
}
|
||||
console.log('所有文件的函数信息提取完成');
|
||||
}
|
||||
finally {
|
||||
this.isExtracting = false;
|
||||
}
|
||||
}
|
||||
// 提取所有文件的函数信息
|
||||
async extractAllFunctions(model) {
|
||||
const startTime = Date.now();
|
||||
// 获取所有nut文件路径
|
||||
const filePaths = model.getAllNutFiles();
|
||||
console.log(`准备提取 ${filePaths.length} 个nut文件的函数信息`);
|
||||
if (filePaths.length === 0) {
|
||||
console.log('没有nut文件需要处理');
|
||||
return;
|
||||
}
|
||||
await this.extractFunctionsFromFiles(model, filePaths);
|
||||
// 输出缓存统计信息
|
||||
const stats = this.cacheManager.getCacheStats();
|
||||
const endTime = Date.now();
|
||||
const durationMs = endTime - startTime;
|
||||
const durationSec = durationMs / 1000; // 转换为秒
|
||||
// 记录性能数据
|
||||
this.performanceMonitor.recordExtraction(durationMs, filePaths.length, stats.functionCount);
|
||||
console.log(`函数缓存统计: ${stats.fileCount} 个文件, ${stats.functionCount} 个函数`);
|
||||
console.log(`函数提取完成,耗时: ${durationSec.toFixed(2)} 秒`);
|
||||
// 输出性能统计信息
|
||||
const perfStats = this.performanceMonitor.getStats();
|
||||
console.log(`性能统计: 平均提取时间 ${perfStats.averageExtractionTime.toFixed(2)}ms, 平均每文件处理时间 ${perfStats.averageTimePerFile.toFixed(2)}ms`);
|
||||
}
|
||||
// 更新特定文件的函数缓存
|
||||
async updateFileCache(model, filePath) {
|
||||
console.log(`更新文件 ${filePath} 的函数缓存`);
|
||||
try {
|
||||
// 获取文件内容
|
||||
const content = await model.getFileContent(filePath);
|
||||
if (content) {
|
||||
// 提取函数信息
|
||||
const functions = this.extractFunctionsFromFile(filePath, content);
|
||||
// 更新缓存
|
||||
this.cacheManager.addFunctions(filePath, functions);
|
||||
console.log(`文件 ${filePath} 的函数缓存已更新,提取到 ${functions.length} 个函数`);
|
||||
}
|
||||
else {
|
||||
console.warn(`无法获取文件 ${filePath} 的内容`);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`更新文件 ${filePath} 的函数缓存时出错:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.FunctionExtractor = FunctionExtractor;
|
||||
//# sourceMappingURL=functionExtractor.js.map
|
||||
Reference in New Issue
Block a user