simrat39 / rust-tools.nvim

Tools for better development in rust using neovim's builtin lsp
MIT License
2.16k stars 158 forks source link

rust-tools disables my lsp-keybindings when using codelldb #177

Closed theCollectiv closed 2 years ago

theCollectiv commented 2 years ago

Hi,

if I use lldb-vscode + rusttools, the lsp-shortcuts are workingfine dap_rusttools_LanguageExtension4Rust.lua + lldb-vscode:

-- lldb-vscode config
require('rust-tools').setup({})
local opts = {
    tools = { -- rust-tools options
        -- whether to show hover actions inside the hover window
        -- this overrides the default hover handler so something like lspsaga.nvim's hover would be overriden by this
        -- default: true
        hover_with_actions = true,
    },
    dap = {
        adapter = {
            type = 'executable',
            command = 'lldb-vscode-10',
            name = 'rt_lldb',
        },
    },
}

require('rust-tools').setup(opts)

-- codelldb config
-- Update this path
--local extension_path = vim.env.HOME .. '.config/nvim/custom/rust_code_lldb/codelldb_Version_01.70.00/extension'
--local codelldb_path = extension_path .. 'adapter/codelldb'
--local liblldb_path = extension_path .. 'lldb/lib/liblldb.so'
--
--local opts = {
--    dap = {
--        adapter = require('rust-tools.dap').get_codelldb_adapter(codelldb_path, liblldb_path),
--    },
--}
--
---- Normal setup
--require('rust-tools').setup(opts)

As soon as I use codelldb + rust-tools.nvim my lsp-keybindings stop working, like they never have been binded. dap_rusttools_LanguageExtension4Rust.lua:

-- lldb-vscode config
--require('rust-tools').setup({})
--local opts = {
--    tools = { -- rust-tools options
--        -- whether to show hover actions inside the hover window
--        -- this overrides the default hover handler so something like lspsaga.nvim's hover would be overriden by this
--        -- default: true
--        hover_with_actions = true,
--    },
--    dap = {
--        adapter = {
--            type = 'executable',
--            command = 'lldb-vscode-10',
--            name = 'rt_lldb',
--        },
--    },
--}

--require('rust-tools').setup(opts)

-- codelldb config
-- Update this path
local extension_path = vim.env.HOME .. '.config/nvim/custom/rust_code_lldb/codelldb_Version_01.70.00/extension'
local codelldb_path = extension_path .. 'adapter/codelldb'
local liblldb_path = extension_path .. 'lldb/lib/liblldb.so'

local opts = {
    dap = {
        adapter = require('rust-tools.dap').get_codelldb_adapter(codelldb_path, liblldb_path),
    },
}

-- Normal setup
require('rust-tools').setup(opts)

If I use command mode and type these commands (like : lua vim.lsp.buf.formatting()), these work fine. But not the keybindings.


For better understanding, my nvim-dir looks like this:

nvim
├── custom
│   ├── rust_code_lldb
│   │   ├── codelldb_Version_01.70.00
│   │   │   ├── [Content_Types].xml
│   │   │   ├── extension
│   │   │   │   ├── adapter
│   │   │   │   │   ├── codelldb
│   │   │   │   │   ├── codelldb.py
│   │   │   │   │   ├── console.py
│   │   │   │   │   ├── debugger.py
│   │   │   │   │   ├── libcodelldb.so
│   │   │   │   │   └── value.py
│   │   │   │   ├── CHANGELOG.md
│   │   │   │   ├── extension.js
│   │   │   │   ├── formatters
│   │   │   │   │   ├── __init__.py
│   │   │   │   │   └── rust.py
│   │   │   │   ├── images
│   │   │   │   │   ├── lldb.png
│   │   │   │   │   ├── users.svg
│   │   │   │   │   └── user.svg
│   │   │   │   ├── lldb
│   │   │   │   │   ├── bin
│   │   │   │   │   │   ├── lldb
│   │   │   │   │   │   ├── lldb-argdumper
│   │   │   │   │   │   └── lldb-server
│   │   │   │   │   └── lib
│   │   │   │   │       ├── liblldb.so
│   │   │   │   │       ├── libpython39.so
│   │   │   │   │       ├── lldb-python
│   │   │   │   │       └── python3.9
│   │   │   │   ├── package.json
│   │   │   │   ├── platform.ok
│   │   │   │   ├── README.md
│   │   │   │   └── syntaxes
│   │   │   │       └── disassembly.json
│   │   │   └── extension.vsixmanifest
...
├── init.lua
├── lua
│   ├── global_keymapping.lua
│   ├── global_optical.lua
│   ├── global_options.lua
│   └── plugins
│       ├── 0_plugin_manager.lua
│       ├── airline.lua
│       ├── cmp.lua
│       ├── comment.lua
│       ├── dap_debug_adapter_protocol.lua
│       ├── dap_rusttools_LanguageExtension4Rust.lua
│       ├── dap_UI_extension.lua
│       ├── dap_UI_virtualText.lua
│       ├── fzf_forTelescope.lua
│       ├── gitsigns.lua
│       ├── lsp
│       │   └── init.lua
│       ├── ranger.lua
│       ├── sneak.lua
│       ├── telescope.lua
│       ├── toggleterm.lua
│       ├── treesitter.lua
│       ├── UI_popui.lua
│       ├── undotree.lua
│       └── vimwiki_note_taking.lua
...

My lsp-keybinding (nvim/lua/plugins/lsp/init.lua)

-- setting up rust
require('lspconfig').rust_analyzer.setup({
    capabilities = capabilities,
    on_attach = function()
        -- Mapping: DIAGNOSTICS
        -- See `:help vim.diagnostic.*` for documentation on any of the below functions
        local opts = { noremap = true, silent = true }
        vim.api.nvim_set_keymap('n', '<leader>e', '<cmd>lua vim.diagnostic.open_float()<CR>', opts)
        vim.api.nvim_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts)
        vim.api.nvim_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts)
        vim.api.nvim_set_keymap('n', '<leader>q', '<cmd>lua vim.diagnostic.setloclist()<CR>', opts)
        -- Enable completion triggered by <c-x><c-o>
        vim.api.nvim_buf_set_option(0, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
        -- Mappings: LSP
        -- See `:help vim.lsp.*` for documentation on any of the below functions
        vim.api.nvim_buf_set_keymap(0, 'n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', '<leader>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', '<leader>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', '<leader>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', '<leader>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', '<leader>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', '<leader>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
        vim.api.nvim_buf_set_keymap(0, 'n', '<leader>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
    end,

    -- rust-analyzer official doc
    -- on_attach=on_attach,
    settings = {
        ['rust-analyzer'] = {
            assist = {
                importGranularity = 'module',
                importPrefix = 'self',
            },
                        cargo = {
                            loadOutDirsFromCheck = true
                        },
            procMacro = {
                enable = true,
            },
        },
    },
})

I wonder, if I misconfigured something or if someone else got the same/similar problem to get rusttools + codelldb + lsp-keybindings to work.

simrat39 commented 2 years ago

So, rust-tools uses lspconfig in the background, so you don't need to initialize it on its own. Remove the call to lspconfig and move your server options to opts.server which are passed to rust tools

aryzing commented 2 years ago

@theCollectiv check out https://github.com/simrat39/rust-tools.nvim/issues/183#issuecomment-1120232092