epwalsh / obsidian.nvim

Obsidian 🤝 Neovim
Apache License 2.0
3.86k stars 175 forks source link

Part of workspace folder name appears in link from autocompletion #318

Closed Tetra-20 closed 8 months ago

Tetra-20 commented 8 months ago

🐛 Describe the bug

Hi, so when I use nvim-cmp to complete either an md link or wikilink with prepend_note_path enabled, it leaves an unwanted "m/". I found out that this is the from the name of the workspace folder which I set it to is named "Random".

WindowsTerminal exe_2024-01-09 20-55-01

(Minimal) obsidian and nvim-cmp config:

opts = {
    workspaces = {
        {
            name = "Random",
            path = "~/Documents/T-20/Random",
        }
    },

    detect_cwd = false,

    completion = {
        nvim_cmp = true,

        min_chars = 2,

        new_notes_location = "current_dir",

        prepend_note_id = false,
        prepend_note_path = true,
        use_path_only = false,
    },
}

return {
    "epwalsh/obsidian.nvim",
    version = "*",
    lazy = true,
    ft = "markdown",
    dependencies = {
        "nvim-lua/plenary.nvim",
    },
    config = function()
        local obsidian = require("obsidian")

        obsidian.setup(opts)
    end
}
return {
    'hrsh7th/nvim-cmp',
    dependencies = {
        "hrsh7th/cmp-nvim-lsp",
    },
    config = function()
        local cmp = require("cmp")
        local cmp_select = {behavior = cmp.SelectBehavior.Select}

        cmp.setup({
            sources = {
                {name = 'path'},
                {name = 'nvim_lsp'},
                {name = 'nvim_lua'},
                {name = "obsidian", option = opts},
                -- {name = "obsidian_new", option = opts},
            },
            mapping = cmp.mapping.preset.insert({
                ['<C-p>'] = cmp.mapping.select_prev_item(cmp_select),
                ['<C-n>'] = cmp.mapping.select_next_item(cmp_select),
                ['<C-y>'] = cmp.mapping.confirm({ select = true }),

                ["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
                ["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
            }),
        })
    end
}

Versions

nvim --version

NVIM v0.9.4
Build type: RelWithDebInfo
LuaJIT 2.1.1696883897
   system vimrc file: "$VIM\sysinit.vim"
  fall-back for $VIM: "C:/Program Files (x86)/nvim/share/nvim"

lua require("obsidian").info()

[obsidian.nvim (v2.5.3)] Commit SHA: 9a6eb2afb3218b22ac0026065f4f437745a851f6
[plenary.nvim] Commit SHA: 55d9fe89e33efd26f532ef20223e5f9430c8b0c0
[nvim-cmp] Commit SHA: 538e37ba87284942c1d76ed38dd497e54e65b891
[telescope.nvim] Commit SHA: d90956833d7c27e73c621a61f20b29fdb7122709
ripgrep 13.0.0 (rev af6b6c543b)
epwalsh commented 8 months ago

Huh... that's odd. Can you run this from a note in your vault and tell me what's printed:

lua print(require("obsidian").get_client():vault_relative_path("~/Documents/T-20/Random/300 Other/A Note.md"))
Tetra-20 commented 8 months ago

It prints: m/300 Other/A Note.md

epwalsh commented 8 months ago

Is your vault behind a symlink?

Tetra-20 commented 8 months ago

No, this issue still happens even when I change the workspace directory to its parent folder (~/Documents/T-20)

epwalsh commented 8 months ago

Do you use the Obsidian app? Do you have a .obsidian folder?

Tetra-20 commented 8 months ago

Yes, I do have the app and have the .obsidian folder, but it's with default settings.

epwalsh commented 8 months ago

Hmm, okay I'm still not able to reproduce so this might be an OS-specific bug (likely with plenary.nvim). Can you step through the Client:vault_relative_path function to debug?

https://github.com/epwalsh/obsidian.nvim/blob/5ac4951a62facac6f2399c92a2c1518d07be499f/lua/obsidian/client.lua#L171-L189

I would just throw print statements after each line. That way we can see where it goes wrong.

Tetra-20 commented 8 months ago

The issue is the j variable in line 180. The solution for me is to increase the variable by 2 in order to account for the last character of the workspace name (m) and the directory. (/) This would mean "m/" is removed from the link. Thanks for the help!

epwalsh commented 8 months ago

Thanks for figuring that out @Tetra-20! I'll add that fix in #322