Skip to content

Commit

Permalink
添加初始化Y3库快捷操作
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Mar 28, 2024
1 parent 264fc40 commit e335f1b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"type": "string",
"default": "",
"ignoreSync": true,
"pattern": "^.*[\\\\/]Editor.exe$"
"pattern": "(^.*[\\\\/]Editor.exe$)|(^$)"
},
"Y3-Helper.CSVPath": {
"title": "CSV物编路径",
Expand Down
19 changes: 17 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ class Helper {
}

private registerCommandOfInitProject() {
let running = false;
vscode.commands.registerCommand('y3-helper.initProject', async () => {
vscode.window.withProgress({
if (running) {
return;
}
running = true;
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: '正在初始化Y3项目...',
}, async (progress, token) => {
Expand All @@ -66,6 +71,13 @@ class Helper {
let scriptUri = this.env.scriptUri!;
let y3Uri = this.env.y3Uri!;

try {
if ((await vscode.workspace.fs.stat(vscode.Uri.joinPath(y3Uri, '.git'))).type === vscode.FileType.Directory) {
vscode.window.showErrorMessage('此项目已经初始化过了!');
return;
}
} catch {}

try {
let state = await vscode.workspace.fs.stat(y3Uri);
if (state.type === vscode.FileType.Directory) {
Expand All @@ -75,7 +87,7 @@ class Helper {
recursive: true,
useTrash: true,
});
tools.log.info(`已将原有的 ${y3Uri.fsPath} 目录移至回收站`);
vscode.window.showInformationMessage(`已将原有的 ${y3Uri.fsPath} 目录移至回收站`);
} catch (error) {
vscode.window.showErrorMessage(`${y3Uri.fsPath} 已被占用,请手动删除它!`);
return;
Expand Down Expand Up @@ -143,7 +155,10 @@ class Helper {
// 打开项目
this.context.globalState.update("NewProjectPath", scriptUri.fsPath);
await vscode.commands.executeCommand('vscode.openFolder', scriptUri);

this.mainMenu.reload(this.env);
});
running = false;
});
}

Expand Down
42 changes: 33 additions & 9 deletions src/mainMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ interface TreeNodeOptional {
iconPath?: typeof vscode.TreeItem.prototype.iconPath;
collapsibleState?: vscode.TreeItemCollapsibleState;
childs?: TreeNode[];
update?: (node: TreeNode, env: Env) => void;
update?: (node: TreeNode, env: Env) => void | Thenable<void>;
}

class TreeNode extends vscode.TreeItem {
childs?: TreeNode[];
update?: (node: TreeNode, env: Env) => void;
update?: (node: TreeNode, env: Env) => void | Thenable<void>;
constructor(label: string, optional?: TreeNodeOptional) {
super(label, vscode.TreeItemCollapsibleState.None);
if (optional) {
Expand All @@ -27,10 +27,10 @@ class TreeNode extends vscode.TreeItem {

class ViewInExplorerNode extends TreeNode {
constructor(uri: vscode.Uri) {
super('在windows中浏览', {
super('在Windows中浏览', {
command: {
command: 'y3-helper.shell',
title: '在windows中浏览',
title: '在Windows中浏览',
arguments: [
'explorer',
uri.fsPath,
Expand All @@ -52,6 +52,11 @@ class ViewInVSCode extends TreeNode {
]
},
iconPath: new vscode.ThemeIcon('window'),
update: (node, env) => {
if (uri.toString() === vscode.workspace.workspaceFolders?.[0].uri.toString()) {
node.iconPath = new vscode.ThemeIcon('error');
}
},
});
}
}
Expand All @@ -68,6 +73,11 @@ class ViewInNewVSCode extends TreeNode {
]
},
iconPath: new vscode.ThemeIcon('empty-window'),
update: (node, env) => {
if (uri.toString() === vscode.workspace.workspaceFolders?.[0].uri.toString()) {
node.iconPath = new vscode.ThemeIcon('error');
}
},
});
}
}
Expand All @@ -84,7 +94,21 @@ let nodeAction = new TreeNode('操作', {
iconPath: new vscode.ThemeIcon('beaker'),
collapsibleState: vscode.TreeItemCollapsibleState.Expanded,
childs: [
new TreeNode('初始化Y3库'),
new TreeNode('初始化Y3库', {
command: {
command: 'y3-helper.initProject',
title: '初始化Y3库',
},
update: async (node, env) => {
node.iconPath = new vscode.ThemeIcon('cloud-download');
try {
let stat = await vscode.workspace.fs.stat(vscode.Uri.joinPath(env.y3Uri!, '.git'));
if (stat.type === vscode.FileType.Directory) {
node.iconPath = new vscode.ThemeIcon('check');
}
} catch {}
},
}),
new TreeNode('启动游戏', {
command: {
command: 'y3-helper.launchGame',
Expand All @@ -109,7 +133,7 @@ let nodeEnv = new TreeNode('当前环境', {
update: (node, env) => {
node.tooltip = env.editorUri?.fsPath;
node.iconPath = env.editorUri ? new vscode.ThemeIcon('settings') : new vscode.ThemeIcon('error');
node.description = env.editorUri ? undefined : '未找到编辑器';
node.description = env.editorUri ? env.editorUri.fsPath : '未找到编辑器';
node.childs = env.editorUri ? [
new TreeNode('启动编辑器', {
command: {
Expand All @@ -130,7 +154,7 @@ let nodeEnv = new TreeNode('当前环境', {
update: (node, env) => {
node.tooltip = env.scriptUri?.fsPath;
node.iconPath = env.scriptUri ? new vscode.ThemeIcon('book') : new vscode.ThemeIcon('error');
node.description = env.scriptUri ? undefined : '未找到Lua脚本';
node.description = env.scriptUri ? env.scriptUri.fsPath : '未找到Lua脚本';
node.childs = env.scriptUri ? [
new ViewInExplorerNode(env.scriptUri),
new ViewInVSCode(env.scriptUri),
Expand Down Expand Up @@ -166,8 +190,8 @@ class TreeProvider implements vscode.TreeDataProvider<TreeNode> {
return node.childs;
}

getTreeItem(node: TreeNode): TreeNode {
node.update?.(node, this.env);
async getTreeItem(node: TreeNode): Promise<TreeNode> {
await node.update?.(node, this.env);
node.collapsibleState = node.collapsibleState ?? (node.childs ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None);
return node;
}
Expand Down

0 comments on commit e335f1b

Please sign in to comment.