Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cd1152a

Browse files
authored
Merge pull request #2290 from jyn514/nvim-config
expand ${workspaceFolder} in sample vim config
2 parents 077fae6 + bfda715 commit cd1152a

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

‎src/building/suggested.md‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,30 @@ Another way is without a plugin, and creating your own logic in your
123123
configuration. The following code will work for any checkout of rust-lang/rust (newer than Febuary 2025):
124124

125125
```lua
126+
local function expand_config_variables(option)
127+
local var_placeholders = {
128+
['${workspaceFolder}'] = function(_)
129+
return vim.lsp.buf.list_workspace_folders()[1]
130+
end,
131+
}
132+
133+
if type(option) == "table" then
134+
local mt = getmetatable(option)
135+
local result = {}
136+
for k, v in pairs(option) do
137+
result[expand_config_variables(k)] = expand_config_variables(v)
138+
end
139+
return setmetatable(result, mt)
140+
end
141+
if type(option) ~= "string" then
142+
return option
143+
end
144+
local ret = option
145+
for key, fn in pairs(var_placeholders) do
146+
ret = ret:gsub(key, fn)
147+
end
148+
return ret
149+
end
126150
lspconfig.rust_analyzer.setup {
127151
root_dir = function()
128152
local default = lspconfig.rust_analyzer.config_def.default_config.root_dir()
@@ -142,7 +166,7 @@ lspconfig.rust_analyzer.setup {
142166
-- load rust-lang/rust settings
143167
local file = io.open(config)
144168
local json = vim.json.decode(file:read("*a"))
145-
client.config.settings["rust-analyzer"] = json.lsp["rust-analyzer"].initialization_options
169+
client.config.settings["rust-analyzer"] = expand_config_variables(json.lsp["rust-analyzer"].initialization_options)
146170
client.notify("workspace/didChangeConfiguration", { settings = client.config.settings })
147171
end
148172
return true

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /