1.0.4 将库函数外置,插件文件夹下api-functions.json
This commit is contained in:
@@ -11,10 +11,14 @@ import { SignatureHelpProvider } from './providers/signatureHelpProvider';
|
||||
import { OnTypeFormattingProvider } from './providers/onTypeFormattingProvider';
|
||||
import { DocumentFormattingProvider } from './providers/documentFormattingProvider';
|
||||
import { CodeErrorProvider } from './providers/codeErrorProvider';
|
||||
import { ApiParser } from './providers/apiParser';
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
console.log('Squirrel NUT Explorer 正在激活...');
|
||||
|
||||
// 初始化API解析器
|
||||
const apiParser = ApiParser.getInstance();
|
||||
|
||||
// 创建模型和提供者
|
||||
const model = new FileModel();
|
||||
const provider = new FileProvider(model);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
// API函数信息接口
|
||||
export interface ApiFunction {
|
||||
@@ -53,8 +55,12 @@ export class ApiParser {
|
||||
private functions: ApiFunction[] = [];
|
||||
private classes: ApiClass[] = [];
|
||||
private constants: ApiConstant[] = [];
|
||||
private jsonFilePath: string;
|
||||
|
||||
private constructor() {
|
||||
// 获取扩展路径并设置JSON文件路径
|
||||
const extensionPath = vscode.extensions.getExtension('local.squirrel-nut-explorer')?.extensionPath || __dirname;
|
||||
this.jsonFilePath = path.join(extensionPath, 'api-functions.json');
|
||||
this.initializeApiDocumentation();
|
||||
}
|
||||
|
||||
@@ -68,6 +74,35 @@ export class ApiParser {
|
||||
|
||||
// 初始化API文档
|
||||
private initializeApiDocumentation(): void {
|
||||
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.classes = apiData.classes || [];
|
||||
|
||||
// 加载常量数据
|
||||
this.constants = apiData.constants || [];
|
||||
} else {
|
||||
// 如果JSON文件不存在,使用默认数据并创建文件
|
||||
this.initializeDefaultApiDocumentation();
|
||||
this.saveApiDocumentation();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('读取API函数JSON文件失败:', error);
|
||||
// 如果读取失败,使用默认数据
|
||||
this.initializeDefaultApiDocumentation();
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化默认API文档
|
||||
private initializeDefaultApiDocumentation(): void {
|
||||
// 初始化内置函数
|
||||
this.functions = [
|
||||
{
|
||||
@@ -344,12 +379,6 @@ export class ApiParser {
|
||||
description: '圆周率',
|
||||
category: 'math'
|
||||
},
|
||||
{
|
||||
name: 'E',
|
||||
value: '2.71828',
|
||||
description: '自然常数',
|
||||
category: 'math'
|
||||
},
|
||||
{
|
||||
name: 'true',
|
||||
value: 'true',
|
||||
@@ -371,6 +400,26 @@ export class ApiParser {
|
||||
];
|
||||
}
|
||||
|
||||
// 保存API文档到JSON文件
|
||||
private saveApiDocumentation(): void {
|
||||
try {
|
||||
const apiData = {
|
||||
functions: this.functions,
|
||||
classes: this.classes,
|
||||
constants: this.constants
|
||||
};
|
||||
|
||||
fs.writeFileSync(this.jsonFilePath, JSON.stringify(apiData, null, 2), 'utf8');
|
||||
} catch (error) {
|
||||
console.error('保存API函数JSON文件失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 重新加载API文档
|
||||
public reloadApiDocumentation(): void {
|
||||
this.initializeApiDocumentation();
|
||||
}
|
||||
|
||||
// 获取所有函数
|
||||
public getFunctions(): ApiFunction[] {
|
||||
return this.functions;
|
||||
|
||||
Reference in New Issue
Block a user