forked from PKief/vscode-markdown-checkbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateCheckbox.ts
44 lines (39 loc) · 1.38 KB
/
createCheckbox.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Position, Range, TextEditor, TextEditorEdit, TextLine } from 'vscode';
import * as helpers from './helpers';
/** Create a new checkbox at selected lines or the current cursor position */
export const createCheckbox = async (editor: TextEditor) => {
const selection = editor.selection;
for (let r = selection.start.line; r <= selection.end.line; r++) {
const line = editor.document.lineAt(r);
await createCheckboxOfLine(editor, line);
}
};
const createCheckboxOfLine = (
editor: TextEditor,
line: TextLine
): Thenable<boolean> => {
const withBulletPoint = helpers.getConfig<boolean>('withBulletPoint');
const typeOfBulletPoint = helpers.getConfig<string>('typeOfBulletPoint');
const hasBullet = helpers.lineHasBulletPointAlready(line);
const checkboxOfLine = helpers.getCheckboxOfLine(line);
const checkboxCharacters = '[ ] ';
return editor.edit((editBuilder: TextEditorEdit) => {
if (!checkboxOfLine) {
editBuilder.insert(
new Position(line.lineNumber, hasBullet.pos),
(withBulletPoint && !hasBullet.bullet ? typeOfBulletPoint + ' ' : '') +
checkboxCharacters
);
} else {
editBuilder.delete(
new Range(
new Position(line.lineNumber, hasBullet.pos),
new Position(
line.lineNumber,
hasBullet.pos + checkboxCharacters.length
)
)
);
}
});
};