From 6984d4b630a1c8048616045f9b846db5c7151199 Mon Sep 17 00:00:00 2001 From: omar-dulaimi Date: Thu, 18 Apr 2024 23:53:19 +0300 Subject: [PATCH] implement delete support --- package.json | 10 +++ src/commands/delete-bookmarks-flow.cmd.ts | 71 +++++++++++++++++++ src/commands/load-bookmarks.cmd.ts | 4 +- src/commands/save-current-breakpoints.cmd.ts | 11 +-- src/extension.ts | 11 ++- .../breakpoint-bookmarks.provider.ts | 9 ++- 6 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 src/commands/delete-bookmarks-flow.cmd.ts diff --git a/package.json b/package.json index b723a92..71c9234 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,12 @@ "light": "resources/light/refresh.svg", "dark": "resources/dark/refresh.svg" } + }, + { + "command": "extension.deleteFlow", + "title": "Delete Bookmarks Flow", + "shortTitle": "Delete Flow", + "icon": "$(trash)" } ], "menus": { @@ -53,6 +59,10 @@ { "command": "extension.loadBookmarks", "group": "inline" + }, + { + "command": "extension.deleteFlow", + "group": "inline" } ], "view/title": [ diff --git a/src/commands/delete-bookmarks-flow.cmd.ts b/src/commands/delete-bookmarks-flow.cmd.ts new file mode 100644 index 0000000..cdb18ec --- /dev/null +++ b/src/commands/delete-bookmarks-flow.cmd.ts @@ -0,0 +1,71 @@ +import { readdir, unlink } from "fs/promises"; +import * as path from "path"; +import * as vscode from "vscode"; +import { BreakpointBookmarksProvider } from "../providers/breakpoint-bookmarks.provider"; + +export const deleteBookmarksFlow = + (provider: BreakpointBookmarksProvider) => async (item: any) => { + const workspacePath = vscode.workspace.workspaceFolders?.[0]?.uri + ?.fsPath as string; + + if (!workspacePath) { + vscode.window.showInformationMessage("No workspace opened!"); + return; + } + + const config = vscode.workspace.getConfiguration("breakpointBookmark"); + const saveLocation = config.get("saveLocation") as string; + + const isDirExist = await provider.assureSaveDirectoryExist( + saveLocation, + workspacePath + ); + if (!isDirExist) return; + + const flowsPaths = await readdir( + saveLocation + ? path.join(workspacePath, saveLocation) + : path.join(workspacePath, ".vscode", "breakpoints") + ); + + const foundFilePath = flowsPaths.find((flowPath) => flowPath === item.id); + if (foundFilePath) { + const choice = await vscode.window.showWarningMessage( + "", + { + modal: true, + detail: `Are you sure you want to delete ${item.label}?`, + }, + "Delete" + ); + + if (choice === "Delete") { + const filePath = saveLocation + ? path.join(workspacePath, saveLocation, foundFilePath) + : path.join( + workspacePath, + ".vscode", + "breakpoints", + `${foundFilePath}` + ); + + try { + await unlink(filePath); + vscode.window.showInformationMessage( + `Successfully deleted: ${item.label}` + ); + } catch (error) { + vscode.window.showErrorMessage( + `Failed to delete, reason: ${ + (error as { message: string }).message + }` + ); + } + } + } else { + vscode.window.showErrorMessage( + `Failed to delete, flow not found: ${item.label}` + ); + } + provider.refresh(); + }; diff --git a/src/commands/load-bookmarks.cmd.ts b/src/commands/load-bookmarks.cmd.ts index a8eb20e..e735843 100644 --- a/src/commands/load-bookmarks.cmd.ts +++ b/src/commands/load-bookmarks.cmd.ts @@ -34,7 +34,7 @@ export const loadBookmarks = const flowsPaths = await readdir( saveLocation - ? `${saveLocation}` + ? path.join(workspacePath, saveLocation) : path.join(workspacePath, ".vscode", "breakpoints") ); @@ -48,7 +48,7 @@ export const loadBookmarks = } const filePath = saveLocation - ? `${saveLocation}/${foundFilePath}` + ? path.join(workspacePath, saveLocation, foundFilePath) : path.join( workspacePath, ".vscode", diff --git a/src/commands/save-current-breakpoints.cmd.ts b/src/commands/save-current-breakpoints.cmd.ts index 34fdfea..c86836e 100644 --- a/src/commands/save-current-breakpoints.cmd.ts +++ b/src/commands/save-current-breakpoints.cmd.ts @@ -22,10 +22,11 @@ export const saveCurrentBreakpoints = ); if (!isDirExist) return; - const fileName = await vscode.window.showInputBox({ - title: "Enter file name without extension", - placeHolder: "test express bug", - }); + const fileName = + (await vscode.window.showInputBox({ + title: "Enter file name without extension", + placeHolder: "test express bug", + })) ?? ""; const currentBreakpoints = ( vscode.debug.breakpoints as vscode.SourceBreakpoint[] @@ -52,7 +53,7 @@ export const saveCurrentBreakpoints = }); const filePath = saveLocation - ? `${saveLocation}/${fileName}.json` + ? `${path.join(workspacePath, saveLocation, fileName)}.json` : path.join(workspacePath, ".vscode", "breakpoints", `${fileName}.json`); await writeFile(filePath, JSON.stringify(currentBreakpoints), { diff --git a/src/extension.ts b/src/extension.ts index c8ea290..fc90f41 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,8 +1,9 @@ import * as vscode from "vscode"; -import { BreakpointBookmarksProvider } from "./providers/breakpoint-bookmarks.provider"; -import { saveCurrentBreakpoints } from "./commands/save-current-breakpoints.cmd"; +import { deleteBookmarksFlow } from "./commands/delete-bookmarks-flow.cmd"; import { loadBookmarks } from "./commands/load-bookmarks.cmd"; import { refresh } from "./commands/refresh.cmd"; +import { saveCurrentBreakpoints } from "./commands/save-current-breakpoints.cmd"; +import { BreakpointBookmarksProvider } from "./providers/breakpoint-bookmarks.provider"; export async function activate(context: vscode.ExtensionContext) { vscode.debug.breakpoints; @@ -22,6 +23,12 @@ export async function activate(context: vscode.ExtensionContext) { ) ); + context.subscriptions.push( + vscode.commands.registerCommand("extension.deleteFlow", (item) => + deleteBookmarksFlow(provider)(item) + ) + ); + context.subscriptions.push( vscode.commands.registerCommand("extension.refresh", () => refresh(provider) diff --git a/src/providers/breakpoint-bookmarks.provider.ts b/src/providers/breakpoint-bookmarks.provider.ts index 46439f1..2774406 100644 --- a/src/providers/breakpoint-bookmarks.provider.ts +++ b/src/providers/breakpoint-bookmarks.provider.ts @@ -62,7 +62,7 @@ export class BreakpointBookmarksProvider { const flowsPaths = await readdir( saveLocation - ? `${saveLocation}` + ? path.resolve(workspacePath, saveLocation) : path.join(workspacePath, ".vscode", "breakpoints") ); const treeData = flowsPaths.map((flowPath, index) => ({ @@ -84,13 +84,16 @@ export class BreakpointBookmarksProvider { } return true; } else { - if (!existsSync(saveLocation)) { + const fullPath = path.resolve(workspacePath, saveLocation); + if (!existsSync(fullPath)) { vscode.window.showInformationMessage( "Specified save location does not exist. Please make sure it exists" ); + return false; + } else { + return true; } } - return false; } async refresh() {