From 076a69bd4d70b95af0265475b4cddc1f30bf965d Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Fri, 17 Feb 2023 16:09:29 -0600 Subject: feat(nvim,lsp,rust): add command for setting cargo target I need to figure out how to do this without defer, or maybe even reloading. The workspace seems to need to be reloaded after the notification due to the [inactive-code] diagnostic, but the reload request being processed before the notification is completed, so I can only get it to work by defering. This technically introduces a race condition? --- .../nvim/lua/tobyvin/plugins/rust-tools.lua | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/nvim/.config/nvim/lua/tobyvin/plugins/rust-tools.lua b/nvim/.config/nvim/lua/tobyvin/plugins/rust-tools.lua index b7eb84d..c15190a 100644 --- a/nvim/.config/nvim/lua/tobyvin/plugins/rust-tools.lua +++ b/nvim/.config/nvim/lua/tobyvin/plugins/rust-tools.lua @@ -17,6 +17,48 @@ local M = { }, } +local function set_target() + local Job = require("plenary.job") + local targets = Job:new({ + command = "rustc", + args = { "--print=target-list" }, + enable_recording = true, + }):sync() + table.insert(targets, 1, "default") + + vim.ui.select(targets, {}, function(input) + if not input then + return + end + + if input == "default" then + input = nil + end + + ---@diagnostic disable-next-line: assign-type-mismatch + for _, client in pairs(vim.lsp.get_active_clients({ name = "rust_analyzer" })) do + client.config.settings["rust-analyzer"].cargo.target = input + client.notify("workspace/didChangeConfiguration", { + settings = client.config.settings, + }) + + -- TODO: Figure out how to do this without defer, or maybe even reloading. + -- The workspace seems to need to be reloaded after the notification due to the + -- [inactive-code] diagnostic, but the request being processed before the + -- notification is completed, so I can only get it to work by defering. This is + -- technically still a race condition? + vim.defer_fn(function() + client.request("rust-analyzer/reloadWorkspace", nil, function(err) + if err then + error(tostring(err)) + end + vim.notify("Cargo workspace reloaded") + end, 0) + end, 500) + end + end) +end + function M.init() require("tobyvin.lsp.configs").rust_analyzer = nil @@ -28,6 +70,8 @@ function M.init() return end + vim.api.nvim_create_user_command("RustSetTarget", set_target, { desc = "Set cargo target" }) + vim.keymap.set("n", "dd", require("rust-tools").debuggables.debuggables, { desc = "debug", buffer = args.buf, -- cgit v1.2.3-70-g09d2