diff --git a/package.json b/package.json index dd812e9..6cb417d 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,9 @@ }, { "command": "linenote.removeNote", "title": "%command.removeNote.title%" + }, { + "command": "linenote.removeNoteWithConfirmation", + "title": "%command.removeNoteWithConfirmation.title%" }], "menus": { "editor/context": [{ diff --git a/package.nls.json b/package.nls.json index 620bb63..21f46b6 100644 --- a/package.nls.json +++ b/package.nls.json @@ -2,6 +2,7 @@ "command.addNote.title": "Add note at current position", "command.openNote.title": "Open notes at current position", "command.removeNote.title": "Remove the note at current position", + "command.removeNoteWithConfirmation.title": "Remove the note at current position, but confirm first", "configuration.lineColor.description": "Background color of line with notes (name or HEX)", "configuration.rulerColor.description": "Color of the ruler with notes (name or HEX)", "configuration.automaticallyDelete.description": "If it set to true, notes that do not correspond to files are automatically deleted.", diff --git a/src/extension.ts b/src/extension.ts index 2eff061..180c905 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -74,6 +74,29 @@ export const activate = (context: vscode.ExtensionContext) => { ]; }; + const removeNoteImmediately = async (notePath?: string) => { + const editor = vscode.window.activeTextEditor; + if (notePath) { + // remove specified note (when invoked from the hover text) + const note = await Note.fromNotePath(notePath); + await note.remove(); + decorateDebounce(); + } else if (editor) { + // remove one note at current cursor (when invoked from the command palette) + const fsPath = editor.document.uri.fsPath; + if (await isNotePath(fsPath)) { + return; + } + const notes = await getCorrespondingNotes(fsPath); + const [from, to] = getSelectionLineRange(editor); + const note = notes.find(note => note.isOverlapped(from, to)); + if (note) { + await note.remove(); + decorateDebounce(); + } + } + }; + context.subscriptions.push( new vscode.Disposable(() => (disposed = true)), @@ -164,27 +187,21 @@ export const activate = (context: vscode.ExtensionContext) => { vscode.commands.registerCommand( "linenote.removeNote", + removeNoteImmediately + ), + + vscode.commands.registerCommand( + "linenote.removeNoteWithConfirmation", async (notePath?: string) => { - const editor = vscode.window.activeTextEditor; - if (notePath) { - // remove specified note (when invoked from the hover text) - const note = await Note.fromNotePath(notePath); - await note.remove(); - decorateDebounce(); - } else if (editor) { - // remove one note at current cursor (when invoked from the command palette) - const fsPath = editor.document.uri.fsPath; - if (await isNotePath(fsPath)) { - return; - } - const notes = await getCorrespondingNotes(fsPath); - const [from, to] = getSelectionLineRange(editor); - const note = notes.find(note => note.isOverlapped(from, to)); - if (note) { - await note.remove(); - decorateDebounce(); - } + const confirmed = await vscode.window + .showInformationMessage(`Are you sure you'd like to permanently remove this note?`, + ...['Yes', 'No']) === 'Yes'; + + if (!confirmed) { + return; } + + return await removeNoteImmediately(notePath); } ), diff --git a/src/note.ts b/src/note.ts index 2b6bd68..7db6757 100644 --- a/src/note.ts +++ b/src/note.ts @@ -200,12 +200,12 @@ export class Note implements Props { )}` )})`; const remove = `[Remove](${vscode.Uri.parse( - `command:linenote.removeNote?${encodeURIComponent( + `command:linenote.removeNoteWithConfirmation?${encodeURIComponent( JSON.stringify(this.notePath) )}` )})`; - return `${body}\n\n*${path.basename(this.notePath)}* ${edit} ${remove}`; + return `${body}\n\n*${path.basename(this.notePath)}* | ${edit} | ${remove}`; } // remove empty dir recursively