Hey there, if anyone of you is using LunarVim with many different rust projects, you might find this information helpful:
So, I use LunarVim as my main editor and am pretty satisfied with it. As I use it to edit a lot of different projects both at work and in my spare time, I recently found myself needing to configure rust-analyzer to do different things for some of the projects. Now, configuring this is not hard, but I wanted/needed to do some of these things on a project-by-project basis, instead of globally.
I have searched a bit but did not find any satisfying way to do that. (If there is an easier way than what I share here, please let me know)
So I instead started experimenting with loading special configuration scripts when i open LunarVim in the project root.
So I did something like this:
-- ~/.config/lvim/config.lua
-- Main LunarVim config
local project_config = vim.fn.getcwd() .. "/.lvim-config.lua"
if vim.fn.filereadable(project_config) == 1 then
dofile(project_config)
end
This code, when put into LunarVim's main configuration file, will tell LunarVim on startup to check if it can fine a lvim-config.lua
file in the current directory, and if so, execute it.
I can hear some of you cringing now, but hear me out:
I know this has the potential to be incredibly insecure. But I think as long as you don't commit this file - and pay attention to files like this in the project you are working on and make sure they are safe- it's not too bad. I guess you could also implement extra checks or something, but my Lua is not really that good.
In your project root you then put any LSP configuration you want. For example, if you want to tell rust-analyzer to check your project with all features enabled - so it does not ignore code behind a feature cfg
- you can do it like this:
-- your/repo/.lvim-config.lua
local lspconfig = require('lspconfig')
lspconfig.rust_analyzer.setup {
settings = {
["rust-analyzer"] = {
cargo = {
allFeatures = true,
},
},
},
}
This script will then be executed when you start LunarVim and the config will be respected until you close it.
And that's it. Now you can use different features and checks for your project.
I know this is not an amazing solution (and for sure someone thought of this before), but maybe it helps, in the off-chance someone still uses LunarVim.
What do you think about this approach?
Edit: Someone just told me you can configure rust-analyzer by a corresponding .json in my project root