simrat39 / rust-tools.nvim

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

Not starting automatically on Linux. #397

Open ls-devs opened 1 year ago

ls-devs commented 1 year ago

Hi, Rust-tools does not start automatically rust-analyser with the config in setup on my Linux laptop. To have rust-analyser and rust-tools working properly on my Linux I have to run RustStartStandaloneServerForBuffer

I use lsp-zero & lazy.nvim

Here's lazy config :

  -- Rust tools
  {
    "simrat39/rust-tools.nvim",
    ft = { "rust" },
    config = require("ls-devs.plugins.rust-tools").config,
    dependencies = { { "nvim-lua/plenary.nvim" } },
  },

Here's lsp-zero config :

  require("lspconfig.ui.windows").default_options.border = "rounded"

  local lsp = require("lsp-zero").preset({
    name = "recommended",
    manage_nvim_cmp = false,
  })

  vim.diagnostic.config({
    virtual_text = false,
    update_in_insert = false,
    underline = true,
    severity_sort = true,
    float = {
      focusable = true,
      style = "minmal",
      border = "rounded",
      source = "always",
      header = "",
      prefix = "",
    },
  })

  lsp.skip_server_setup({ "rust_analyzer" })
  lsp.skip_server_setup({ "hls" })
  lsp.skip_server_setup({ "clangd" })
  lsp.skip_server_setup({ "tsserver" })

  lsp.on_attach(function(client, bufnr)
    require("lsp-inlayhints").on_attach(client, bufnr)
    if client.name == "volar" or client.name == "tsserver" then
      client.server_capabilities.documentFormattingProvider = false
      client.server_capabilities.documentFormattingRangeProvider = false
    end
  end)

  require("lspconfig").glslls.setup(require("ls-devs.lsp.settings.glslls"))

  local get_servers = require("mason-lspconfig").get_installed_servers
  for _, server in pairs(get_servers()) do
    local has_config, config = pcall(require, "ls-devs.lsp.settings." .. server)
    if has_config then
      require("lspconfig")[server].setup(config)
    end
  end

  lsp.nvim_workspace()

  lsp.setup()

  vim.lsp.handlers["textDocument/hover"] = function(_, result, ctx, config)
    config = config or {}
    config.focus_id = ctx.method
    if not (result and result.contents) then
      return
    end
    config.border = "rounded"
    local markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
    markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
    if vim.tbl_isempty(markdown_lines) then
      return
    end
    return vim.lsp.util.open_floating_preview(markdown_lines, "markdown", config)
  end
end

Here's rust-tools config :

  local rt = require("rust-tools")
  local mason_registry = require("mason-registry")
  local codelldb = mason_registry.get_package("codelldb")
  local extension_path = codelldb:get_install_path() .. "/extension/"
  local codelldb_path = extension_path .. "adapter/codelldb"
  local liblldb_path = vim.fn.has("mac") == 1 and extension_path .. "lldb/lib/liblldb.dylib"
      or extension_path .. "lldb/lib/liblldb.so"
  rt.setup({
    tools = {
      runnables = {
        use_telescope = true,
      },
      inlay_hints = { auto = true, show_parameter_hints = true, locationLinks = false },
      hover_actions = { auto_focus = true },
    },
    dap = {
      adapter = require("rust-tools.dap").get_codelldb_adapter(codelldb_path, liblldb_path),
    },
    server = {
      standalone = false,
      on_attach = function(_, bufnr)
        vim.keymap.set("n", "K", rt.hover_actions.hover_actions, { buffer = bufnr })
        vim.keymap.set("n", "<leader>ca", rt.code_action_group.code_action_group, { buffer = bufnr })
        local opts = { noremap = true, silent = true }
        local keymap = vim.api.nvim_buf_set_keymap
        keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
        keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
        keymap(bufnr, "n", "gt", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
        keymap(bufnr, "n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
        keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
        keymap(bufnr, "n", "<C-K>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
        keymap(bufnr, "n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
        keymap(bufnr, "n", "<leader>fm", "<cmd>lua vim.lsp.buf.format( { timeout_ms = 5000  })<CR>", opts)
        keymap(bufnr, "n", "<leader>do", "<cmd>lua vim.diagnostic.open_float()<CR>", opts)
        keymap(bufnr, "n", "<leader>dp", "<cmd>lua vim.diagnostic.goto_prev()<CR>", opts)
        keymap(bufnr, "n", "<leader>dn", "<cmd>lua vim.diagnostic.goto_next()<CR>", opts)
        keymap(bufnr, "n", "<leader>uo", "<cmd>lua require('dapui').toggle()<CR>", opts)
        keymap(bufnr, "n", "<leader>uc", "<cmd>lua require('dapui').close()<CR>", opts)
        keymap(bufnr, "n", "<leader>un", ":DapContinue<CR>", opts)
        keymap(bufnr, "n", "<leader>ut", ":DapTerminate<CR>", opts)
        keymap(bufnr, "n", "<leader>bb", ":DapToggleBreakpoint<CR>", opts)
      end,
      ["rust-analyzer"] = {
        inlayHints = { auto = true, show_parameter_hints = true },
        lens = {
          enable = true,
        },
        checkonsave = {
          command = "clippy",
        },
        procMacros = {
          enabled = true,
        },
      },
    },
  })

Do I miss some required executables on my Linux for rust-tools to work properly ?

calops commented 1 year ago

I have the same issue using lazy. The issue is that the plugin isn't loaded until the first rust file is opened, and at this point, for some reason, it doesn't auto-attach (you need to :LspStart yourself.

A workaround for this is to remove your ft = { "rust" } line so that it doesn't load lazily. It'll auto-attach properly then. But yeah, it's not ideal and I don't really understand why it behaves that way.

B14CK313 commented 1 year ago

I had the same issue but found a workaround that I am currently using while still lazy loading rust-tools: Specify the event manually using event = { "BufReadPost *.rs" }. I don't know why this works and ft = { "rust" } does not, but at least it does work somehow. Hopefully it works for you aswell.

ls-devs commented 1 year ago

I had the same issue but found a workaround that I am currently using while still lazy loading rust-tools: Specify the event manually using event = { "BufReadPost *.rs" }. I don't know why this works and ft = { "rust" } does not, but at least it does work somehow. Hopefully it works for you aswell.

Thank you this is working.

calops commented 1 year ago

Yes, I can confirm this works. I guess the issue comes from either lazy or rust-tools. Pretty hard to diagnose.

I'll add another detail: for me, the issue isn't present when rust-analyzer is installed through mason-lspconfig. Only when I provide my own. Which seems to differ from the initial post here so I don't know what to think.