From 40f1351cda118aac7865350d9dd9a4fa2cdd78f7 Mon Sep 17 00:00:00 2001 From: Frank Ganske Date: Fri, 12 May 2023 09:56:22 +0200 Subject: [PATCH 1/4] Specification test with begin (#17) --- src/test/spec/checkbox/createCheckbox.spec.ts | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/test/spec/checkbox/createCheckbox.spec.ts b/src/test/spec/checkbox/createCheckbox.spec.ts index ff48214..ebc1e33 100644 --- a/src/test/spec/checkbox/createCheckbox.spec.ts +++ b/src/test/spec/checkbox/createCheckbox.spec.ts @@ -1,7 +1,7 @@ import * as assert from 'assert'; import * as vscode from 'vscode'; import { createCheckbox } from '../../../createCheckbox'; -import { getConfig, getEditor } from '../../../helpers'; +import { getConfig, getDateString, getEditor } from '../../../helpers'; import { setSettingsToDefault } from '../defaultSettings'; describe('create checkboxes', () => { @@ -140,4 +140,32 @@ describe('create checkboxes', () => { assert.strictEqual(content, expectedResult); }); + + it('should create checkbox with begin date', async () => { + // create new document + const newDocument = await vscode.workspace.openTextDocument({ + content: 'this is a text', + language: 'markdown', + }); + await vscode.window.showTextDocument(newDocument); + + // set the cursor to the current line + const editor = getEditor(); + const position = editor.selection.active; + const newCursorPosition = position.with(0, 0); + const newSelection = new vscode.Selection( + newCursorPosition, + newCursorPosition + ); + editor.selection = newSelection; + + await createCheckbox(editor); + + const dateNow = getDateString(new Date()); + const content = editor.document.getText(); + const typeOfBulletPoint = getConfig('typeOfBulletPoint'); + const expectedResult = `${typeOfBulletPoint} [ ] ${dateNow} this is a text`; + + assert.strictEqual(content, expectedResult); + }); }); From 2092e40f6ef3e19f05a57639fabc9c38f7a4ff2a Mon Sep 17 00:00:00 2001 From: Frank Ganske Date: Fri, 12 May 2023 11:01:15 +0200 Subject: [PATCH 2/4] Create Checkbox with date inserted (#17) --- package.json | 5 +++++ src/createCheckbox.ts | 4 +++- src/test/spec/checkbox/createCheckbox.spec.ts | 7 ++++++- src/test/spec/defaultSettings.ts | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 1109e76..3b7ce1f 100644 --- a/package.json +++ b/package.json @@ -167,6 +167,11 @@ "type": "string", "default": "[{date}]", "markdownDescription": "The date template `{date}` is replaced by the actual date." + }, + "markdown-checkbox.dateWhenCreated": { + "type": "boolean", + "default": true, + "description": "Insert the current date along with the created checkbox." } } } diff --git a/src/createCheckbox.ts b/src/createCheckbox.ts index a98139c..6d66238 100644 --- a/src/createCheckbox.ts +++ b/src/createCheckbox.ts @@ -18,9 +18,11 @@ const createCheckboxOfLine = ( const withBulletPoint = helpers.getConfig('withBulletPoint'); const typeOfBulletPoint = helpers.getConfig('typeOfBulletPoint'); const hasBullet = helpers.lineHasBulletPointAlready(line); + const dateWhenCreated = helpers.getConfig('dateWhenCreated'); + const dateNow = helpers.getDateString(new Date()); const checkboxOfLine = helpers.getCheckboxOfLine(line); - const checkboxCharacters = '[ ] '; + const checkboxCharacters = dateWhenCreated ? `[ ] ${dateNow} ` : '[ ] '; return editor.edit((editBuilder: TextEditorEdit) => { if (!checkboxOfLine) { diff --git a/src/test/spec/checkbox/createCheckbox.spec.ts b/src/test/spec/checkbox/createCheckbox.spec.ts index ebc1e33..154be0b 100644 --- a/src/test/spec/checkbox/createCheckbox.spec.ts +++ b/src/test/spec/checkbox/createCheckbox.spec.ts @@ -141,7 +141,7 @@ describe('create checkboxes', () => { assert.strictEqual(content, expectedResult); }); - it('should create checkbox with begin date', async () => { + it('should create checkbox with current date added', async () => { // create new document const newDocument = await vscode.workspace.openTextDocument({ content: 'this is a text', @@ -149,6 +149,11 @@ describe('create checkboxes', () => { }); await vscode.window.showTextDocument(newDocument); + // update config to insert creation date if configured + await vscode.workspace + .getConfiguration('markdown-checkbox') + .update('dateWhenCreated', true); + // set the cursor to the current line const editor = getEditor(); const position = editor.selection.active; diff --git a/src/test/spec/defaultSettings.ts b/src/test/spec/defaultSettings.ts index fd9389d..c15ac0f 100644 --- a/src/test/spec/defaultSettings.ts +++ b/src/test/spec/defaultSettings.ts @@ -15,6 +15,7 @@ export const setSettingsToDefault = async () => { dateWhenChecked: true, showStatusBarItem: true, dateFormat: 'YYYY-MM-DD', + dateWhenCreated: false, }; await Promise.all( Object.entries(defaultSettings).map(async ([key, value]) => { From c97257e3f49c41839d6fb291de9e0197a7c44e5e Mon Sep 17 00:00:00 2001 From: Frank Ganske Date: Fri, 12 May 2023 15:46:42 +0200 Subject: [PATCH 3/4] Avoid multiply creation dates (#17) --- src/createCheckbox.ts | 6 +++- src/test/spec/checkbox/createCheckbox.spec.ts | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/createCheckbox.ts b/src/createCheckbox.ts index 6d66238..03f8a0e 100644 --- a/src/createCheckbox.ts +++ b/src/createCheckbox.ts @@ -22,7 +22,11 @@ const createCheckboxOfLine = ( const dateNow = helpers.getDateString(new Date()); const checkboxOfLine = helpers.getCheckboxOfLine(line); - const checkboxCharacters = dateWhenCreated ? `[ ] ${dateNow} ` : '[ ] '; + const hasDate = helpers + .getPlainLineText(line.text) + .match(/^\d{4}-\d{2}-\d{2} /); + const checkboxCharacters = + dateWhenCreated && !hasDate ? `[ ] ${dateNow} ` : '[ ] '; return editor.edit((editBuilder: TextEditorEdit) => { if (!checkboxOfLine) { diff --git a/src/test/spec/checkbox/createCheckbox.spec.ts b/src/test/spec/checkbox/createCheckbox.spec.ts index 154be0b..c6b80a7 100644 --- a/src/test/spec/checkbox/createCheckbox.spec.ts +++ b/src/test/spec/checkbox/createCheckbox.spec.ts @@ -173,4 +173,36 @@ describe('create checkboxes', () => { assert.strictEqual(content, expectedResult); }); + + it('should not insert creation date twice if exists', async () => { + // create new document + const newDocument = await vscode.workspace.openTextDocument({ + content: '9999-99-99 this is a text', + language: 'markdown', + }); + await vscode.window.showTextDocument(newDocument); + + // update config to insert creation date if configured + await vscode.workspace + .getConfiguration('markdown-checkbox') + .update('dateWhenCreated', true); + + // set the cursor to the current line + const editor = getEditor(); + const position = editor.selection.active; + const newCursorPosition = position.with(0, 0); + const newSelection = new vscode.Selection( + newCursorPosition, + newCursorPosition + ); + editor.selection = newSelection; + + await createCheckbox(editor); + + const content = editor.document.getText(); + const typeOfBulletPoint = getConfig('typeOfBulletPoint'); + const expectedResult = `${typeOfBulletPoint} [ ] 9999-99-99 this is a text`; + + assert.strictEqual(content, expectedResult); + }); }); From 16990074c0faa9e0f37d479c9c7059298a0556a8 Mon Sep 17 00:00:00 2001 From: Frank Ganske Date: Fri, 12 May 2023 18:50:43 +0200 Subject: [PATCH 4/4] Fix: existing bullet point causes duplicates (#17) --- src/createCheckbox.ts | 2 +- src/test/spec/checkbox/createCheckbox.spec.ts | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/createCheckbox.ts b/src/createCheckbox.ts index 03f8a0e..1310761 100644 --- a/src/createCheckbox.ts +++ b/src/createCheckbox.ts @@ -24,7 +24,7 @@ const createCheckboxOfLine = ( const checkboxOfLine = helpers.getCheckboxOfLine(line); const hasDate = helpers .getPlainLineText(line.text) - .match(/^\d{4}-\d{2}-\d{2} /); + .match(/^(?:[+*-]\s)?\d{4}-\d{2}-\d{2} /); const checkboxCharacters = dateWhenCreated && !hasDate ? `[ ] ${dateNow} ` : '[ ] '; diff --git a/src/test/spec/checkbox/createCheckbox.spec.ts b/src/test/spec/checkbox/createCheckbox.spec.ts index c6b80a7..ea49ada 100644 --- a/src/test/spec/checkbox/createCheckbox.spec.ts +++ b/src/test/spec/checkbox/createCheckbox.spec.ts @@ -174,7 +174,7 @@ describe('create checkboxes', () => { assert.strictEqual(content, expectedResult); }); - it('should not insert creation date twice if exists', async () => { + it('should not insert another creation date', async () => { // create new document const newDocument = await vscode.workspace.openTextDocument({ content: '9999-99-99 this is a text', @@ -205,4 +205,35 @@ describe('create checkboxes', () => { assert.strictEqual(content, expectedResult); }); + + it('should not insert another creation date with existing bullet', async () => { + // create new document + const newDocument = await vscode.workspace.openTextDocument({ + content: '- 9999-99-99 this is a text', + language: 'markdown', + }); + await vscode.window.showTextDocument(newDocument); + + // update config to insert creation date if configured + await vscode.workspace + .getConfiguration('markdown-checkbox') + .update('dateWhenCreated', true); + + // set the cursor to the current line + const editor = getEditor(); + const position = editor.selection.active; + const newCursorPosition = position.with(0, 0); + const newSelection = new vscode.Selection( + newCursorPosition, + newCursorPosition + ); + editor.selection = newSelection; + + await createCheckbox(editor); + + const content = editor.document.getText(); + const expectedResult = `- [ ] 9999-99-99 this is a text`; + + assert.strictEqual(content, expectedResult); + }); });