Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

Neovim LSP woes.

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
12 posts • Page 1 of 1
Author
Message
leyvi
l33t
l33t
Posts: 719
Joined: Fri Sep 08, 2023 1:22 pm

Neovim LSP woes.

  • Quote

Post by leyvi » Mon Dec 01, 2025 6:34 pm

I'm trying to set up LSP support in Neovim. It has not been easy.

I found this: https://github.com/neovim/nvim-lspconfi ... ter.tar.gz.
I unpacked nvim-lspconfig-master/lsp to ~/.config/nvim/lsp. This didn't cut it.
I added an init.lua to ~/.config/nvim/lsp, containing a require for each file in that directory. That didn't cut it.

What's the big deal?
How am I supposed to do this?

Code: Select all

Error detected while processing /home/leyvi/.config/nvim/init.lua:
E5113: Error while calling lua chunk: ./lsp/init.lua:1: module 'da_ls' not found:
	no field package.preload['da_ls']
	cache_loader: module 'da_ls' not found
	cache_loader_lib: module 'da_ls' not found
	no file './da_ls.lua'
	no file '/usr/share/luajit-2.1/da_ls.lua'
	no file '/usr/local/share/lua/5.1/da_ls.lua'
	no file '/usr/local/share/lua/5.1/da_ls/init.lua'
	no file '/usr/share/lua/5.1/da_ls.lua'
	no file '/usr/share/lua/5.1/da_ls/init.lua'
	no file './da_ls.so'
	no file '/usr/local/lib/lua/5.1/da_ls.so'
	no file '/usr/lib64/lua/5.1/da_ls.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
	[C]: in function 'require'
	./lsp/init.lua:1: in main chunk
	[C]: in function 'require'
	/home/leyvi/.config/nvim/init.lua:4: in main chunk
Top
logrusx
Advocate
Advocate
User avatar
Posts: 3539
Joined: Thu Feb 22, 2018 2:29 pm

Re: Neovim LSP woes.

  • Quote

Post by logrusx » Mon Dec 01, 2025 6:50 pm

leyvi wrote: I found this: https://github.com/neovim/nvim-lspconfi ... ter.tar.gz.
I unpacked nvim-lspconfig-master/lsp to ~/.config/nvim/lsp. This didn't cut it.
That's not at all how you do it.

Watch this video playlist:

https://www.youtube.com/playlist?list=P ... 8m674KOvLG

As well as at least this video: https://www.youtube.com/watch?v=CuWfgiw ... XTlhKeQejM
and the next one from the playlist.

Configuring Neovim is not a small task, I can answer many questions but first you at least need to be clear on the basics.

Best Regards,
Georgi.
Top
leyvi
l33t
l33t
Posts: 719
Joined: Fri Sep 08, 2023 1:22 pm

  • Quote

Post by leyvi » Mon Dec 01, 2025 7:12 pm

Thanks. I've kept the nvim/lsp directory, and I now include only the LSPs I have installed in nvim/lsp.lua. Everything starts now, but it doesn't seem to be working (no fancy autocomplete from blink, no warning/error messages about my code, etc):

Code: Select all

return {
	{
		"neovim/nvim-lspconfig",
		config = function()
			require('lsp.rust-analyzer')
			require('lsp.clangd')
		end,
	}
}

Code: Select all

---@brief
---
--- https://github.com/rust-lang/rust-analyzer
---
--- rust-analyzer (aka rls 2.0), a language server for Rust
---
---
--- See [docs](https://rust-analyzer.github.io/book/configuration.html) for extra settings. The settings can be used like this:
--- ```lua
--- vim.lsp.config('rust_analyzer', {
---   settings = {
---     ['rust-analyzer'] = {
---       diagnostics = {
---         enable = false;
---       }
---     }
---   }
--- })
--- ```
---
--- Note: do not set `init_options` for this LS config, it will be automatically populated by the contents of settings["rust-analyzer"] per
--- https://github.com/rust-lang/rust-analyzer/blob/eb5da56d839ae0a9e9f50774fa3eb78eb0964550/docs/dev/lsp-extensions.md?plain=1#L26.

local function reload_workspace(bufnr)
  local clients = vim.lsp.get_clients { bufnr = bufnr, name = 'rust_analyzer' }
  for _, client in ipairs(clients) do
    vim.notify 'Reloading Cargo Workspace'
    ---@diagnostic disable-next-line:param-type-mismatch
    client:request('rust-analyzer/reloadWorkspace', nil, function(err)
      if err then
        error(tostring(err))
      end
      vim.notify 'Cargo workspace reloaded'
    end, 0)
  end
end

local function is_library(fname)
  local user_home = vim.fs.normalize(vim.env.HOME)
  local cargo_home = os.getenv 'CARGO_HOME' or user_home .. '/.cargo'
  local registry = cargo_home .. '/registry/src'
  local git_registry = cargo_home .. '/git/checkouts'

  local rustup_home = os.getenv 'RUSTUP_HOME' or user_home .. '/.rustup'
  local toolchains = rustup_home .. '/toolchains'

  for _, item in ipairs { toolchains, registry, git_registry } do
    if vim.fs.relpath(item, fname) then
      local clients = vim.lsp.get_clients { name = 'rust_analyzer' }
      return #clients > 0 and clients[#clients].config.root_dir or nil
    end
  end
end

---@type vim.lsp.Config
return {
  cmd = { 'rust-analyzer' },
  filetypes = { 'rust' },
  root_dir = function(bufnr, on_dir)
    local fname = vim.api.nvim_buf_get_name(bufnr)
    local reused_dir = is_library(fname)
    if reused_dir then
      on_dir(reused_dir)
      return
    end

    local cargo_crate_dir = vim.fs.root(fname, { 'Cargo.toml' })
    local cargo_workspace_root

    if cargo_crate_dir == nil then
      on_dir(
        vim.fs.root(fname, { 'rust-project.json' })
          or vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1])
      )
      return
    end

    local cmd = {
      'cargo',
      'metadata',
      '--no-deps',
      '--format-version',
      '1',
      '--manifest-path',
      cargo_crate_dir .. '/Cargo.toml',
    }

    vim.system(cmd, { text = true }, function(output)
      if output.code == 0 then
        if output.stdout then
          local result = vim.json.decode(output.stdout)
          if result['workspace_root'] then
            cargo_workspace_root = vim.fs.normalize(result['workspace_root'])
          end
        end

        on_dir(cargo_workspace_root or cargo_crate_dir)
      else
        vim.schedule(function()
          vim.notify(('[rust_analyzer] cmd failed with code %d: %s\n%s'):format(output.code, cmd, output.stderr))
        end)
      end
    end)
  end,
  capabilities = {
    experimental = {
      serverStatusNotification = true,
      commands = {
        commands = {
          'rust-analyzer.showReferences',
          'rust-analyzer.runSingle',
          'rust-analyzer.debugSingle',
        },
      },
    },
  },
  settings = {
    ['rust-analyzer'] = {
      lens = {
        debug = { enable = true },
        enable = true,
        implementations = { enable = true },
        references = {
          adt = { enable = true },
          enumVariant = { enable = true },
          method = { enable = true },
          trait = { enable = true },
        },
        run = { enable = true },
        updateTest = { enable = true },
      },
    },
  },
  before_init = function(init_params, config)
    -- See https://github.com/rust-lang/rust-analyzer/blob/eb5da56d839ae0a9e9f50774fa3eb78eb0964550/docs/dev/lsp-extensions.md?plain=1#L26
    if config.settings and config.settings['rust-analyzer'] then
      init_params.initializationOptions = config.settings['rust-analyzer']
    end
    ---@param command table{ title: string, command: string, arguments: any[] }
    vim.lsp.commands['rust-analyzer.runSingle'] = function(command)
      local r = command.arguments[1]
      local cmd = { 'cargo', unpack(r.args.cargoArgs) }
      if r.args.executableArgs and #r.args.executableArgs > 0 then
        vim.list_extend(cmd, { '--', unpack(r.args.executableArgs) })
      end

      local proc = vim.system(cmd, { cwd = r.args.cwd })

      local result = proc:wait()

      if result.code == 0 then
        vim.notify(result.stdout, vim.log.levels.INFO)
      else
        vim.notify(result.stderr, vim.log.levels.ERROR)
      end
    end
  end,
  on_attach = function(_, bufnr)
    vim.api.nvim_buf_create_user_command(bufnr, 'LspCargoReload', function()
      reload_workspace(bufnr)
    end, { desc = 'Reload current cargo workspace' })
  end,
}

Code: Select all

---@brief
---
--- https://clangd.llvm.org/installation.html
---
--- - **NOTE:** Clang >= 11 is recommended! See [#23](https://github.com/neovim/nvim-lspconfig/issues/23).
--- - If `compile_commands.json` lives in a build directory, you should
---   symlink it to the root of your source tree.
---   ```
---   ln -s /path/to/myproject/build/compile_commands.json /path/to/myproject/
---   ```
--- - clangd relies on a [JSON compilation database](https://clang.llvm.org/docs/JSONCompilationDatabase.html)
---   specified as compile_commands.json, see https://clangd.llvm.org/installation#compile_commandsjson

-- https://clangd.llvm.org/extensions.html#switch-between-sourceheader
local function switch_source_header(bufnr, client)
  local method_name = 'textDocument/switchSourceHeader'
  ---@diagnostic disable-next-line:param-type-mismatch
  if not client or not client:supports_method(method_name) then
    return vim.notify(('method %s is not supported by any servers active on the current buffer'):format(method_name))
  end
  local params = vim.lsp.util.make_text_document_params(bufnr)
  ---@diagnostic disable-next-line:param-type-mismatch
  client:request(method_name, params, function(err, result)
    if err then
      error(tostring(err))
    end
    if not result then
      vim.notify('corresponding file cannot be determined')
      return
    end
    vim.cmd.edit(vim.uri_to_fname(result))
  end, bufnr)
end

local function symbol_info(bufnr, client)
  local method_name = 'textDocument/symbolInfo'
  ---@diagnostic disable-next-line:param-type-mismatch
  if not client or not client:supports_method(method_name) then
    return vim.notify('Clangd client not found', vim.log.levels.ERROR)
  end
  local win = vim.api.nvim_get_current_win()
  local params = vim.lsp.util.make_position_params(win, client.offset_encoding)
  ---@diagnostic disable-next-line:param-type-mismatch
  client:request(method_name, params, function(err, res)
    if err or #res == 0 then
      -- Clangd always returns an error, there is no reason to parse it
      return
    end
    local container = string.format('container: %s', res[1].containerName) ---@type string
    local name = string.format('name: %s', res[1].name) ---@type string
    vim.lsp.util.open_floating_preview({ name, container }, '', {
      height = 2,
      width = math.max(string.len(name), string.len(container)),
      focusable = false,
      focus = false,
      title = 'Symbol Info',
    })
  end, bufnr)
end

---@class ClangdInitializeResult: lsp.InitializeResult
---@field offsetEncoding? string

---@type vim.lsp.Config
return {
  cmd = { 'clangd' },
  filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' },
  root_markers = {
    '.clangd',
    '.clang-tidy',
    '.clang-format',
    'compile_commands.json',
    'compile_flags.txt',
    'configure.ac', -- AutoTools
    '.git',
  },
  capabilities = {
    textDocument = {
      completion = {
        editsNearCursor = true,
      },
    },
    offsetEncoding = { 'utf-8', 'utf-16' },
  },
  ---@param init_result ClangdInitializeResult
  on_init = function(client, init_result)
    if init_result.offsetEncoding then
      client.offset_encoding = init_result.offsetEncoding
    end
  end,
  on_attach = function(client, bufnr)
    vim.api.nvim_buf_create_user_command(bufnr, 'LspClangdSwitchSourceHeader', function()
      switch_source_header(bufnr, client)
    end, { desc = 'Switch between source/header' })

    vim.api.nvim_buf_create_user_command(bufnr, 'LspClangdShowSymbolInfo', function()
      symbol_info(bufnr, client)
    end, { desc = 'Show symbol info' })
  end,
}
Top
logrusx
Advocate
Advocate
User avatar
Posts: 3539
Joined: Thu Feb 22, 2018 2:29 pm

  • Quote

Post by logrusx » Mon Dec 01, 2025 7:37 pm

This is how it goes with Neovim configuration help: you share your complete configuration somewhere so that somebody, in this case I, can see it and comment on it. Very often it's needed in its entirety because the order of execution matters too, especially if you're using lazy.nvim.

p.s. you've got to call vim.lsp.enable somewhere.

Also require('lsp.something') is the wrong way to go. Does it even execute without error? It's not under lua directory so require should fail to load it. Even if it did, it would only return a table, what would that do for the lsp configuration?

Generally, neovim should call those files automatically and use the returned table to configure the lsp's if the files are under the lsp directory. Read this:
https://gpanders.com/blog/whats-new-in-neovim-0-11/#lsp
Best Regards,
Georgi
Top
leyvi
l33t
l33t
Posts: 719
Joined: Fri Sep 08, 2023 1:22 pm

  • Quote

Post by leyvi » Tue Dec 02, 2025 12:55 pm

  • Code: Select all

    require('options')
    require('keymaps')
    require('config.lazy')
    vim.lsp.enable = true
  • lsp/init.lua and lsp.lua have been deleted.
Still, while I do get some autocomplete, I don't get completion in (for example) Rust module paths. I also don't see any sort of error message when I type something wrong...
Top
logrusx
Advocate
Advocate
User avatar
Posts: 3539
Joined: Thu Feb 22, 2018 2:29 pm

  • Quote

Post by logrusx » Tue Dec 02, 2025 1:10 pm

Will you share your while config at last? Otherwise I'm not able to help you.

Best Regards,
Georgi
Top
leyvi
l33t
l33t
Posts: 719
Joined: Fri Sep 08, 2023 1:22 pm

  • Quote

Post by leyvi » Tue Dec 02, 2025 2:25 pm

logrusx wrote:Will you share your while config at last? Otherwise I'm not able to help you.

Best Regards,
Georgi
Here you go.
Top
logrusx
Advocate
Advocate
User avatar
Posts: 3539
Joined: Thu Feb 22, 2018 2:29 pm

  • Quote

Post by logrusx » Tue Dec 02, 2025 3:24 pm

Code: Select all

vim.lsp.enable = true
Did you read the link I posted above?

vim.lsp.enable is a function. I told you to call it, not to make it true. While valid lua code, that's nonsense. You need to call vim.lsp.enable for all the lsp's you have installed.

But you really need to invest several hours in understanding how all that works.

Best Regards,
Georgi
Top
leyvi
l33t
l33t
Posts: 719
Joined: Fri Sep 08, 2023 1:22 pm

  • Quote

Post by leyvi » Thu Dec 04, 2025 3:58 pm

logrusx wrote:

Code: Select all

vim.lsp.enable = true
Did you read the link I posted above?

vim.lsp.enable is a function. I told you to call it, not to make it true. While valid lua code, that's nonsense. You need to call vim.lsp.enable for all the lsp's you have installed.

But you really need to invest several hours in understanding how all that works.

Best Regards,
Georgi
Sorry about that.

I've done the following:

Code: Select all

...
vim.lsp.enable({
	'rust-analyzer',
	'clangd'
})
Can I delete the contents of the lsp directory, which is filled with the contents of https://github.com/neovim/nvim-lspconfi ... master/lsp, since I assume those are the built-in defaults?

Also, while there is autocomplete, it doesn't look like rust-analyzer is indexing available crates (or at least not showing them in autocomplete)...
Top
logrusx
Advocate
Advocate
User avatar
Posts: 3539
Joined: Thu Feb 22, 2018 2:29 pm

  • Quote

Post by logrusx » Thu Dec 04, 2025 4:10 pm

vim.lsp.enable does not take a list. One at a time. This trial and error approach of yours is counterproductive for helping you.

If you put all those things in lsp from the nvim-lspconfig package, remove them and just install nvim-lspconfig.

Best Regards,
Georgi
Top
leyvi
l33t
l33t
Posts: 719
Joined: Fri Sep 08, 2023 1:22 pm

  • Quote

Post by leyvi » Thu Dec 04, 2025 4:36 pm

logrusx wrote:vim.lsp.enable does not take a list. One at a time. This trial and error approach of yours is counterproductive for helping you.

If you put all those things in lsp from the nvim-lspconfig package, remove them and just install nvim-lspconfig.

Best Regards,
Georgi
I read the documentation for vim.lsp.enable(), it explicitly specifies a list...

Is there a lazy.nvim package for nvim-lspconfig? I didn't find one in the repository.
Top
logrusx
Advocate
Advocate
User avatar
Posts: 3539
Joined: Thu Feb 22, 2018 2:29 pm

  • Quote

Post by logrusx » Thu Dec 04, 2025 5:01 pm

leyvi wrote:
logrusx wrote:vim.lsp.enable does not take a list. One at a time. This trial and error approach of yours is counterproductive for helping you.

If you put all those things in lsp from the nvim-lspconfig package, remove them and just install nvim-lspconfig.

Best Regards,
Georgi
I read the documentation for vim.lsp.enable(), it explicitly specifies a list...
Sorry, I misread your configuration. I didn't see the curly brackets and it looked like varargs.
leyvi wrote:Is there a lazy.nvim package for nvim-lspconfig? I didn't find one in the repository.
I don't understand what you're trying to say. There is no such thing as lazy package. Lazy is *A* package manager, not a repository. This is how you make lazy to install anything from GitHub:

Code: Select all

return { 'neovim/nvim-lspconfig' }
The quoted text is user/repository from GitHub.

If it's not from GitHub then you should use:

Code: Select all

return { url='...' }
Ref: https://lazy.folke.io/spec

Please watch at least the two videos on lua and lazy from TJ_DeVries' Advent of Neovim playlist I linked above.

Best Regards,
Georgi
Top
Post Reply

12 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic