rust-tools.nvim icon indicating copy to clipboard operation
rust-tools.nvim copied to clipboard

Extend `rust.vim`'s `Cargo` command with `add`

Open ModProg opened this issue 3 years ago • 2 comments

I did some experimenting and came up with this:

  • Cargo reload reloads the cargo for lsp
  • Cargo add runs add and afterwards reloads cargo
  • Cargo * will behave like rust.vim's Cargo command.

Not sure if this fits in here, maybe in a stripped down version, as this depends on rust.vim for the non add or reload commands.

Put it in .config/nvim/after/ftplugin/rust.lua

vim.api.nvim_buf_add_user_command(0, "Cargo", function(env)
    local cmd = env.args:match("^(%S+)")
    io.stdout:setvbuf 'no'

    if cmd == "add" then
        local file = assert(io.popen('cargo ' .. env.args .. ' 2>&1', 'r'))
        file:flush() -- > important to prevent receiving partial output
        local output = file:read('*all')
        print(output)
        require'lspconfig'["rust_analyzer"].commands["CargoReload"][1]()
    elseif cmd == "reload" then
        require'lspconfig'["rust_analyzer"].commands["CargoReload"][1]()
    else
        vim.cmd('call cargo#cmd("' .. env.args .. '")')
    end
end, {nargs = "+", force = true});

ModProg avatar Jan 26 '22 07:01 ModProg

Yeah not sure if it's in the scope of this plugin, atleast wouldn't wanna depend on some other plugin

simrat39 avatar Feb 22 '22 01:02 simrat39

Yeah, just a general Idea, as a lot of people use cargo edit to manage their dependencies, and running cargo add does not reload rust-analyzer.

ModProg avatar Feb 22 '22 06:02 ModProg