diff --git a/configuration.nix b/configuration.nix index 8831dab7..3a29e8fe 100644 --- a/configuration.nix +++ b/configuration.nix @@ -74,6 +74,7 @@ isMaximal: { typst.enable = isMaximal; rust = { enable = isMaximal; + extensions.rustaceanvim.enable = isMaximal; extensions.crates-nvim.enable = isMaximal; }; toml.enable = isMaximal; diff --git a/modules/plugins/lsp/presets/rust-analyzer.nix b/modules/plugins/lsp/presets/rust-analyzer.nix new file mode 100644 index 00000000..0bd4d130 --- /dev/null +++ b/modules/plugins/lsp/presets/rust-analyzer.nix @@ -0,0 +1,193 @@ +{ + config, + lib, + pkgs, + ... +}: let + inherit (lib.meta) getExe; + inherit (lib.modules) mkIf; + inherit (lib.nvim.types) mkLspPresetEnableOption; + inherit (lib.nvim.dag) entryBefore; + inherit (lib.generators) mkLuaInline; + + cfg = config.vim.lsp.presets.rust-analyzer; +in { + options.vim.lsp.presets.rust-analyzer = { + enable = mkLspPresetEnableOption "rust-analyzer" "rust-analyzer" []; + }; + + # 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. + + config = mkIf cfg.enable { + luaConfigRC.rust-util = entryBefore ["lsp-servers"] '' + local function rust_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 rust_user_sysroot_src() + return vim.tbl_get(vim.lsp.config['rust-analyzer'], 'settings', 'rust-analyzer', 'cargo', 'sysrootSrc') + end + + local function rust_default_sysroot_src() + local sysroot = vim.tbl_get(vim.lsp.config['rust-analyzer'], 'settings', 'rust-analyzer', 'cargo', 'sysroot') + if not sysroot then + local rustc = ${getExe pkgs.rustc} + local result = vim.system({ rustc, '--print', 'sysroot' }, { text = true }):wait() + + local stdout = result.stdout + if result.code == 0 and stdout then + if string.sub(stdout, #stdout) == '\n' then + if #stdout > 1 then + sysroot = string.sub(stdout, 1, #stdout - 1) + else + sysroot = ''''''; + end + else + sysroot = stdout + end + end + end + + return sysroot and vim.fs.joinpath(sysroot, 'lib/rustlib/src/rust/library') or nil + end + + local function rust_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' + + local sysroot_src = rust_user_sysroot_src() or default_sysroot_src() + + for _, item in ipairs { toolchains, registry, git_registry, sysroot_src } do + if item and 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 + ''; + + vim.lsp.servers.rust-analyzer = { + enable = true; + cmd = [(getExe pkgs.rust-analyzer)]; + + on_attach = mkLuaInline '' + function(client, bufnr) + vim.api.nvim_buf_create_user_command(bufnr, 'LspCargoReload', function() + rust_reload_workspace(bufnr) + end, { desc = 'Reload current cargo workspace' }) + end + ''; + + root_dir = mkLuaInline '' + function(bufnr, on_dir) + local fname = vim.api.nvim_buf_get_name(bufnr) + local reused_dir = rust_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 = { + '${getExe pkgs.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; + }; + }; + }; + }; + }; + }; +}