diff options
| author | Lorenzo Bellina <59364991+TheRealLorenz@users.noreply.github.com> | 2025-04-13 00:15:43 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-12 15:15:43 -0700 |
| commit | 81a570f58b2113cc2d538efae743ad38d6ab564f (patch) | |
| tree | 74f9691bbb1b303cd7cfaab7b543f82cb0dc6b46 /lsp/rust_analyzer.lua | |
| parent | refactor: replace vim.loop with vim.uv #3703 (diff) | |
| download | nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar.gz nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar.bz2 nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar.lz nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar.xz nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.tar.zst nvim-lspconfig-81a570f58b2113cc2d538efae743ad38d6ab564f.zip | |
feat: migrate to vim.lsp.config #3659
Problem:
Nvim 0.11 has vim.lsp.config, which mostly replaces the legacy
nvim-lspconfig "framework".
Solution:
Migrate all configs to `lsp/*` variants. The old configs in
`lua/lspconfig/` are "frozen".
The new configs include these changes:
- `commands` field became raw calls to
`vim.api.nvim_buf_create_user_command` inside `on_attach`.
- `root_dir` became:
- `root_markers` whenever the file list was simple didn't need to mach `*`
- if the logic was complicated, or needed to match something like
'\*.c', it was defined as a vim.lsp.Config `root_dir` callback.
- `on_config_change` became `before_init`. I don't actually know if this
is the correct approach, but looking around the documentation of
`nvim-lspconfig` a saw that it was defined as the function that gets
called as soon as the config have `root_dir`, and so I thought
`before_init` might be the closest alternative.
- `docs.description` became a luadoc `@brief` docstring.
- `single_file_support = false`?
Co-authored-by: Aliou Diallo <aliou@users.noreply.github.com>
Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
Diffstat (limited to 'lsp/rust_analyzer.lua')
| -rw-r--r-- | lsp/rust_analyzer.lua | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/lsp/rust_analyzer.lua b/lsp/rust_analyzer.lua new file mode 100644 index 00000000..c14df89d --- /dev/null +++ b/lsp/rust_analyzer.lua @@ -0,0 +1,118 @@ +local util = require 'lspconfig.util' + +local function reload_workspace(bufnr) + bufnr = util.validate_bufnr(bufnr) + local clients = vim.lsp.get_clients { bufnr = bufnr, name = 'rust_analyzer' } + for _, client in ipairs(clients) do + vim.notify 'Reloading Cargo Workspace' + 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 util.path.is_descendant(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 + +---@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. +return { + cmd = { 'rust-analyzer' }, + filetypes = { 'rust' }, + root_dir = function(bufnr, on_dir) + local fname = vim.api.nvim_buf_get_name(bufnr) + local reuse_active = is_library(fname) + if reuse_active then + return reuse_active + end + + local cargo_crate_dir = util.root_pattern 'Cargo.toml'(fname) + local cargo_workspace_root + + if cargo_crate_dir == nil then + on_dir( + util.root_pattern 'rust-project.json'(fname) + 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.notify(('[rust_analyzer] cmd failed with code %d: %s\n%s'):format(output.code, cmd, output.stderr)) + end + end) + end, + capabilities = { + experimental = { + serverStatusNotification = 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 + end, + on_attach = function() + vim.api.nvim_buf_create_user_command(0, 'CargoReload', function() + reload_workspace(0) + end, { desc = 'Reload current cargo workspace' }) + end, +} |
