Skip to content

nvim-cmp completions for crates don't show up #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tranzystorekk opened this issue Oct 5, 2023 · 6 comments · Fixed by #86
Closed

nvim-cmp completions for crates don't show up #85

tranzystorekk opened this issue Oct 5, 2023 · 6 comments · Fixed by #86
Assignees
Labels
bug Something isn't working

Comments

@tranzystorekk
Copy link

Current behavior

Completion menu for a crate version field doesn't show crate versions.

Expected behavior

crates completions should show up.

Additional context/Screenshots

crates is configured as a source in my nvim-cmp:

cmp.setup {
  -- other settings omitted for brevity
  sources = cmp.config.sources({
    { name = 'nvim_lsp' },
    { name = 'luasnip' },
    { name = 'crates' },
  }),
}

Verified for crates.nvim versions 0.3, 0.4

Possible solution

Neovim version

nvim --version
output of nvim --version

0.9.2

@tranzystorekk tranzystorekk added the bug Something isn't working label Oct 5, 2023
@saecki
Copy link
Owner

saecki commented Oct 5, 2023

This is a minimal config that you can use with nvim -u minimal.lua:

local root = vim.fn.fnamemodify("crates_issue", ":p")
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy.nvim
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

require("lazy").setup({
    {
        "saecki/crates.nvim",
        dependencies = "nvim-lua/plenary.nvim",
        config = function()
            require("crates").setup({
                null_ls = {
                    enabled = true,
                },
                -- add crates.nvim config that is _necessary_ for reproducing the ISSUE
            })
        end,
    },
    {
        "hrsh7th/nvim-cmp",
        config = function()
            require("cmp").setup({
                sources = {
                    { name = "crates" },
                },
                -- add nvim-cmp config that is _necessary_ for reproducing the ISSUE
            })
        end,
    },
    {
        "jose-elias-alvarez/null-ls.nvim",
        config = function()
            require("null-ls").setup({
                null_ls = {
                    enabled = true,
                },
                -- add crates.nvim config that is _necessary_ for reproducing the ISSUE
            })
        end,
    },
}, {
    root = root .. "/plugins",
})

-- add init.lua settings that are _necessary_ for reproducing the ISSUE

I will probably add that later to the bug report form. Can you try to create a minimal reproducible example Cargo.toml file using this config?

@tranzystorekk
Copy link
Author

I am currently attempting to find the minimal repro, but with no success so far.

@tranzystorekk
Copy link
Author

Okay, the reproduction path has to do with running cmp.setup after the main lazy.setup. Running :CmpStatus in a Cargo.toml file then shows crates under "unknown source names".

I'm not sure if it's sth that could be easily fixed in crates.nvim, but at the very least this is how https://github.com/nvim-lua/kickstart.nvim configures nvim-cmp.

local root = vim.fn.fnamemodify("crates_issue", ":p")
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy.nvim
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

require("lazy").setup({
    {
        "saecki/crates.nvim",
        dependencies = "nvim-lua/plenary.nvim",
        config = function()
            require("crates").setup({
                null_ls = {
                    enabled = true,
                },
                -- add crates.nvim config that is _necessary_ for reproducing the ISSUE
            })
        end,
    },
    {
        "hrsh7th/nvim-cmp",
        -- CONFIG COMMENTED OUT FOR REPRODUCTION
        -- config = function()
        --     local cmp = require("cmp")
        --     cmp.setup({
        --         mapping = cmp.mapping.preset.insert {},
        --         sources = {
        --             { name = "crates" },
        --         },
        --         -- add nvim-cmp config that is _necessary_ for reproducing the ISSUE
        --     })
        -- end,
    },
    {
        "jose-elias-alvarez/null-ls.nvim",
        config = function()
            require("null-ls").setup({
                null_ls = {
                    enabled = true,
                },
                -- add crates.nvim config that is _necessary_ for reproducing the ISSUE
            })
        end,
    },
}, {
    root = root .. "/plugins",
})

-- add init.lua settings that are _necessary_ for reproducing the ISSUE

local cmp = require 'cmp'
cmp.setup {
    sources = {
        { name = 'crates' }
    }
}

@tranzystorekk
Copy link
Author

Thank you!

@noahxzhu
Copy link

noahxzhu commented Dec 7, 2023

Hi @saecki, I'm encountering the same issue after I added an event for the cmp plugin. Below is the minimal configuration required for reproduction.

local root = vim.fn.fnamemodify("crates_issue", ":p")
for _, name in ipairs({ "config", "data", "state", "cache" }) do
	vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy.nvim
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

require("lazy").setup({
	{
		"saecki/crates.nvim",
		dependencies = "nvim-lua/plenary.nvim",
		config = function()
			require("crates").setup({
				src = {
					cmp = {
						enabled = true,
					},
				},
				-- add crates.nvim config that is _necessary_ for reproducing the ISSUE
			})
		end,
	},
	{
		"hrsh7th/nvim-cmp",
		-- CONFIG COMMENTED OUT FOR REPRODUCTION
		event = "InsertEnter",
		config = function()
			local cmp = require("cmp")
			cmp.setup({
				mapping = cmp.mapping.preset.insert({}),
				sources = {
					{ name = "crates" },
				},
				-- add nvim-cmp config that is _necessary_ for reproducing the ISSUE
			})
		end,
	},
}, {
	root = root .. "/plugins",
})

-- add init.lua settings that are _necessary_ for reproducing the ISSUE

@saecki
Copy link
Owner

saecki commented Dec 7, 2023

crates.nvim tries to register it's nvim-cmp when a Cargo.toml is loaded, if nvim-cmp isn't loaded at that time, you'll have to register it yourself. Eg calling:

require("crates.src.cmp").setup()

You could do that inside your nvim-cmp config, to make sure it is called even if nvim-cmp is loaded after crates.nvim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants