Skip to content

Commit

Permalink
implement delete support
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-dulaimi committed Apr 18, 2024
1 parent d573dfe commit 6984d4b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 12 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,23 @@
"light": "resources/light/refresh.svg",
"dark": "resources/dark/refresh.svg"
}
},
{
"command": "extension.deleteFlow",
"title": "Delete Bookmarks Flow",
"shortTitle": "Delete Flow",
"icon": "$(trash)"
}
],
"menus": {
"view/item/context": [
{
"command": "extension.loadBookmarks",
"group": "inline"
},
{
"command": "extension.deleteFlow",
"group": "inline"
}
],
"view/title": [
Expand Down
71 changes: 71 additions & 0 deletions src/commands/delete-bookmarks-flow.cmd.ts
Original file line number Diff line number Diff line change
@@ -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();
};
4 changes: 2 additions & 2 deletions src/commands/load-bookmarks.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const loadBookmarks =

const flowsPaths = await readdir(
saveLocation
? `${saveLocation}`
? path.join(workspacePath, saveLocation)
: path.join(workspacePath, ".vscode", "breakpoints")
);

Expand All @@ -48,7 +48,7 @@ export const loadBookmarks =
}

const filePath = saveLocation
? `${saveLocation}/${foundFilePath}`
? path.join(workspacePath, saveLocation, foundFilePath)
: path.join(
workspacePath,
".vscode",
Expand Down
11 changes: 6 additions & 5 deletions src/commands/save-current-breakpoints.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -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), {
Expand Down
11 changes: 9 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions src/providers/breakpoint-bookmarks.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => ({
Expand All @@ -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() {
Expand Down

0 comments on commit 6984d4b

Please sign in to comment.