Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": true,
"source.organizeImports": true
},
"eslint.validate": ["typescript"],
"editor.formatOnSave": true
Expand Down
2 changes: 1 addition & 1 deletion src/checkboxStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import * as helpers from './helpers';

export class CheckboxStatus {
private statusBarItem: StatusBarItem;
private statusBarItem!: StatusBarItem;

public updateCheckboxStatus() {
if (!this.statusBarItem) {
Expand Down
13 changes: 7 additions & 6 deletions src/commands/quickPick.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import { getAllCheckboxes, getEditor } from '../helpers';
import Checkbox from '../models/checkbox';
import { Checkbox } from '../models/checkbox';
import { toggleCheckboxOfLine } from '../toggleCheckbox';

/** Register command */
Expand Down Expand Up @@ -35,13 +35,14 @@ const showQuickPickItems = (checkboxes: Checkbox[]) => {
};

/** Handle the actions from the QuickPick. */
const handleQuickPickActions = async (items: vscode.QuickPickItem[]) => {
const handleQuickPickActions = async (
items: vscode.QuickPickItem[] | undefined
) => {
const allCheckboxes: Checkbox[] = getAllCheckboxes();

// get all line numbers that must be checked
const linesToCheck: number[] = items.map((i) =>
parseInt(getLineNumberOfLabel(i.label))
);
const linesToCheck: number[] =
items?.map((i) => parseInt(getLineNumberOfLabel(i.label) ?? '0')) ?? [];

// get all line numbers that must be unchecked
const linesToUncheck = allCheckboxes
Expand All @@ -64,4 +65,4 @@ const handleQuickPickActions = async (items: vscode.QuickPickItem[]) => {
};

// Get the line number out of the label of a quick pick item
const getLineNumberOfLabel = (label: string) => label.match(/\d+/)[0];
const getLineNumberOfLabel = (label: string) => label.match(/\d+/)?.[0];
13 changes: 9 additions & 4 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import { Position, TextEditor, TextLine } from 'vscode';
import Checkbox from '../models/checkbox';
import { Checkbox } from '../models/checkbox';

/** Get the current cursor position */
export const getCursorPosition = (): Position => {
Expand All @@ -9,7 +9,12 @@ export const getCursorPosition = (): Position => {

/** Get the active editor of VS Code */
export const getEditor = (): TextEditor => {
return vscode.window.activeTextEditor;
const editor = vscode.window.activeTextEditor;
if (editor) {
return editor;
} else {
throw new Error('Could not load the VS Code editor instance.');
}
};

/** Give the information if the line has already a bullet point */
Expand All @@ -29,7 +34,7 @@ export const lineHasBulletPointAlready = (
};

/** Get the checkbox of a specific line */
export const getCheckboxOfLine = (line: TextLine): Checkbox => {
export const getCheckboxOfLine = (line: TextLine): Checkbox | undefined => {
const lineText = line.text.toString();
const cbPosition = lineText.indexOf('[ ]');
const cbPositionMarked = lineText.search(new RegExp(/\[.\]/));
Expand Down Expand Up @@ -73,7 +78,7 @@ export const getPlainLineText = (text: string) => {

/** Get the value of a workspace config property */
export const getConfig = <T>(config: string): T =>
vscode.workspace.getConfiguration('markdown-checkbox').get<T>(config);
vscode.workspace.getConfiguration('markdown-checkbox').get<T>(config) as T;

/** Determine whether a given language ID of the configuration is valid to activate this extension. */
export const isActivationLanguageId = (languageId: string): boolean => {
Expand Down
2 changes: 1 addition & 1 deletion src/models/checkbox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Position } from 'vscode';

export default class Checkbox {
export interface Checkbox {
checked: boolean;
position: Position;
text: string;
Expand Down
6 changes: 3 additions & 3 deletions src/test/spec/checkbox/createCheckbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('create checkboxes', () => {
const typeOfBulletPoint = getConfig<string>('typeOfBulletPoint');
const expectedResult = `${typeOfBulletPoint} [ ] this is a text`;

assert.equal(content, expectedResult);
assert.strictEqual(content, expectedResult);
});

it('should be created without new bullet point', async () => {
Expand All @@ -59,7 +59,7 @@ describe('create checkboxes', () => {
const content = editor.document.getText();
const expectedResult = `- [ ] this is a text`;

assert.equal(content, expectedResult);
assert.strictEqual(content, expectedResult);
});

it('should be created with new bullet points', async () => {
Expand All @@ -83,6 +83,6 @@ describe('create checkboxes', () => {
const typeOfBulletPoint = getConfig<string>('typeOfBulletPoint');
const expectedResult = `${typeOfBulletPoint} [ ] this is a text\n${typeOfBulletPoint} [ ] this is a second text\n- [ ] this is a third text`;

assert.equal(content, expectedResult);
assert.strictEqual(content, expectedResult);
});
});
8 changes: 4 additions & 4 deletions src/test/spec/checkbox/toggleCheckbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('toggle checkboxes', () => {
const dateNow = getDateString(new Date());
const expectedResult = `[X] ~~*this is a text*~~ [${dateNow}]\n[X] ~~*this is another text*~~ [${dateNow}]\n[X] ~~*another new line*~~ [${dateNow}]`;

assert.equal(content, expectedResult);
assert.strictEqual(content, expectedResult);
});

it('should be toggled without selection', async () => {
Expand All @@ -45,7 +45,7 @@ describe('toggle checkboxes', () => {
const dateNow = getDateString(new Date());
const expectedResult = `[X] ~~*this is a text*~~ [${dateNow}]\n[ ] this is another text\n[ ] another new line`;

assert.equal(content, expectedResult);
assert.strictEqual(content, expectedResult);
});

it('should be toggled with trailing whitespace', async () => {
Expand All @@ -69,7 +69,7 @@ describe('toggle checkboxes', () => {
const dateNow = getDateString(new Date());
const expectedResult = `[X] ~~*this is a text*~~ [${dateNow}] `;

assert.equal(content, expectedResult);
assert.strictEqual(content, expectedResult);
});

it('should be toggled with configured checkmark', async () => {
Expand Down Expand Up @@ -98,6 +98,6 @@ describe('toggle checkboxes', () => {
const dateNow = getDateString(new Date());
const expectedResult = `[x] ~~*this is a text*~~ [${dateNow}]\n[x] ~~*this is another text*~~ [${dateNow}]\n[x] ~~*another new line*~~ [${dateNow}]`;

assert.equal(content, expectedResult);
assert.strictEqual(content, expectedResult);
});
});
4 changes: 2 additions & 2 deletions src/test/spec/helpers/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as assert from 'assert';
import * as vscode from 'vscode';
import Checkbox from '../../../models/checkbox';
import * as helpers from '../../../helpers';
import { useDefaultSettings } from '..';
import * as helpers from '../../../helpers';
import { Checkbox } from '../../../models/checkbox';

describe('helpers', () => {
beforeEach(async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/toggleCheckbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const markField = (
.match(/[\s\n\r]*$/);
const whitespace = foundTrailingWhitespace?.join('') || '';

if (!lhc.checked && textWithoutCheckbox.length > 0) {
if (lhc && !lhc.checked && textWithoutCheckbox.length > 0) {
let newText = textWithoutCheckbox;

// apply different formats to highlight checked status
Expand All @@ -120,7 +120,7 @@ const markField = (
),
newText
);
} else if (lhc.checked) {
} else if (lhc && lhc.checked) {
let newText = textWithoutCheckbox.replace(/~~/g, '').replace(/\*/g, '');
// remove the date string
if (dateWhenChecked) {
Expand Down
34 changes: 18 additions & 16 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": [
"es6"
],
"sourceMap": true,
"rootDir": "src"
},
"include": [
"src/**/*.ts",
"./node_modules/vscode/vscode.d.ts",
"./node_modules/vscode/lib/*",
]
}
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": ["es6"],
"strict": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"noImplicitAny": true,
"sourceMap": true,
"rootDir": "src"
},
"include": [
"src/**/*.ts",
"./node_modules/vscode/vscode.d.ts",
"./node_modules/vscode/lib/*"
]
}