Skip to content

Commit 292d15d

Browse files
committed
Added quick pick to toggle all checkboxes in a document
1 parent cf2c8ab commit 292d15d

11 files changed

+786
-698
lines changed

package-lock.json

+600-587
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+25-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
55
"version": "1.2.0",
66
"publisher": "PKief",
77
"engines": {
8-
"vscode": "^1.4.0"
8+
"vscode": "^1.22.1"
9+
},
10+
"scripts": {
11+
"vscode:prepublish": "npm run compile",
12+
"compile": "tsc -p ./",
13+
"watch": "tsc -watch -p ./",
14+
"postinstall": "node ./node_modules/vscode/bin/install",
15+
"test": "npm run compile && node ./node_modules/vscode/bin/test",
16+
"lint": "tslint ./src/**/*.ts -t stylish"
917
},
1018
"categories": [
1119
"Other"
@@ -26,8 +34,8 @@
2634
},
2735
"activationEvents": [
2836
"onLanguage:markdown",
29-
"onCommand:extension.createCheckbox",
30-
"onCommand:extension.markCheckbox"
37+
"onCommand:markdown-checkbox.createCheckbox",
38+
"onCommand:markdown-checkbox.markCheckbox"
3139
],
3240
"icon": "logo.png",
3341
"galleryBanner": {
@@ -39,23 +47,27 @@
3947
"contributes": {
4048
"commands": [
4149
{
42-
"command": "extension.createCheckbox",
50+
"command": "markdown-checkbox.createCheckbox",
4351
"title": "Markdown: Create checkbox"
4452
},
4553
{
46-
"command": "extension.markCheckbox",
54+
"command": "markdown-checkbox.markCheckbox",
4755
"title": "Markdown: Mark checkbox"
56+
},
57+
{
58+
"command": "markdown-checkbox.showQuickPick",
59+
"title": "Markdown: Pick checkboxes"
4860
}
4961
],
5062
"keybindings": [
5163
{
52-
"command": "extension.createCheckbox",
64+
"command": "markdown-checkbox.createCheckbox",
5365
"key": "ctrl+shift+c",
5466
"mac": "cmd+shift+c",
5567
"when": "editorTextFocus"
5668
},
5769
{
58-
"command": "extension.markCheckbox",
70+
"command": "markdown-checkbox.markCheckbox",
5971
"key": "ctrl+shift+enter",
6072
"mac": "cmd+shift+enter",
6173
"when": "editorTextFocus"
@@ -65,14 +77,14 @@
6577
"editor/context": [
6678
{
6779
"when": "resourceLangId == markdown",
68-
"command": "extension.createCheckbox",
69-
"alt": "extension.createCheckbox",
80+
"command": "markdown-checkbox.createCheckbox",
81+
"alt": "markdown-checkbox.createCheckbox",
7082
"group": "navigation"
7183
},
7284
{
7385
"when": "resourceLangId == markdown",
74-
"command": "extension.markCheckbox",
75-
"alt": "extension.markCheckbox",
86+
"command": "markdown-checkbox.markCheckbox",
87+
"alt": "markdown-checkbox.markCheckbox",
7688
"group": "navigation"
7789
}
7890
]
@@ -119,17 +131,11 @@
119131
}
120132
}
121133
},
122-
"scripts": {
123-
"vscode:prepublish": "npm run compile",
124-
"compile": "tsc -p ./",
125-
"postinstall": "node ./node_modules/vscode/bin/install",
126-
"tslint": "tslint ./src/**/*.ts -t stylish"
127-
},
128134
"devDependencies": {
129135
"@types/mocha": "^5.0.0",
130-
"@types/node": "^9.6.0",
136+
"@types/node": "^9.6.2",
131137
"tslint": "^5.9.1",
132-
"typescript": "^2.7.2",
138+
"typescript": "^2.8.1",
133139
"vscode": "^1.1.14"
134140
}
135141
}

src/checkbox.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ import { Position } from 'vscode';
33
export default class Checkbox {
44
checked: boolean;
55
position: Position;
6-
}
6+
text: string;
7+
lineNumber: number;
8+
}

src/checkboxStatus.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import * as helpers from './helpers';
2-
import { Disposable, StatusBarItem, StatusBarAlignment, window, workspace } from 'vscode';
31
import * as vscode from 'vscode';
2+
import { Disposable, StatusBarAlignment, StatusBarItem, window, workspace } from 'vscode';
3+
import * as helpers from './helpers';
44

55
export class CheckboxStatus {
66
private _statusBarItem: StatusBarItem;
77

88
public updateCheckboxStatus() {
99
if (!this._statusBarItem) {
1010
this._statusBarItem = window.createStatusBarItem(StatusBarAlignment.Right, 200);
11+
this._statusBarItem.command = 'markdown-checkbox.showQuickPick';
1112
}
1213

1314
const editor = window.activeTextEditor;
@@ -17,11 +18,10 @@ export class CheckboxStatus {
1718
}
1819

1920
const doc = editor.document;
20-
2121
const showStatusBarItem = vscode.workspace.getConfiguration('markdown-checkbox').get('showStatusBarItem', true);
2222

23-
if (doc.languageId === "markdown" && showStatusBarItem) {
24-
const allCheckboxes = helpers.getAllCheckboxes(doc);
23+
if (doc.languageId === 'markdown' && showStatusBarItem) {
24+
const allCheckboxes = helpers.getAllCheckboxes();
2525

2626
if (allCheckboxes.length === 0) {
2727
this._statusBarItem.hide();
@@ -66,4 +66,4 @@ export class CheckboxStatusController {
6666
public dispose() {
6767
this._disposable.dispose();
6868
}
69-
}
69+
}

src/commands/createCheckbox.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as vscode from 'vscode';
2+
import { createCheckbox } from '../createCheckbox';
3+
import { getEditor } from '../helpers';
4+
5+
export const createCheckboxCommand = vscode.commands.registerCommand('markdown-checkbox.createCheckbox', () => {
6+
const editor = getEditor();
7+
8+
if (!editor) {
9+
return;
10+
}
11+
12+
const doc = editor.document;
13+
if (doc.languageId === 'markdown') {
14+
createCheckbox(editor);
15+
}
16+
});

src/commands/markCheckbox.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as vscode from 'vscode';
2+
import * as helpers from '../helpers';
3+
import { toggleCheckbox } from '../toggleCheckbox';
4+
5+
export const markCheckboxCommand = vscode.commands.registerCommand('markdown-checkbox.markCheckbox', () => {
6+
const editor = helpers.getEditor();
7+
8+
if (!editor) {
9+
return;
10+
}
11+
12+
const doc = editor.document;
13+
14+
if (doc.languageId === 'markdown') {
15+
toggleCheckbox();
16+
}
17+
});

src/commands/quickpick.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as vscode from 'vscode';
2+
import Checkbox from '../checkbox';
3+
import { getAllCheckboxes, getEditor } from '../helpers';
4+
import { toggleCheckboxOfLine } from '../toggleCheckbox';
5+
6+
/** Register command */
7+
export const showQuickPickCommand = vscode.commands.registerCommand('markdown-checkbox.showQuickPick', () => {
8+
showQuickPick();
9+
});
10+
11+
/** Command to toggle the folder icons. */
12+
const showQuickPick = () => {
13+
const allCheckboxes: Checkbox[] = getAllCheckboxes();
14+
showQuickPickItems(allCheckboxes).then(handleQuickPickActions);
15+
};
16+
17+
/** Show QuickPick items to select prefered configuration for the folder icons. */
18+
const showQuickPickItems = (checkboxes: Checkbox[]) => {
19+
const pickItems: vscode.QuickPickItem[] = checkboxes.map(checkbox => {
20+
return {
21+
description: checkbox.text,
22+
picked: checkbox.checked,
23+
label: 'line ' + checkbox.lineNumber.toString(),
24+
} as vscode.QuickPickItem;
25+
});
26+
return vscode.window.showQuickPick(
27+
pickItems, {
28+
placeHolder: 'Toggle checkboxes',
29+
ignoreFocusOut: false,
30+
matchOnDescription: true,
31+
canPickMany: true,
32+
});
33+
};
34+
35+
/** Handle the actions from the QuickPick. */
36+
const handleQuickPickActions = async (items: vscode.QuickPickItem[]) => {
37+
const allCheckboxes: Checkbox[] = getAllCheckboxes();
38+
39+
// get all line numbers that must be checked
40+
const linesToCheck: number[] = items.map(i => parseInt(getLineNumberOfLabel(i.label)));
41+
42+
// get all line numbers that must be unchecked
43+
const linesToUncheck = allCheckboxes.filter(c => !linesToCheck.some(l => l === c.lineNumber)).map(c => c.lineNumber);
44+
45+
const editor = getEditor();
46+
47+
// check the checked items by the quickpick
48+
for (let lineNumber of linesToCheck) {
49+
const line = editor.document.lineAt(lineNumber);
50+
await toggleCheckboxOfLine(line, true);
51+
}
52+
53+
// uncheck the items that are not returned by the quick pick
54+
for (let lineNumber of linesToUncheck) {
55+
const line = editor.document.lineAt(lineNumber);
56+
await toggleCheckboxOfLine(line, false);
57+
}
58+
};
59+
60+
// Get the line number out of the label of a quick pick item
61+
const getLineNumberOfLabel = (label: string) => label.match(/\d+/)[0];

src/createCheckbox.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
import { Position, TextEditor, TextEditorEdit } from 'vscode';
12
import * as helpers from './helpers';
2-
import * as vscode from 'vscode';
3-
import { Position, TextEditorEdit, TextEditor } from 'vscode';
43

54
/** create a new checkbox at the current cursor position */
65
export const createCheckbox = (editor: TextEditor): any => {
@@ -10,13 +9,13 @@ export const createCheckbox = (editor: TextEditor): any => {
109

1110
const line = editor.document.lineAt(helpers.getCursorPosition().line);
1211
const hasBullet = helpers.lineHasBulletPointAlready(line);
13-
if (helpers.lineHasCheckbox(line) === null) {
12+
if (!helpers.lineHasCheckbox(line)) {
1413
return editor.edit((editBuilder: TextEditorEdit) => {
1514
editBuilder.insert(new Position(
1615
line.lineNumber,
1716
hasBullet.pos
1817
), (withBulletPoint && !hasBullet.bullet ? typeOfBulletPoint + ' ' : '') + '[ ] ');
1918
});
2019
}
21-
return null;
22-
};
20+
return undefined;
21+
};

src/extension.ts

+11-35
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,24 @@
1-
'use strict';
21
import * as vscode from 'vscode';
3-
import Checkbox from './checkbox';
4-
import * as helpers from './helpers';
5-
import { createCheckbox } from './createCheckbox';
6-
import { toggleCheckbox } from './toggleCheckbox';
72
import { CheckboxStatus, CheckboxStatusController } from './checkboxStatus';
3+
import { createCheckboxCommand } from './commands/createCheckbox';
4+
import { markCheckboxCommand } from './commands/markCheckbox';
5+
import { showQuickPickCommand } from './commands/quickpick';
86

97
export const activate = (context: vscode.ExtensionContext) => {
108
// item in the status bar to show checkbox information
119
const checkboxStatus = new CheckboxStatus();
1210
const controller = new CheckboxStatusController(checkboxStatus);
1311

1412
// subscribe to changes to update the status bar
15-
context.subscriptions.push(controller);
1613
context.subscriptions.push(checkboxStatus);
14+
context.subscriptions.push(controller);
1715

18-
const extMarkCheckbox = vscode.commands.registerCommand('extension.markCheckbox', () => {
19-
const editor = helpers.getEditor();
20-
if (!editor) {
21-
return;
22-
}
23-
24-
const doc = editor.document;
25-
26-
if (doc.languageId === "markdown") {
27-
toggleCheckbox();
28-
}
29-
});
30-
31-
const extCreateCheckbox = vscode.commands.registerCommand('extension.createCheckbox', () => {
32-
const editor = helpers.getEditor();
33-
if (!editor) {
34-
return;
35-
}
36-
const doc = editor.document;
37-
if (doc.languageId === "markdown") {
38-
createCheckbox(editor);
39-
}
40-
});
41-
42-
context.subscriptions.push(extMarkCheckbox, extCreateCheckbox);
16+
context.subscriptions.push(
17+
markCheckboxCommand,
18+
createCheckboxCommand,
19+
showQuickPickCommand
20+
);
4321
};
4422

45-
// this method is called when your extension is deactivated
46-
export const deactivate = () => {
47-
// loadStatusBarItem();
48-
};
23+
// this method is called when the extension is deactivated
24+
export const deactivate = () => { };

0 commit comments

Comments
 (0)