From 9613a875d1407f604aecfdc915b460f2b52fbe54 Mon Sep 17 00:00:00 2001 From: Guillermo Gabarrin Date: Wed, 1 Mar 2023 21:58:53 -0300 Subject: [PATCH] Fix update for note status when using brackets in notes --- src/helpers.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/helpers.ts b/src/helpers.ts index cfd3663..5cf1a83 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -29,7 +29,7 @@ export const saveNoteComment = ( ); thread.comments = [...thread.comments, newComment]; if (firstComment) { - updateNoteStatus(newComment, NoteStatus.TODO); + updateNoteStatus(newComment, NoteStatus.TODO, true); thread.contextValue = uuidv4(); noteMap.set(thread.contextValue, thread); } @@ -46,7 +46,7 @@ export const setNoteStatus = ( remoteDb?: RemoteDb, ) => { // Prepend new status on first note comment - updateNoteStatus(thread.comments[0], status); + updateNoteStatus(thread.comments[0], status, false); // Add note comment about status change saveNoteComment( @@ -59,9 +59,21 @@ export const setNoteStatus = ( ); }; -const updateNoteStatus = (comment: vscode.Comment, status: NoteStatus) => { - // Remove previous status if any - comment.body = comment.body.toString().replace(/^\[.*\] /, ''); +const updateNoteStatus = ( + comment: vscode.Comment, + status: NoteStatus, + firstComment: boolean, +) => { + // Remove previous status if not first comment + if (!firstComment) { + let removed = false; + Object.values(NoteStatus).forEach((noteStatus) => { + if (!removed && comment.body.toString().startsWith(`[${noteStatus}] `)) { + comment.body = comment.body.toString().slice(noteStatus.length + 3); + removed = true; + } + }); + } // Set new status comment.body = `[${status}] ${comment.body}`;