zk-org / zk-nvim

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

ZkNewFromTitleSelection and ZkNewFromContentSelection only capture the first letter of range #19

Closed prmadev closed 2 years ago

prmadev commented 2 years ago

Hey! So when I select a range of characters, and invoke '<,'>ZkNewFromTitleSelection or '<,'>ZkNewFromContentSelection, the earlier makes a note with the title of the first letter of range and the later, a note with content of the first range. As an example this is a note:

# note one

this should become a link

When I enter visual mode and select this should become a link and invoke with the above commands I end up with the first note with this content:

# note one

[t](9a963a5c)his should become a link

and another note with this content:

# t

I have tried this with neovim versions

NVIM v0.6.1
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by nixbld

and also the latest nightly build available on git. I have also disabled all my other configurations.

Here is the content of my .zk/config.toml:

# zk configuration file

[note]

filename = "{{id}}"

extension = "md"

template = "default.md"

id-charset = "hex"

id-length = 8

# Letter case for the random IDs, among lower, upper or mixed.
id-case = "lower"

[extra]

[group.journal]
paths = [
    "journal",
]
[group.journal.note]
filename = "{{date now}}"
[format.markdown]

hashtags = true
colon-tags = false
multiword-tags = false

[tool]
editor = "nvim"

pager = "less -FIRX"
fzf-preview = "bat -p --color always {-1}"

[lsp]

[lsp.diagnostics]
dead-link = "error"

[lsp.completion]
note-label = "{{title-or-path}}"
note-filter-text = " {{title}} {{path}}"
note-detail = "{{filename-stem}}"

[filter]

[alias]
i = "zk edit -i"
ls = "zk list $@"
list = "zk list --quiet $@"
editlast = "zk edit --limit 1 --sort modified- $@"
recent = "zk edit --sort created- --created-after 'last two weeks' --interactive"
hist = "zk list --format path --delimiter0 --quiet $@ | xargs -t -0 git log --patch --"
conf = '$EDITOR "$ZK_NOTEBOOK_DIR/.zk/config.toml"'

Also here is the content of my zk.lua configuration file:

require("zk").setup({
    picker = "select",
    lsp = {
        -- `config` is passed to `vim.lsp.start_client(config)`
        config = {
            cmd = { "zk", "lsp" },
            name = "zk",
            -- on_attach = ...
            -- etc, see `:h vim.lsp.start_client()`
        },
        -- automatically attach buffers in a zk notebook that match the given filetypes
        auto_attach = {
            enabled = true,
            filetypes = { "markdown" },
        },
    },
})
require("telescope").load_extension("zk")
vim.api.nvim_set_keymap("v", "N", ":ZkNewFromTitleSelection<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<Leader>zc", "<cmd>ZkNew<CR>", { noremap = true })
vim.api.nvim_set_keymap("x", "<Leader>zc", ":'<'>ZkNewFromTitleSelection<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<Leader>zn", "<cmd>ZkNotes<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<Leader>zb", "<cmd>ZkBacklinks<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<Leader>zl", "<cmd>ZkLinks<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<Leader>zt", "<cmd>ZkTags<CR>", { noremap = true })

Please tell me to provide any additional information, if needed.

eric-hansen commented 2 years ago

I do wonder if using this code would work better for you? https://github.com/mickael-menu/zk-nvim/pull/14

Since it's been merged in, not sure when (if not already) it'll be set for release.

prmadev commented 2 years ago

I do wonder if using this code would work better for you? #14

Since it's been merged in, not sure when (if not already) it'll be set for release.

I think I'm already at the latest commit on main, which is one commit ahead of #14. And yet the problem persists, I'm not sure if the problem is caused by #14. Would you like me to try commits before its merge to see if the issue is present then as well?

kabouzeid commented 2 years ago

If you were trying this via your N mapping. This mapping vim.api.nvim_set_keymap("v", "N", ":ZkNewFromTitleSelection<CR>", { noremap = true }) should be vim.api.nvim_set_keymap("v", "N", ":'<'>ZkNewFromTitleSelection<CR>", { noremap = true }).

The '<'> is important here, Neovim will only update these marks after you leave visual mode. This makes sure to leave visual mode before executing the command.


Just to be sure: First you make a selection, and then hit : which should automatically insert :'<,'> for you, and the you complete this to :'<,'>ZkNewFromTitleSelection and hit enter. Is this what you're doing?

prmadev commented 2 years ago

y

If you were trying this via your N mapping. This mapping vim.api.nvim_set_keymap("v", "N", ":ZkNewFromTitleSelection<CR>", { noremap = true }) should be vim.api.nvim_set_keymap("v", "N", ":'<'>ZkNewFromTitleSelection<CR>", { noremap = true }).

The '<'> is important here, Neovim will only update these marks after you leave visual mode. This makes sure to leave visual mode before executing the command.

oh yeah, I actually changed that, just because I saw that it was repeated in the cmdline, just to see if it had anything to do with my problem. However, no tangible difference was made.

Just to be sure: First you make a selection, and then hit : which should automatically insert :'<,'> for you, and the you complete this to :'<,'>ZkNewFromTitleSelection and hit enter. Is this what you're doing?

Exactly. I have also tried differnet combinations of keymappings to invoke this function. Again, nothing substantial was changed.

I sometimes get lucky and the function executes correctly, but I am yet to pin-point the effective parameter.

kabouzeid commented 2 years ago

Just trying to pin this down; does it maybe work if you make a selection, then hit esc to go back to normal mode first, then manually type :'<,'>ZkNewFromTitleSelection?

kabouzeid commented 2 years ago

ok so I tracked it down now. there is a bug in this neovim 0.6.1 function (https://github.com/neovim/neovim/blob/b4fbb9dcf2752d95b9be335d99b0c55efb5f17de/runtime/lua/vim/lsp/util.lua#L193), which causes vim.lsp.util.make_given_range_params() to not always return the correct value on unsaved buffers.

I've made a workaround PR which fixes this on our side (https://github.com/mickael-menu/zk-nvim/pull/20/files), but I'll also create an issue/PR at Neovim to properly fix this on their side when I get time.

kabouzeid commented 2 years ago

Thanks for reporting! I was previously testing this with Neovim 0.6.0 where the bug is not present.

kabouzeid commented 2 years ago

I've reported this bug upstream. https://github.com/neovim/neovim/issues/16985