Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
8 changes: 7 additions & 1 deletion src/createCheckbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ const createCheckboxOfLine = (
const withBulletPoint = helpers.getConfig<boolean>('withBulletPoint');
const typeOfBulletPoint = helpers.getConfig<string>('typeOfBulletPoint');
const hasBullet = helpers.lineHasBulletPointAlready(line);
const dateWhenCreated = helpers.getConfig<boolean>('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} /);
Copy link

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded regex pattern /^(?:[+*-]\s)?\d{4}-\d{2}-\d{2} / assumes ISO date format, but the code ignores the dateFormat configuration. This creates inconsistency between date insertion and detection logic, potentially causing issues if users have different date formats configured.

Copilot uses AI. Check for mistakes.
const checkboxCharacters =
dateWhenCreated && !hasDate ? `[ ] ${dateNow} ` : '[ ] ';

return editor.edit((editBuilder: TextEditorEdit) => {
if (!checkboxOfLine) {
Expand Down
98 changes: 97 additions & 1 deletion src/test/spec/checkbox/createCheckbox.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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<string>('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<string>('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);
});
});
57 changes: 29 additions & 28 deletions src/test/spec/defaultSettings.ts
Original file line number Diff line number Diff line change
@@ -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,
Copy link

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test default for dateWhenCreated is set to false, but the package.json default is true. This inconsistency could lead to confusion and test scenarios that don't match the actual user experience. Consider aligning the test default with the production default.

Suggested change
dateWhenCreated: false,
dateWhenCreated: true,

Copilot uses AI. Check for mistakes.
};
await Promise.all(
Object.entries(defaultSettings).map(async ([key, value]) => {
return await vscode.workspace
.getConfiguration(extensionName)
.update(key, value);
})
);
};