@@ -35,8 +35,8 @@ local function is_in_workspace(client, root_dir)
3535 return false
3636end
3737
38- --- @ Searches upward for a .vscode/settings.json that contains rust-analyzer
39- --- settings and applies them.
38+ --- Searches upward for a .vscode/settings.json that contains rust-analyzer
39+ --- settings and returns them.
4040--- @param bufname string
4141--- @return table server_settings or an empty table if no settings were found
4242local function find_vscode_settings (bufname )
@@ -54,6 +54,25 @@ local function find_vscode_settings(bufname)
5454 return content and require (' rustaceanvim.config.json' ).silent_decode (content ) or {}
5555end
5656
57+ --- Generate the settings from config and vscode settings if found.
58+ --- settings and returns them.
59+ --- @param bufname string
60+ --- @param root_dir string | nil
61+ --- @param client_config table
62+ --- @return table server_settings or an empty table if no settings were found
63+ local function get_start_settings (bufname , root_dir , client_config )
64+ local settings = client_config .settings
65+ local evaluated_settings = type (settings ) == ' function' and settings (root_dir , client_config .default_settings )
66+ or settings
67+
68+ if config .server .load_vscode_settings then
69+ local json_settings = find_vscode_settings (bufname )
70+ require (' rustaceanvim.config.json' ).override_with_rust_analyzer_json_keys (evaluated_settings , json_settings )
71+ end
72+
73+ return evaluated_settings
74+ end
75+
5776--- @class LspStartConfig : RustaceanLspClientConfig
5877--- @field root_dir string | nil
5978--- @field init_options ? table
@@ -84,17 +103,7 @@ M.start = function(bufnr)
84103 lsp_start_config .init_options = { detachedFiles = { bufname } }
85104 end
86105
87- local settings = client_config .settings
88- local evaluated_settings = type (settings ) == ' function' and settings (root_dir , client_config .default_settings )
89- or settings
90-
91- --- @cast evaluated_settings table
92- lsp_start_config .settings = evaluated_settings
93-
94- if config .server .load_vscode_settings then
95- local json_settings = find_vscode_settings (bufname )
96- require (' rustaceanvim.config.json' ).override_with_rust_analyzer_json_keys (lsp_start_config .settings , json_settings )
97- end
106+ lsp_start_config .settings = get_start_settings (bufname , root_dir , client_config )
98107
99108 -- Check if a client is already running and add the workspace folder if necessary.
100109 for _ , client in pairs (rust_analyzer .get_active_rustaceanvim_clients ()) do
@@ -222,6 +231,24 @@ M.stop = function(bufnr)
222231 return clients
223232end
224233
234+ --- Reload settings for the LSP client.
235+ --- @param bufnr ? number The buffer number , defaults to the current buffer
236+ --- @return table[] clients A list of clients that will be have their settings reloaded
237+ M .reload_settings = function (bufnr )
238+ bufnr = bufnr or vim .api .nvim_get_current_buf ()
239+ local clients = rust_analyzer .get_active_rustaceanvim_clients (bufnr )
240+ --- @cast clients lsp.Client[]
241+ for _ , client in ipairs (clients ) do
242+ local settings = get_start_settings (vim .api .nvim_buf_get_name (bufnr ), client .config .root_dir , config .server )
243+ --- @diagnostic disable-next-line : inject-field
244+ client .settings = settings
245+ client .notify (' workspace/didChangeConfiguration' , {
246+ settings = client .settings ,
247+ })
248+ end
249+ return clients
250+ end
251+
225252--- Restart the LSP client.
226253--- Fails silently if the buffer's filetype is not one of the filetypes specified in the config.
227254--- @param bufnr ? number The buffer number (optional ), defaults to the current buffer
@@ -262,6 +289,7 @@ local RustAnalyzerCmd = {
262289 start = ' start' ,
263290 stop = ' stop' ,
264291 restart = ' restart' ,
292+ reload_settings = ' reloadSettings' ,
265293}
266294
267295local function rust_analyzer_cmd (opts )
@@ -274,6 +302,8 @@ local function rust_analyzer_cmd(opts)
274302 M .stop ()
275303 elseif cmd == RustAnalyzerCmd .restart then
276304 M .restart ()
305+ elseif cmd == RustAnalyzerCmd .reload_settings then
306+ M .reload_settings ()
277307 end
278308end
279309
@@ -283,7 +313,7 @@ vim.api.nvim_create_user_command('RustAnalyzer', rust_analyzer_cmd, {
283313 complete = function (arg_lead , cmdline , _ )
284314 local clients = rust_analyzer .get_active_rustaceanvim_clients ()
285315 --- @type RustAnalyzerCmd[]
286- local commands = # clients == 0 and { ' start' } or { ' stop' , ' restart' }
316+ local commands = # clients == 0 and { ' start' } or { ' stop' , ' restart' , ' reloadSettings ' }
287317 if cmdline :match (' ^RustAnalyzer%s+%w*$' ) then
288318 return vim .tbl_filter (function (command )
289319 return command :find (arg_lead ) ~= nil
0 commit comments