zk-org / zk-nvim

Neovim extension for zk
https://github.com/zk-org/zk
GNU General Public License v3.0
502 stars 40 forks source link

Create a new note from selection without changing the selected text #120

Closed denolehov closed 1 year ago

denolehov commented 1 year ago

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](https://github.com/mickael-menu/zk/blob/a8d1db4c5738b8ae549bf87c079b829396bcbe9e/internal/adapter/lsp/cmd_new.go#L108-L111) from a server after creating a note, gets [ignored](https://github.com/mickael-menu/zk-nvim/blob/ff3a9661d9054a74d8baa51743c9dc11f8a215c3/lua/zk/api.lua#L41) 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](https://github.com/mickael-menu/zk/blob/a8d1db4c5738b8ae549bf87c079b829396bcbe9e/internal/adapter/lsp/cmd_new.go#L91) 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.

mickael-menu commented 1 year ago

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.