Rust-analyzer JSON global config location?

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?

1 Like

The way LSP is specified, server configuration is handled by the client (the editor). So it’s up to particular editor where configuration is stored.

We could add rust-analyzer specific config files, but we didn’t do that yet.

1 Like

Oh, so my assumption that rust-analyzer allows for a global configuration file is incorrect?

If so, apologies, I missed that in the docs.

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.

2 Likes

Even in (neo)vim, the way of how RA is configured varies in different lsp plugins.

2 Likes

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)

1 Like

In my init.vim I have this (Lua):

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")

and I pass it in with:

settings = {
	["rust-analyzer"] = user_ra_config,
},

This will load ~/.config/nvim/rust-analyzer.json as the default config.

3 Likes

Can you give a complete example, please? In this case: how is the settings variable used and where exactly?

Still learning both NeoVim and Lua itself, and I am mostly fumbling about still.

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
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.