diff --git a/packages/zowe-explorer/src/trees/dataset/DatasetTree.ts b/packages/zowe-explorer/src/trees/dataset/DatasetTree.ts index 45b85755ab..4245104777 100644 --- a/packages/zowe-explorer/src/trees/dataset/DatasetTree.ts +++ b/packages/zowe-explorer/src/trees/dataset/DatasetTree.ts @@ -97,27 +97,8 @@ export class DatasetTree extends ZoweTreeProvider implemen */ public async rename(node: IZoweDatasetTreeNode): Promise { ZoweLogger.trace("DatasetTree.rename called."); - const currentFilePath = node.resourceUri.path; // The user's complete local file path for the node - await Profiles.getInstance().checkCurrentProfile(node.getProfile()); - const openedTextDocuments: readonly vscode.TextDocument[] = vscode.workspace.textDocuments; // Array of all documents open in VS Code - - for (const doc of openedTextDocuments) { - const docIsChild = SharedUtils.checkIfChildPath(currentFilePath, doc.fileName); - if (doc.fileName === currentFilePath || docIsChild === true) { - if (doc.isDirty === true) { - Gui.errorMessage( - vscode.l10n.t({ - message: - "Unable to rename {0} because you have unsaved changes in this data set. " + - "Please save your work before renaming the data set.", - args: [node.label], - comment: ["Node path"], - }), - { vsCodeOpts: { modal: true } } - ); - return; - } - } + if (await TreeViewUtils.promptedForUnsavedResource(node)) { + return; } if (Profiles.getInstance().validProfile === Validation.ValidationType.VALID || !SharedContext.isValidationEnabled(node)) { diff --git a/packages/zowe-explorer/src/trees/uss/USSTree.ts b/packages/zowe-explorer/src/trees/uss/USSTree.ts index a7e74cc951..147dd27491 100644 --- a/packages/zowe-explorer/src/trees/uss/USSTree.ts +++ b/packages/zowe-explorer/src/trees/uss/USSTree.ts @@ -252,29 +252,14 @@ export class USSTree extends ZoweTreeProvider implements Types */ public async rename(originalNode: IZoweUSSTreeNode): Promise { ZoweLogger.trace("USSTree.rename called."); - const currentFilePath = originalNode.resourceUri.path; // The user's complete local file path for the node - const openedTextDocuments: readonly vscode.TextDocument[] = vscode.workspace.textDocuments; // Array of all documents open in VS Code + + if (await TreeViewUtils.promptedForUnsavedResource(originalNode)) { + return; + } + const nodeType = SharedContext.isFolder(originalNode) ? "folder" : "file"; const parentPath = path.dirname(originalNode.fullPath); - // If the USS node or any of its children are locally open with unsaved data, prevent rename until user saves their work. - for (const doc of openedTextDocuments) { - const docIsChild = SharedUtils.checkIfChildPath(currentFilePath, doc.fileName); - if (doc.fileName === currentFilePath || docIsChild === true) { - if (doc.isDirty === true) { - Gui.errorMessage( - vscode.l10n.t({ - message: - "Unable to rename {0} because you have unsaved changes in this {1}. Please save your work before renaming the {1}.", - args: [originalNode.fullPath, nodeType], - comment: ["Node path", "Node type"], - }), - { vsCodeOpts: { modal: true } } - ); - return; - } - } - } const loadedNodes = await this.getAllLoadedItems(); const options: vscode.InputBoxOptions = { prompt: vscode.l10n.t({ diff --git a/packages/zowe-explorer/src/utils/TreeViewUtils.ts b/packages/zowe-explorer/src/utils/TreeViewUtils.ts index c6814ae79b..57a5a5b887 100644 --- a/packages/zowe-explorer/src/utils/TreeViewUtils.ts +++ b/packages/zowe-explorer/src/utils/TreeViewUtils.ts @@ -9,13 +9,15 @@ * */ -import { Types, IZoweTree, IZoweTreeNode, PersistenceSchemaEnum } from "@zowe/zowe-explorer-api"; -import { TreeViewExpansionEvent } from "vscode"; +import { Types, IZoweTree, IZoweTreeNode, PersistenceSchemaEnum, Gui } from "@zowe/zowe-explorer-api"; +import { l10n, TextDocument, TreeViewExpansionEvent, workspace } from "vscode"; import { IconGenerator } from "../icons/IconGenerator"; import type { ZoweTreeProvider } from "../trees/ZoweTreeProvider"; import { ZoweLocalStorage } from "../tools/ZoweLocalStorage"; import { ZoweLogger } from "../tools/ZoweLogger"; import { Profiles } from "../configuration/Profiles"; +import { SharedUtils } from "../trees/shared/SharedUtils"; +import { SharedContext } from "../trees/shared/SharedContext"; export class TreeViewUtils { /** @@ -107,4 +109,44 @@ export class TreeViewUtils { } } } + + /** + * Prompts the user when a file/data set is unsaved in the editor. + * @param node The USS file or data set to check for in the editor. Also checks child paths for the node (for PDS members and inner USS files). + * @returns Whether a child or the resource itself is open with unsaved changes in the editor + */ + public static async promptedForUnsavedResource(node: IZoweTreeNode): Promise { + const currentFilePath = node.resourceUri.fsPath; // The user's complete local file path for the node + await Profiles.getInstance().checkCurrentProfile(node.getProfile()); + const openedTextDocuments: readonly TextDocument[] = workspace.textDocuments; // Array of all documents open in VS Code + + const isUss = SharedContext.isUssNode(node); + let nodeType: string; + if (isUss) { + nodeType = SharedContext.isUssDirectory(node) ? "directory" : "file"; + } else { + nodeType = "data set"; + } + + for (const doc of openedTextDocuments) { + const docIsChild = SharedUtils.checkIfChildPath(currentFilePath, doc.fileName); + if (doc.fileName === currentFilePath || docIsChild === true) { + if (doc.isDirty === true) { + Gui.errorMessage( + l10n.t({ + message: + "Unable to rename {0} because you have unsaved changes in this {1}. " + + "Please save your work before renaming the {1}.", + args: [node.label, nodeType], + comment: ["Node path", "Node type (directory, file or data set)"], + }), + { vsCodeOpts: { modal: true } } + ); + return true; + } + } + } + + return false; + } }