Best method of changing target during runtime? #147
-
Hello all! I'm migrating over from Within a single rust workspace, I'm sometimes looking at it in terms of building for wasm32-unknown-unknown and other times for x86_64-unknown-linux-gnu. This involves different dependencies and other conditional compilation options. To switch rust-analyzer to a different target, I had keymaps configured that would call custom functions for setting or printing the current target. Here are those functions: local lspconfig = require("lspconfig")
-- This function makes it easy to switch the build target during runtime
local function set_rust_target(target)
lspconfig.rust_analyzer.setup(vim.tbl_deep_extend("force", lspconfig.rust_analyzer.manager.config, {
settings = {
["rust-analyzer"] = {
cargo = {
target = target,
},
},
},
}))
end
local function print_rust_target()
local target = lspconfig.rust_analyzer.manager.config.settings["rust-analyzer"].cargo.target
-- Check if target was set during runtime
if target ~= nil then
print(target)
return
end
-- Otherwise, check the default target
local handle = io.popen("rustc -vV")
if handle ~= nil then
local result = handle:read("*a")
handle:close()
for line in result:gmatch("[^\r\n]+") do
if line:find("host:") then
target = line:match("^%s*(.-)%s*$"):gsub("host: ", "")
end
end
else
print("Unable to run `rustc -vV` to get rust target")
end
-- Print the results
if target ~= nil then
print(target)
else
print("Unable to find the rust target in `rustc -vV` output")
end
end Unfortunately, that method doesn't work with rustaceanvim. I'm trying to figure out what to modify, but I'm not a very advanced lua programmer. I'm probably making a mistake somewhere, so I'm reaching out to you all with this question. How can I modify the target during runtime? I've tried modifying local function set_rust_target(target)
print("Setting target to " .. target)
local settings = vim.g.rustaceanvim.server.settings
settings["rust-analyzer"] =
vim.tbl_deep_extend("force", settings["rust-analyzer"], { cargo = { target = target } })
vim.g.rustaceanvim.server.settings = settings
end The message prints, but the value returned by What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hey 👋 This plugin evaluates It does, however, allow the vim.g.rustaceanvim = {
server = {
---@param project_root string
settings = function(project_root)
return {
if type(vim.g.rust_analyzer_cargo_target) ~= 'string' then
-- Fall back to the default implementation if no valid target has been set
return require('rustaceanvim.config.server').load_rust_analyzer_settings(project_root)
end
["rust-analyzer"] = {
cargo = {
target = vim.g.rust_analyzer_cargo_target, -- set this variable with your keymap
},
},
}
end,
},
} The Your If your build target is project-dependent (doesn't change for an individual project), you could also A third option would be to write the By the way, you can find a complete documentation of all rustaceanvim configuration options with |
Beta Was this translation helpful? Give feedback.
Hey 👋
This plugin evaluates
vim.g.rustaceanvim
only once, during initialization.It's not meant to be modified at runtime.
It does, however, allow the
settings
field to be a function.For example: