summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--nvim/.config/nvim/lua/tobyvin/plugins/rust-tools.lua44
1 files changed, 44 insertions, 0 deletions
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", "<leader>dd", require("rust-tools").debuggables.debuggables, {
desc = "debug",
buffer = args.buf,