zk-org/zk-nvim

Create a new note from selection without changing the selected text

denolehov opened this issue · 1 comments

ZkInsertLinkAtSelection allows to link an existing note without changing the selected text. I was wondering if it's possible to extend this functionality to the new note creation as well. I.e. select a text, create a new note with a specific title, and link it to the selected text without replacing it with a note's title like ZkNewFromTitleSelection does.

Does this use case make sense? If so, would it be possible to add a new ZkNewFromSelection command or add an option to the existing command that would not replace a selected text with the note's title? I believe there is such an option in ZkInsertLinkAtSelection.

What I tried as a workaround

I was trying to hack a workaround for this by using zk.new to create a note and then calling zk.link on it with an option to not replace the selection. But zk.link requires a note's path, which, although is returned from a server after creating a note, gets ignored on nvim side. I am assuming it's because the request is async and the only way to get the response back is through a callback, which doesn't make sense in that context. Anyway, it looks like the cleanest way would be to actually change this behavior on the server side.

I am curious to hear your thoughts on this. If you think this is a useful feature to have, I'd be happy to take a stab at this. Just not sure which path to take: change this on the server side by extending the zk.new command or somehow tap into the response from the LSP server on nvim side, or something else entirely.

Yes, I think that would make sense to support this.

If you want to implement this, I think you could use the insertLinkAtLocation parameter of zk.new. Right now it takes the note title but we could have an additional title parameter inside the insertLinkAtLocation object, e.g.:

{
    "title": "Selection content",
    "uri":"file:///Users/mickael/notes/9se3.md",
    "range": {
        "end":{"line": 5, "character":149},
        "start":{"line": 5, "character":137}
    }
}

This is not part of the LSP specification, but it's JSON so we might as well use the extensibility it offers.

Once this is implemented server-side, we need a command in zk-nvim for convenience. Instead of adding a new one, how about supporting ranges for ZkNew? So that if it's called with :'<,'>ZkNew, then the selection will be used to create a link to the new note.