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

Issues with creating note from selection #122

Closed Rolv-Apneseth closed 5 months ago

Rolv-Apneseth commented 1 year ago

Hi, first of all thanks for making and maintaining this plugin and the zk tool itself, they are great.

I'm having issues when trying to create notes from visual selection, with both ZkNewFromTitleSelection and ZkNewFromContentSelection. When I use these functions while not in a markdown file, the file is created, but I am not brought to editing the file, but instead given this error (which I also see on #104 but I'm using the latest commit and still having this issue):

Error executing vim.schedule lua callback: /home/rolv/.local/share/nvim/lazy/zk-nvim/lua/zk.lua:68: RPC[Error] code_name = InvalidRequest, message = "Cannot insert link in 'file:///home/rolv/.dotfiles/nvim/.config/nvim/lua/core/keymappings.lua'"
stack traceback:
    [C]: in function 'assert'
    /home/rolv/.local/share/nvim/lazy/zk-nvim/lua/zk.lua:68: in function 'handler'
    /usr/share/nvim/runtime/lua/vim/lsp.lua:1394: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

Another issue I have is that when using these functions within a markdown file, it doesn't seem to use the currently selected word, but rather the previous selection I had before the current one, which also causes words to get cut off if I start selecting, and use h or l to expand / shrink the selection. How I would have expected it to function, is for example, I select a word, call the function, and what I have selected is used for the title.

Do you have any idea what could be causing these issues / how I could fix them? Any help would be appreciated.

lcmen commented 1 year ago

What NeoVIM version do you use? I had the same error when I was on 0.7.2. After upgrading to latest version (0.9.0), error is gone.

Rolv-Apneseth commented 1 year ago

I use 0.9.0, still currently having those 2 issues. They are issues I can live with as the rest of the plugin works well so probably not very high priority, was just wondering if anyone had encountered it before.

mpas commented 1 year ago

Have the same error when trying to create notes from a visual selection.

image

Rolv-Apneseth commented 1 year ago

Yeah I'm not sure about just closing issues because there's no activity, shouldn't it be left open until it's confirmed fixed? Not a major issue here but may as well have been left open until resolved

mickael-menu commented 1 year ago

@Rolv-Apneseth That's the plan, I'm just not on top of the triaging.

Rolv-Apneseth commented 1 year ago

All good, thank you for reopening and more importantly thanks for creating and maintaining this tool

jakejasey commented 1 year ago

I am having this same issue on Neovim version 0.9.1

Rolv-Apneseth commented 6 months ago

My issue with the previous selection is solved by using : instead of <cmd> in the keybind:

":'<,'>ZkNewFromTitleSelection { dir = 'program' }<CR>"

As for the main problem, I added a notify to see what was going on:

    if err ~= nil then
        vim.notify(tostring(err), 4)
    end

and the resulting error (which is passed by the LSP) is:

Error: RPC[Error] code_name = InvalidRequest, message = "Cannot insert link in 'file:///home/rolv/.dotfiles/nvim/.config/nvim/lua/core/keymappings.lua'"

So looks like the issue is in zk. The note gets created, but I guess since the current file isn't a note, so zk can't insert the link?

w4krzw9N8aIvT8cdEFd commented 6 months ago

My issue with the previous selection is solved by using : instead of <cmd> in the keybind:

So, I did this, and it seems to have fixed everything for me. I'm on 0.9.5. I am curious as to why : is OK and is not

Rolv-Apneseth commented 6 months ago

I don't see why it would be a problem. I used to think they were the exact same but I believe there's a few differences and one of those is that the selection is escaped

w4krzw9N8aIvT8cdEFd commented 6 months ago

I see. Perhaps somebody else is on the same track of thinking — didn’t know that there would be a significant difference either 🤔

tjex commented 5 months ago

So looks like the issue is in zk. The note gets created, but I guess since the current file isn't a note, so zk can't insert the link?

That's correct. zk only works with notes that are within its root folder, i.e the folder where you ran zk init. When you call ZkInsertLink or similar, zk is likely still working because you have the $ZK_NOTEBOOK_DIR env variables set? So it can see your notes, but it doesn't know that in that moment you are wanting to insert the note in a file that doesn't belong to the notebook. It only realises this once it tries to insert the link, and it finds that the file you're inserting into lies outside its root folder.

vim.keymap.set("v", "<leader>zn", ":ZkNewFromTitleSelection<cr>") is indeed the correct syntax. However I also just tried with <cmd>... instead of :... and it also worked. Without seeing the full keybind, I can't know further. I'll close for now, as I think everythings been covered. If not, reopen and ping me!

Rolv-Apneseth commented 5 months ago

Hey, thanks for replying.

When creating a note from a selection not in your notes, could it just skip the insert link step? It would be nice to be able to create a new note off anything I'm reading, which it does create but I guess the main bug here is that it doesn't open it, since it errors out while trying to create the link.

vim.keymap.set("v", "<leader>zn", ":ZkNewFromTitleSelection<cr>") is indeed the correct syntax. However I also just tried with <cmd>... instead of :... and it also worked.

I can confirm that for me, <cmd> only works if I select the word twice (it's using the previous selection). This is the keybind I'm using that works (I also tried the <cmd> version without the range, same result):

:'<,'>ZkNewFromTitleSelection<CR>

Edit: Ping as requested @tjex

tjex commented 5 months ago

@Rolv-Apneseth, I'm not personally convinced that implementing a new feature to account for this use case is the best course of action, because the functionality you're wanting can be easily achieved with relative ease: highlight -> copy -> ZkNew {title = "paste"}.

This can be made into a keybind, to take the title as a prompt, like: ":ZkNew {title = vim.fn.input('Title: ')}<cr>". Running ZkNew in a buffer that's outside of your zk root folder will work (provided you have the $ZK_NOTEBOOK_DIR env variable set or a global zk config [notebook] path ) Or, as we expose the API for zk, you could make this into a [custom Zk... command] pretty quickly.

Regarding the highlight+keybind issue, I have to take back what I wrote. I must've made a mistake with config, because key("v", "<leader>zn", ":<cmd>ZkNewFromTitleSelection {dir = vim.fn.input('Dir: ')}<cr>") does not work for me. Although, on my end I get an error that no selection has been made at all. I read through :h <cmd> but it says more or less that by and large <cmd> = :.

So I'd say there is some vim/nvim specific quirk to do here? In any case, I don't have any immediate clues. I would advise to just stick with :Command within this zk-nvim space, as the stated differences and benefits of :<cmd> shouldn't make any large differences worthy of looking into this further.

As a side note, executing ZkNewFromTitleSelection without range selection, should always throw an error "...try making a selection first".

Rolv-Apneseth commented 5 months ago

Yeah fair enough, pasting does work well enough for me.

As for : vs <cmd>, I believe I saw on a StackOverflow answer somewhere while I was trying to figure it out that there are a couple of differences, where one of them is something about escaping the selected range first. Obviously I'm no expert but seems that is what happens. The <cmd> version does work for me but it will use the previous selection, i.e. not what you currently have selected, so it won't work the first time

tjex commented 5 months ago

@Rolv-Apneseth . I had a look through your zk keybinds in your dotfiles, which made me realise I was missing the nvim syntax for range selection in my own keybind test... '<,'>.

So I tried this: key("v", "<leader>zn", ":<cmd>'<,'>ZkNewFromTitleSelection<cr>"), which left me with an open command prompt with :<,'>, whereby I had to hit enter to continue.

Then I put the <cr> in myself: key("v", "<leader>zn", ":<cmd>'<,'><cr>ZkNewFromTitleSelection<cr>"). And this actually does work for me; as in, the highlighted range becomes the title (not the previous highlighted range). There may be some conflict going elsewhere in your nvim config..

In any case, just wanted to correct myself (again!) for future readers and in case adding the <cr> after '<,'> in your keybind also fixes your issue.

Rolv-Apneseth commented 5 months ago

Huh, strange. I don't get that prompt. Oh well, sticking to : works well enough for me.

Thanks again for maintaining this, zk has become an essential part of my setup