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

feat: Add 'rename' into the view context menu #353

Merged
merged 8 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@
"command": "java.view.package.moveFileToTrash",
"title": "%contributes.commands.java.view.package.moveFileToTrash%",
"category": "Java"
},
{
"command": "java.view.package.renameFile",
"title": "%contributes.commands.java.view.package.renameFile%",
"category": "Java"
}
],
"configuration": {
Expand Down Expand Up @@ -191,9 +196,16 @@
}
},
"keybindings": [
{
"command": "java.view.package.renameFile",
"key": "F2",
"mac": "enter",
"when": "java:projectManagerActivated && focusedView == javaProjectExplorer"
},
{
"command": "java.view.package.moveFileToTrash",
"key": "delete",
"mac": "cmd+backspace",
"when": "java:projectManagerActivated && focusedView == javaProjectExplorer"
}
],
Expand Down Expand Up @@ -255,6 +267,10 @@
"command": "java.view.package.newPackage",
"when": "false"
},
{
"command": "java.view.package.renameFile",
"when": "false"
},
{
"command": "java.view.package.moveFileToTrash",
"when": "false"
Expand Down Expand Up @@ -360,20 +376,35 @@
"group": "6_copypath@25"
},
{
"command": "java.view.package.moveFileToTrash",
"command": "java.view.package.renameFile",
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot)(?=.*?\\b\\+source\\b)(?=.*?\\b\\+uri\\b)/",
"group": "7_modification@10"
},
{
"command": "java.view.package.moveFileToTrash",
"command": "java.view.package.renameFile",
"when": "view == javaProjectExplorer && viewItem =~ /java:file(?=.*?\\b\\+uri\\b)/",
"group": "7_modification@10"
},
{
"command": "java.view.package.moveFileToTrash",
"command": "java.view.package.renameFile",
"when": "view == javaProjectExplorer && viewItem =~ /java:type(?=.*?\\b\\+uri\\b)/",
"group": "7_modification@10"
},
{
"command": "java.view.package.moveFileToTrash",
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot)(?=.*?\\b\\+source\\b)(?=.*?\\b\\+uri\\b)/",
"group": "7_modification@20"
},
{
"command": "java.view.package.moveFileToTrash",
"when": "view == javaProjectExplorer && viewItem =~ /java:file(?=.*?\\b\\+uri\\b)/",
"group": "7_modification@20"
},
{
"command": "java.view.package.moveFileToTrash",
"when": "view == javaProjectExplorer && viewItem =~ /java:type(?=.*?\\b\\+uri\\b)/",
"group": "7_modification@20"
},
{
"command": "java.view.package.newJavaClass",
"when": "view == javaProjectExplorer && viewItem =~ /java:(package|packageRoot)(?=.*?\\b\\+source\\b)(?=.*?\\b\\+uri\\b)/",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"contributes.commands.java.view.package.copyRelativeFilePath": "Copy Relative Path",
"contributes.commands.java.view.package.newJavaClass": "New Java Class",
"contributes.commands.java.view.package.newPackage": "New Package",
"contributes.commands.java.view.package.renameFile": "Rename",
"contributes.commands.java.view.package.moveFileToTrash": "Delete",
"configuration.java.dependency.showMembers": "Show the members in the explorer",
"configuration.java.dependency.syncWithFolderExplorer": "Synchronize Java Projects explorer selection with folder explorer",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"contributes.commands.java.view.package.copyRelativeFilePath": "复制相对路径",
"contributes.commands.java.view.package.newJavaClass": "创建 Java 类",
"contributes.commands.java.view.package.newPackage": "创建包",
"contributes.commands.java.view.package.renameFile": "重命名",
"contributes.commands.java.view.package.moveFileToTrash": "删除",
"configuration.java.dependency.showMembers": "在 Java 项目管理器中显示成员",
"configuration.java.dependency.syncWithFolderExplorer": "在 Java 项目管理器中同步关联当前打开的文件",
Expand Down
2 changes: 2 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export namespace Commands {

export const VIEW_PACKAGE_NEW_JAVA_PACKAGE = "java.view.package.newPackage";

export const VIEW_PACKAGE_RENAME_FILE = "java.view.package.renameFile";

export const VIEW_PACKAGE_MOVE_FILE_TO_TRASH = "java.view.package.moveFileToTrash";

export const VIEW_PACKAGE_REVEAL_IN_PROJECT_EXPLORER = "java.view.package.revealInProjectExplorer";
Expand Down
1 change: 0 additions & 1 deletion src/explorerCommands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export async function deleteFiles(node: DataNode, selectedNode: ExplorerNode): P
// if command not invoked by context menu, use selected node in explorer
if (!node) {
node = selectedNode as DataNode;
// avoid delete dependency files
if (!isMutable(node)) {
return;
}
Expand Down
43 changes: 43 additions & 0 deletions src/explorerCommands/rename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as path from "path";
import { Uri, window, workspace, WorkspaceEdit } from "vscode";
import { DataNode } from "../views/dataNode";
import { ExplorerNode } from "../views/explorerNode";
import { isMutable } from "./utils";

export async function renameFile(node: DataNode, selectedNode: ExplorerNode): Promise<void> {
// if command not invoked by context menu, use selected node in explorer
if (!node) {
node = selectedNode as DataNode;
if (!isMutable(node)) {
return;
}
}

const newName: string | undefined = await window.showInputBox({
xqzlgy2 marked this conversation as resolved.
Show resolved Hide resolved
placeHolder: "Input new file name",
ignoreFocusOut: true,
});

if (!newName) {
return;
}

const oldFsPath = Uri.parse(node.uri).fsPath;
const renamedFilePath = getRenamedFilePath(oldFsPath, newName);

const workspaceEdit: WorkspaceEdit = new WorkspaceEdit();
workspaceEdit.renameFile(Uri.file(oldFsPath), Uri.file(renamedFilePath));
workspace.applyEdit(workspaceEdit);
}

function getRenamedFilePath(oldUri: string, newName: string): string {
// preserve default file extension if not provided
if (!path.extname(newName)) {
xqzlgy2 marked this conversation as resolved.
Show resolved Hide resolved
newName += path.extname(oldUri);
}
const dirname = path.dirname(oldUri);
return path.join(dirname, newName);
}
1 change: 1 addition & 0 deletions src/explorerCommands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { DataNode } from "../views/dataNode";

export function isMutable(node: DataNode): boolean {
// avoid modify dependency files
const packageExp = /java:(package|packageRoot)(?=.*?\b\+source\b)(?=.*?\b\+uri\b)/;
const fileExp = /java:file(?=.*?\b\+uri\b)/;
const typeExp = /java:type(?=.*?\b\+uri\b)/;
Expand Down
10 changes: 8 additions & 2 deletions src/views/dependencyExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-w
import { Commands } from "../commands";
import { Build } from "../constants";
import { deleteFiles } from "../explorerCommands/delete";
import { renameFile } from "../explorerCommands/rename";
import { isStandardServerReady } from "../extension";
import { Jdtls } from "../java/jdtls";
import { INodeData } from "../java/nodeData";
Expand Down Expand Up @@ -74,10 +75,15 @@ export class DependencyExplorer implements Disposable {
}),
);

context.subscriptions.push(
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_RENAME_FILE, (node: DataNode) => {
renameFile(node, this._dependencyViewer.selection[0]);
}),
);

context.subscriptions.push(
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_MOVE_FILE_TO_TRASH, (node: DataNode) => {
const firstSelectedNode = this._dependencyViewer.selection[0];
deleteFiles(node, firstSelectedNode);
deleteFiles(node, this._dependencyViewer.selection[0]);
}),
);
}
Expand Down