diff --git a/package.json b/package.json index 5ada97e..36328bc 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,10 @@ "command": "environments.add", "title": "Add", "icon": "$(add)" + }, + { + "command": "environments.rename", + "title": "Rename" } ], "menus": { @@ -71,6 +75,10 @@ "when": "viewItem == file", "group": "inline" }, + { + "command": "environments.rename", + "when": "viewItem == file" + }, { "command": "environments.edit", "when": "viewItem == keyValue-string", @@ -147,4 +155,4 @@ "webpack": "^5.92.1", "webpack-cli": "^5.1.4" } -} +} \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 02f95ca..10a0da4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -131,6 +131,42 @@ export function activate(context: vscode.ExtensionContext) { } } ); + + vscode.commands.registerCommand( + "environments.rename", + async (element: EnvironmentFileTreeItem) => { + const newFile = await vscode.window.showInputBox({ + prompt: "Enter the new name for the environment file", + value: element.name, + }); + + if (!newFile) { + return; + } + + try { + await vscode.workspace.fs.rename( + element.uri, + vscode.Uri.joinPath( + element.uri.with({ + path: element.uri.path.substring( + 0, + element.uri.path.lastIndexOf("/") + ), + }), + newFile + ) + ); + + treeDataProvider.refresh(); + vscode.window.showInformationMessage(`File renamed to ${newFile}`); + } catch (error: any) { + vscode.window.showErrorMessage( + `Failed to rename file: ${error.message}` + ); + } + } + ); } // this method is called when your extension is deactivated