Skip to content

Commit

Permalink
Provide the detick command for removing checkbox leaving text
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasHubelbauer committed May 21, 2018
1 parent 1d080d0 commit 69b0487
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Reveal to-do item checkboxes in the center of the editor after clicking
- The default caused issue with releaving outside of visible range
- The at-top option caused CodeLens for that item to be hidden
- Contribute a detick command for removing just the checkbox leaving the text

## `8.0.0` (2018-05-20)

Expand Down
16 changes: 16 additions & 0 deletions icon/detick.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
"title": "Untick",
"icon": "icon/untick.svg"
},
{
"command": "markdown-todo.detick",
"title": "Detick",
"icon": "icon/detick.svg"
},
{
"command": "markdown-todo.refresh",
"title": "Refresh",
Expand Down Expand Up @@ -107,6 +112,11 @@
"command": "markdown-todo.remove",
"when": "viewItem =~ /todo-(un)?ticked/"
},
{
"group": "inline",
"command": "markdown-todo.detick",
"when": "viewItem =~ /todo-(un)?ticked/"
},
{
"group": "inline",
"command": "markdown-todo.tick",
Expand Down
45 changes: 42 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
context.subscriptions.push(commands.registerCommand('markdown-todo.remove', remove));
context.subscriptions.push(commands.registerCommand('markdown-todo.tick', tick));
context.subscriptions.push(commands.registerCommand('markdown-todo.untick', untick));
context.subscriptions.push(commands.registerCommand('markdown-todo.detick', detick));

context.subscriptions.push(todoTreeDataProvider);
context.subscriptions.push(window.createTreeView('to-do-explorer', { treeDataProvider: todoTreeDataProvider }));
Expand Down Expand Up @@ -67,7 +68,7 @@ async function remove(todoOrPath: Todo | string, ln?: number): Promise<void> {

const textEditor = await window.showTextDocument(Uri.file(path), { preview: true });
const range = textEditor.document.lineAt(line).rangeIncludingLineBreak;
textEditor.revealRange(range, TextEditorRevealType.InCenter);
textEditor.revealRange(range);
await textEditor.edit(editBuilder => {
editBuilder.replace(range, '');
});
Expand All @@ -93,7 +94,7 @@ async function tick(todoOrPath: Todo | string, ln?: number, ind?: string): Promi

const textEditor = await window.showTextDocument(Uri.file(path), { preview: true });
const range = textEditor.document.lineAt(line).range;
textEditor.revealRange(range, TextEditorRevealType.InCenter);
textEditor.revealRange(range);
await textEditor.edit(editBuilder => {
const document = textEditor.document;
// TODO: Verify this won't break with -[ which we I guess support (use indexOf otherwise or improve MarkDownDOM to give this)
Expand Down Expand Up @@ -124,7 +125,7 @@ async function untick(todoOrPath: Todo | string, ln?: number, ind?: string): Pro

const textEditor = await window.showTextDocument(Uri.file(path), { preview: true });
const range = textEditor.document.lineAt(line).range;
textEditor.revealRange(range, TextEditorRevealType.InCenter);
textEditor.revealRange(range);
await textEditor.edit(editBuilder => {
const document = textEditor.document;
// TODO: Verify this won't break with -[ which we I guess support (use indexOf otherwise or improve MarkDownDOM to give this)
Expand All @@ -137,6 +138,37 @@ async function untick(todoOrPath: Todo | string, ln?: number, ind?: string): Pro
await textEditor.document.save();
}

async function detick(todo: Todo): Promise<void>;
async function detick(path: string, line: number, indent: string): Promise<void>;
async function detick(todoOrPath: Todo | string, ln?: number, ind?: string): Promise<void> {
let path: string;
let line: number;
let indent: string;
if (typeof todoOrPath === 'string') {
path = todoOrPath;
line = ln!;
indent = ind!;
} else {
path = todoOrPath.file.path;
line = todoOrPath.line;
indent = todoOrPath.indent;
}

const textEditor = await window.showTextDocument(Uri.file(path), { preview: true });
const range = textEditor.document.lineAt(line).range;
textEditor.revealRange(range);
await textEditor.edit(editBuilder => {
const document = textEditor.document;
// TODO: Verify this won't break with -[ which we I guess support (use indexOf otherwise or improve MarkDownDOM to give this)
const checkStart = document.positionAt(document.offsetAt(range.start) + indent.length + '- '.length);
const checkEnd = document.positionAt(document.offsetAt(range.start) + indent.length + '- [?] '.length);
const checkRange = new Range(checkStart, checkEnd);
editBuilder.delete(checkRange);
});

await textEditor.document.save();
}

class TodoTreeDataProvider implements TreeDataProvider<Item> {
// Type as an array of `File`s as only files are kept at the top level
private cache: File[] = [];
Expand Down Expand Up @@ -414,6 +446,13 @@ class TodoCodeLensProvider implements CodeLensProvider {

// TODO: Figure out why item.indent is always zero
const indent = (/^\s+/.exec(line.text) || [''])[0];

lenses.push(new CodeLens(line.range, {
title: 'Detick',
command: 'markdown-todo.detick',
arguments: [document.uri.fsPath, line.lineNumber, indent],
}));

if (item.check !== null) {
lenses.push(new CodeLens(line.range, {
title: 'Untick',
Expand Down
4 changes: 0 additions & 4 deletions todo/provide-a-detick-command.md

This file was deleted.

0 comments on commit 69b0487

Please sign in to comment.