Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

rust-tools.setup{} seems to mess with keymaps of lsp setup for rust_analyzer #125

Closed
ambushed opened this issue Jan 12, 2022 · 3 comments
Closed

Comments

@ambushed
Copy link

ambushed commented Jan 12, 2022

I am experiencing trouble getting rust-tools to work smoothly with nvim-lsp rust analyzer setup. Depending on the position of
require"rust-tools".setup({}) line relative to require"lspconfig".rust_analyzer.setup(opts) things break in two ways:

  1. require"rust-tools".setup({}) before require"lspconfig".rust_analyzer.setup(opts) : A large part of "rust-tools" commands goes missing (e.g. RustDebuggables, RustRunnables are not there)

  2. require"rust-tools".setup({}) after require"lspconfig".rust_analyzer.setup(...): My keymaps stop working for the nvim lsp commands (running commands directly works just fine: lua vim.lsp.buf.rename() does the job)

I have checked out #114 which sounded similar to my case but couldn't find any one plugin that interfered with the expected behaviour.

Would be grateful for hints on where to look further!

System Info (Arch Linux):

NVIM v0.6.0
Build type: Release
LuaJIT 2.0.5
Compiled by builduser

Miminal init.lua to reproduce the issue:

vim.g.mapleader = ' '

local fn = vim.fn
local execute = vim.api.nvim_command

local install_path = fn.stdpath('data')..'/site/pack/packer/opt/packer.nvim'
if fn.empty(fn.glob(install_path)) > 0 then
   execute('!git clone https://github.com/wbthomason/packer.nvim '..install_path)
end
vim.cmd [[packadd packer.nvim]]
vim.cmd 'autocmd BufWritePost plugins.lua PackerCompile' -- Auto compile when there are

require('packer').startup(function()

-- native Neovim Lsp
use {'neovim/nvim-lspconfig'}
use {'simrat39/rust-tools.nvim'}

end)

local nvim_lsp = require('lspconfig')

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
  local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
  buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', { noremap=true })
  buf_set_keymap('n', '<space>gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
end

nvim_lsp.rust_analyzer.setup{
    on_attach = on_attach,
    settings = {
        ["rust-analyzer"] = {
            cargo = { loadOutDirsFromCheck = true },
            procMacro = { enable = true },
        },
    },
}

require("rust-tools").setup({})

and a sample rust file where renaming and going to definitions shortcuts can be tested to work or not depending on the position of the require("rust-tools").setup({}) line:

fn main() {
    do_work();
}

fn do_work() -> u32 {
    println!("Work is done");
    0
}

#[test]
fn tests() {
    assert_eq!(0, do_work());
}
@ambushed ambushed changed the title rust-tools.setup{} messes with keymaps of lsp setup for rust_analyzer rust-tools.setup{} seems to mess with keymaps of lsp setup for rust_analyzer Jan 12, 2022
@ambushed
Copy link
Author

I realize that the problem description is a bit vague, so i have included the minimal init.lua that showcases the issue i am having. Any help is appreciated!

@simrat39
Copy link
Owner

You aren't supposed to setup both lspconfig's rust analyzer and rust-tools as rust-tools automatically sets up the lsp. you can pass your on attach and other server config to the server table in the opts (check the example config in the readme).

Sorry for the late reply, haven't gotten much free time on my hands lately.

@ambushed
Copy link
Author

Thanks, another case of RTFM

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants