I am looking into making an editor-agnostic configuration for rust-analyzer and everywhere I look people mention "put this snippet in the JSON config". From only 2-3 places on the net I finally managed to infer that "the JSON config" is usually a settings.json file. But no path is ever mentioned.
So if one wants to have a global rust-analyzer configuration in JSON format (I gather there are no other options?) then what's this elusive location that settings.json must be put into? Is it the standard $XDG_CONFIG_HOME or ~/.config or ~/Library/Preferences etc. stuff?
It's worth noting that there is a global (user) settings for vscode if that's the IDE you're using. If you press CTRL+P to open the command panel and then select for Edit user settings (JSON) (you can type for the usual fuzzy search) that will open the global JSON config that will apply to all of your vscode sessions.
If you use a different editor, consult their documentation.
Thank you -- not using VSCode. I was looking into not having to duplicate config between Emacs and NeoVim but it seems inescapable. I'll just use their recommended ways of configuring rust-analyzer then.
I think it is perhaps likely that you could use serde to transform from some neutral format like json into sexpr for emacs, and either convert to lua or use a lua based json parser for neovim for those settings that end up being passed to rust-analyzer. (sorry about the lack of specifics)
local function json_file(path)
local file = io.open(path, "rb")
if not file then
return {}
end
local content = file:read("*a")
file:close()
return vim.json.decode(content) or {}
end
local config_home = os.getenv("XDG_CONFIG_HOME") or (os.getenv("HOME") .. "/.config")
local user_ra_config = json_file(config_home .. "/nvim/rust-analyzer.json")
lua <<EOF
local function json_file(path)
local file = io.open(path, "rb")
if not file then
return {}
end
local content = file:read("*a")
file:close()
return vim.json.decode(content) or {}
end
local config_home = os.getenv("XDG_CONFIG_HOME") or (os.getenv("HOME") .. "/.config")
local user_ra_config = json_file(config_home .. "/nvim/rust-analyzer.json")
require('rust-tools').setup({
server = { -- rust-analyzer options
settings = {
["rust-analyzer"] = user_ra_config,
},
},
})
EOF