Skip to content

Commit

Permalink
refactor: create common fn TreeViewUtils.promptedForUnsavedResource
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Nov 18, 2024
1 parent e764fbc commit 117a2db
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
23 changes: 2 additions & 21 deletions packages/zowe-explorer/src/trees/dataset/DatasetTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,8 @@ export class DatasetTree extends ZoweTreeProvider<IZoweDatasetTreeNode> implemen
*/
public async rename(node: IZoweDatasetTreeNode): Promise<void> {
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)) {
Expand Down
25 changes: 5 additions & 20 deletions packages/zowe-explorer/src/trees/uss/USSTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,29 +252,14 @@ export class USSTree extends ZoweTreeProvider<IZoweUSSTreeNode> implements Types
*/
public async rename(originalNode: IZoweUSSTreeNode): Promise<void> {
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({
Expand Down
46 changes: 44 additions & 2 deletions packages/zowe-explorer/src/utils/TreeViewUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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<boolean> {
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;
}
}

0 comments on commit 117a2db

Please sign in to comment.