Skip to content

Commit ee11037

Browse files
authored
Implement Toggle create checkbox (#33)
* Implement Toggle create checkbox * Fix linting issue
1 parent 9aee5a1 commit ee11037

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/createCheckbox.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Position, TextEditor, TextEditorEdit, TextLine } from 'vscode';
1+
import { Position, Range, TextEditor, TextEditorEdit, TextLine } from 'vscode';
22
import * as helpers from './helpers';
33

44
/** Create a new checkbox at selected lines or the current cursor position */
@@ -19,15 +19,26 @@ const createCheckboxOfLine = (
1919
const typeOfBulletPoint = helpers.getConfig<string>('typeOfBulletPoint');
2020
const hasBullet = helpers.lineHasBulletPointAlready(line);
2121

22-
if (!helpers.getCheckboxOfLine(line)) {
23-
return editor.edit((editBuilder: TextEditorEdit) => {
22+
const checkboxOfLine = helpers.getCheckboxOfLine(line);
23+
const checkboxCharacters = '[ ] ';
24+
25+
return editor.edit((editBuilder: TextEditorEdit) => {
26+
if (!checkboxOfLine) {
2427
editBuilder.insert(
2528
new Position(line.lineNumber, hasBullet.pos),
26-
(withBulletPoint && !hasBullet.bullet ? typeOfBulletPoint + ' ' : '') +
27-
'[ ] '
29+
(withBulletPoint && hasBullet.bullet ? '' : typeOfBulletPoint + ' ') +
30+
checkboxCharacters
2831
);
29-
});
30-
} else {
31-
return Promise.resolve(undefined);
32-
}
32+
} else {
33+
editBuilder.delete(
34+
new Range(
35+
new Position(line.lineNumber, hasBullet.pos),
36+
new Position(
37+
line.lineNumber,
38+
hasBullet.pos + checkboxCharacters.length
39+
)
40+
)
41+
);
42+
}
43+
});
3344
};

0 commit comments

Comments
 (0)