diff --git a/package.json b/package.json index 1484f98..2c04981 100644 --- a/package.json +++ b/package.json @@ -168,6 +168,11 @@ "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." + }, "markdown-checkbox.quickPickEscBehavior": { "type": "string", "enum": [ diff --git a/src/createCheckbox.ts b/src/createCheckbox.ts index a98139c..1310761 100644 --- a/src/createCheckbox.ts +++ b/src/createCheckbox.ts @@ -18,9 +18,15 @@ 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 hasDate = helpers + .getPlainLineText(line.text) + .match(/^(?:[+*-]\s)?\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 ff48214..ea49ada 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,100 @@ describe('create checkboxes', () => { assert.strictEqual(content, expectedResult); }); + + it('should create checkbox with current date added', async () => { + // create new document + const newDocument = await vscode.workspace.openTextDocument({ + content: '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 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); + }); + + 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', + 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); + }); + + 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); + }); }); diff --git a/src/test/spec/defaultSettings.ts b/src/test/spec/defaultSettings.ts index 3687895..dd58791 100644 --- a/src/test/spec/defaultSettings.ts +++ b/src/test/spec/defaultSettings.ts @@ -1,28 +1,29 @@ -import * as vscode from 'vscode'; - -/** - * Helper function to set all settings to default values before each test - */ -export const setSettingsToDefault = async () => { - const extensionName = 'markdown-checkbox'; - const defaultSettings = { - checkmark: 'X', - languages: ['markdown'], - withBulletPoint: true, - typeOfBulletPoint: '*', - strikeThroughWhenChecked: true, - italicWhenChecked: true, - dateWhenChecked: true, - showStatusBarItem: true, - dateFormat: 'YYYY-MM-DD', - dateTemplate: '[{date}]', - quickPickEscBehavior: 'doNothing', - }; - await Promise.all( - Object.entries(defaultSettings).map(async ([key, value]) => { - return await vscode.workspace - .getConfiguration(extensionName) - .update(key, value); - }) - ); -}; +import * as vscode from 'vscode'; + +/** + * Helper function to set all settings to default values before each test + */ +export const setSettingsToDefault = async () => { + const extensionName = 'markdown-checkbox'; + const defaultSettings = { + checkmark: 'X', + languages: ['markdown'], + withBulletPoint: true, + typeOfBulletPoint: '*', + strikeThroughWhenChecked: true, + italicWhenChecked: true, + dateWhenChecked: true, + showStatusBarItem: true, + dateFormat: 'YYYY-MM-DD', + dateTemplate: '[{date}]', + quickPickEscBehavior: 'doNothing', + dateWhenCreated: false, + }; + await Promise.all( + Object.entries(defaultSettings).map(async ([key, value]) => { + return await vscode.workspace + .getConfiguration(extensionName) + .update(key, value); + }) + ); +}; \ No newline at end of file