Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert date while creating a checkbox #51

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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 @@ -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."
}
}
}
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} /);
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);
});
});
1 change: 1 addition & 0 deletions src/test/spec/defaultSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => {
Expand Down