Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OneExplorer] Support multiple node renaming #1586

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@
"command": "one.cfgEditor.setDefaultValues",
"key": "ctrl+shift+/",
"when": "activeCustomEditorId == one.editor.cfg"
},
{
"command": "one.explorer.rename",
"key": "F2",
"when": "OneExplorerView.active && one.explorer:hasSelectedCfg"
}
],
"menus": {
Expand Down
62 changes: 53 additions & 9 deletions src/OneExplorer/OneExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ export class OneNode extends vscode.TreeItem {
super(node.name, collapsibleState);

this.id = node.id;
this.node = node;
this.resourceUri = node.uri;
this.description = true;
this.tooltip = `${this.node.path}`;
Expand Down Expand Up @@ -483,6 +484,7 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {
public static didHideExtra: boolean = false;

public static hasSelectedCfg: boolean = false;
public static selectedCfgs: Node[] = [];

public static register(context: vscode.ExtensionContext) {
const provider = new OneTreeDataProvider(context.extension.extensionKind);
Expand All @@ -492,7 +494,16 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {
showCollapseAll: true,
canSelectMany: true,
});
provider._treeView.onDidChangeSelection(() => {

provider._treeView.onDidChangeSelection((event) => {
const selectedItems = event.selection;
OneTreeDataProvider.hasSelectedCfg = selectedItems.some(
(item) => item.type === NodeType.config
);
OneTreeDataProvider.selectedCfgs = selectedItems.filter(
(item) => item.type === NodeType.config
);

provider.refreshCfgSelection();
});

Expand Down Expand Up @@ -587,9 +598,28 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {
vscode.commands.registerCommand("one.explorer.delete", (node: Node) =>
provider.delete(node)
),
vscode.commands.registerCommand("one.explorer.rename", (node: Node) =>
provider.rename(node)
),
vscode.commands.registerCommand("one.explorer.rename", async () => {
const nodes = provider.getSelectedCfg();
if(nodes === undefined) {
return;
}
//await Promise.all(nodes.map((node) => this.renameSingleFile(node)));


await provider.renameCfgFiles(nodes);
// assert.ok(nodes.length > 0);
// assert.ok(nodes.every((node) => node.type === NodeType.config));


// Rename the files one-by-one
// const promises = nodes.map(async (cfg) => {
// Logger.info("ONE Explorer", "Shortcut", `rename ${cfg.uri.fsPath}`);
// await provider.renameSingleFile(cfg);
// await provider.refresh(cfg.parent);
// return;
// });
// promises.reduce((prev, curr) => prev.then(() => curr), Promise.resolve());
}),
vscode.commands.registerCommand("one.explorer.refactor", (node: Node) =>
provider.refactor(node)
),
Expand Down Expand Up @@ -748,7 +778,7 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {
*/
private askNewName = (node: Node) => {
return vscode.window.showInputBox({
title: "Enter a file name:",
title: `Renaming '${path.basename(node.uri.fsPath)}':`,
value: `${path.basename(node.uri.fsPath)}`,
valueSelection: [
0,
Expand Down Expand Up @@ -787,26 +817,40 @@ export class OneTreeDataProvider implements vscode.TreeDataProvider<Node> {
};

/**
* Rename a file
* Rename a multiple cfg files
* @note Renaming is only allowed for config files as it has no impact on the explorer view.
* @command one.explorer.rename
* @assumption All nodes are config files
* @todo prohibit special characters from new name for security ('..', '*', etc)
*/
async renameCfgFiles(nodes: Node[]): Promise<void> {
assert.ok(nodes.length > 0);
assert.ok(nodes.every((node) => node.type === NodeType.config));

await Promise.all(nodes.map(async (node) => await this.renameSingleFile(node).then(async ()=>await this.refresh(node.parent))));
}


/**
* Rename a single file
* @note Renaming is only allowed for config files as it has no impact on the explorer view.
* @todo prohibit special characters from new name for security ('..', '*', etc)
*/
rename(node: Node): void {
async renameSingleFile(node: Node): Promise<void> {
assert.ok(node.type === NodeType.config);

if (node.type !== NodeType.config) {
return;
}

this.askNewName(node).then((newname) => {
await this.askNewName(node).then(async (newname) => {
if (newname) {
const dirpath = path.dirname(node.uri.fsPath);
const newpath = `${dirpath}/${newname}`;

const edit = new vscode.WorkspaceEdit();
edit.renameFile(node.uri, vscode.Uri.file(newpath));
vscode.workspace.applyEdit(edit);
await vscode.workspace.applyEdit(edit);
}
});
}
Expand Down