@@ -123,6 +123,30 @@ Another way is without a plugin, and creating your own logic in your
123123configuration. 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
126150lspconfig .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