aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/nvim/.config
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2023-02-17 16:09:29 -0600
committerToby Vincent <tobyv13@gmail.com>2023-02-17 16:09:29 -0600
commit076a69bd4d70b95af0265475b4cddc1f30bf965d (patch)
tree25d432f81a0da2d9297308e9df3be7a12c7e232c /nvim/.config
parentaca330cd9016f55fa8ed54cad2b9a3840d72df32 (diff)
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?
Diffstat (limited to 'nvim/.config')
-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,