dendronhq / dendron

The personal knowledge management (PKM) tool that grows as you do!
https://wiki.dendron.so
Apache License 2.0
6.67k stars 252 forks source link

Allow mapping keybindings to custom task statuses #2643

Open mihaiconstantin opened 2 years ago

mihaiconstantin commented 2 years ago

Please select if your request is either something new or an enhancement

Please select the area your request applies to. (Multiple selections are Possible. You can leave blank if you're not sure.)

Is your feature request related to a problem? Please describe

I often find myself updating task statuses (i.e., changing the character between [ and ] of a markdown list item). Unfortunately, the Markdown all in one extension only allows toggling between [x] and [ ]. I was hoping for something more general that allows mapping whatever symbols (e.g., x, >, < etc.) to custom keybindings.

Describe the solution you'd like

I would like to be able to add custom keybindings to change what is between [ and ] with various characters.

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.

n.a.

Add any other context or screenshots about the feature request here.

https://user-images.githubusercontent.com/20051042/160285337-4f6da6ef-61e4-4f34-aaab-65f8a5d392df.mov

mihaiconstantin commented 2 years ago

I am having trouble building the dendron repository, so I will leave my suggestion here instead of opening a PR.

// taskOperations.ts

import { Range, TextEditor, TextEditorEdit, TextLine, window } from 'vscode';

// Update task status.
function setStatus(symbol: string): void {
    // Regex pattern (i.e., see: https://regex101.com/r/lVQ2am/1).
    const pattern = /(?<=-\s\[)([\sx<>\/])(?=\])/;

    // Ensure an editor is open.
    const editor: TextEditor | undefined = window.activeTextEditor;

    // Ensure an editor is open.
    if (!editor) {
        window.showErrorMessage('No editor open.');
        return
    }

    // Get line.
    const line: TextLine = editor.document.lineAt(editor.selection.active.line);

    // Get line contents.
    let lineContents = line.text;

    // Find index for character inside `[` and `]`.
    let index: number | undefined = lineContents.match(pattern)?.index;

    // Ensure the command is executed on a task.
    if (!index) {
        window.showErrorMessage('Not a valid task item.');
        return
    }

    // Create range for replacement.
    const range: Range = new Range(line.lineNumber, index, line.lineNumber, index + 1);

    editor.edit((editBuilder: TextEditorEdit) => {
        editBuilder.replace(range, symbol);
    });
}

// Set custom status.
export const setCustomStatus = (args: any): void => {
    // Ensure symbol is provided.
    if (!args.symbol) {
        window.showErrorMessage('Symbol not provided for keybinding.');
        return
    }

    // Update the task status.
    setStatus(args.symbol);
}
// extension.ts

import * as vscode from 'vscode';
import * as taskOperations from './taskOperations';

// When extension gets activated.
export function activate(context: vscode.ExtensionContext) {
    context.subscriptions.push(
        // Register command.
        vscode.commands.registerCommand('dendron.taskSetStatus', taskOperations.setCustomStatus)
    );
}
// package.json
{
// ....
    "activationEvents": [
        "onCommand: dendron.taskSetStatus"
    ],
// ...
}

Then, as a keybinding we could have:

// keybindings.json
[
    // ...
    {
        "key": "alt+x",
        "command": "dendron.taskSetStatus",
        "args": {
            "symbol": "x",
        },
        "when": "editorTextFocus"
    },
   // ...
]
SeriousBug commented 2 years ago

Thanks for the suggestion! A command to set the task status is on our roadmap, it got delayed a bit due to our ongoing focus on stability and simplicity. I'll pick this up as soon as I get back to working on tasks!

Also, we'd love to help you get your setup working if you want to contribute this feature. If you ask for help in the #dev channel on Discord I'm sure we can help you get it working.

mihaiconstantin commented 2 years ago

@SeriousBug Glad to hear this is on the roadmap! Thanks, I will reach out later on the #dev channel regarding the build errors I encountered.

aleksey-rowan commented 2 years ago

@codeluggage has a writeup here on automating task management with VSCode macros + vim.

Source: https://discord.com/channels/717965437182410783/743194856788328497/967989288308469860

aleksey-rowan commented 2 years ago

This issue is featured in this week's round of CROP voting on Dendron's Discord server.

SeriousBug commented 2 years ago

This is actually supported now, here's the instructions on how to set a keyboard shortcut: https://wiki.dendron.so/notes/SEASewZSteDK7ry1AshNG/#keyboard-shortcut-for-a-task-status

aleksey-rowan commented 2 years ago

This is actually supported now, here's the instructions on how to set a keyboard shortcut: https://wiki.dendron.so/notes/SEASewZSteDK7ry1AshNG/#keyboard-shortcut-for-a-task-status

@SeriousBug, I believe this issue is about creating keybindings to set status of a regular markdown checkbox, not Denrdon's task notes.