-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add 'rename' into the view context menu (#353)
Co-authored-by: Sheng Chen <sheche@microsoft.com>
- Loading branch information
Showing
8 changed files
with
91 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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({ | ||
placeHolder: "Input new file name", | ||
value: node.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)) { | ||
newName += path.extname(oldUri); | ||
} | ||
const dirname = path.dirname(oldUri); | ||
return path.join(dirname, newName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters