Skip to content

Commit

Permalink
支持文件夹模式
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Oct 10, 2024
1 parent 1e76000 commit bef8898
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 14 deletions.
42 changes: 39 additions & 3 deletions src/customDefine/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ type EventArg = {
desc: string;
};

export type Folder = {
name: string;
childs: { [key: string]: Event | Folder };
};

export class Events extends BaseDefine {
constructor() {
super();

this.onDidChange(() => {
this._eventsCache = undefined;
this._folderCache = undefined;
});
}

private _eventsCache?: Event[];
private _folderCache?: Folder;

get watchPattern() {
if (!env.mapUri) {
Expand Down Expand Up @@ -104,9 +111,38 @@ export class Events extends BaseDefine {
}

public async getEvents() {
if (!this._eventsCache) {
this._eventsCache = await this.loadEvents();
return this._eventsCache ??= await this.loadEvents();
}

private async loadEventsFolder() {
let root: Folder = {
name: '<root>',
childs: {},
};

let events = await this.getEvents();

function getFolder(fullPath: string[]) {
let folder = root;
for (const path of fullPath) {
folder.childs[path] ??= {
name: path,
childs: {},
};
folder = folder.childs[path] as Folder;
}
return folder;
}

for (const event of events) {
let folder = getFolder(event.path);
folder.childs[event.name] = event;
}
return this._eventsCache;

return root;
}

public async getEventsFolder() {
return this._folderCache ??= await this.loadEventsFolder();
}
}
71 changes: 60 additions & 11 deletions src/mainMenu/pages/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,84 @@ import { TreeNode } from "../treeNode";
import * as vscode from 'vscode';
import { define } from "../../customDefine";
import { env } from "../../env";
import * as Events from "../../customDefine/event";

export class 自定义事件 extends TreeNode {
constructor() {
let mode = 'list';
super('自定义事件', {
iconPath: new vscode.ThemeIcon('group-by-ref-type'),

command: {
command: 'y3-helper.changeEventMode',
title: '切换显示模式',
},

show: async () => {
await env.mapReady();
return env.projectUri !== undefined;
},

update: async (node) => {
node.childs = (await define.自定义事件.getEvents()).map(event => {
let args = event.args.map(arg => arg.name);
return new TreeNode(event.name, {
iconPath: new vscode.ThemeIcon('symbol-event'),
description: `${event.id.toString()}(${args.join(',')})`,
contextValue: '自定义事件',
data: event.id,
childs: event.args.map(arg => new TreeNode(arg.name, {
description: `${arg.desc}(${arg.luaType})`,
})),
node.description = mode === 'list'
? '平铺模式(点击切换)'
: '文件夹模式(点击切换)';
if (mode === 'list') {
node.childs = (await define.自定义事件.getEvents()).map(event => {
let args = event.args.map(arg => arg.name);
return new TreeNode(event.name, {
iconPath: new vscode.ThemeIcon('symbol-event'),
description: `${event.id.toString()}(${args.join(',')})`,
contextValue: '自定义事件',
data: event.id,
childs: args.length > 0 ? event.args.map(arg => new TreeNode(arg.name, {
description: `${arg.desc}(${arg.luaType})`,
})) : undefined,
});
});
});
}
if (mode === 'tree') {
function makeChilds(folder: Events.Folder) {
return Object.entries(folder.childs).map(([name, event]) => {
if ('childs' in event) {
return new TreeNode(name, {
iconPath: new vscode.ThemeIcon('folder'),
update: async (node) => {
node.childs = makeChilds(event);
}
});
} else {
let args = event.args.map(arg => arg.name);
return new TreeNode(name, {
iconPath: new vscode.ThemeIcon('symbol-event'),
description: `${event.id.toString()}(${args.join(',')})`,
contextValue: '自定义事件',
data: event.id,
childs: args.length > 0 ? event.args.map(arg => new TreeNode(arg.name, {
description: `${arg.desc}(${arg.luaType})`,
})) : undefined,
});
}
});
}

node.childs = makeChilds(await define.自定义事件.getEventsFolder());
}
},
});

define.自定义事件.onDidChange(() => {
this.refresh();
});

vscode.commands.registerCommand('y3-helper.changeEventMode', () => {
if (mode === 'list') {
mode = 'tree';
} else {
mode = 'list';
}
this.refresh();
});
}
};

Expand Down

0 comments on commit bef8898

Please sign in to comment.