@@ -324,6 +324,13 @@ M.stop = function(bufnr, filter)
324324 return clients
325325end
326326
327+ --- Restart the LSP client.
328+ --- Fails silently if the buffer's filetype is not one of the filetypes specified in the config.
329+ --- @return number | nil client_id The LSP client ID after restart
330+ M .restart = function ()
331+ return restart ()
332+ end
333+
327334--- Reload settings for the LSP client.
328335--- @param bufnr ? number The buffer number , defaults to the current buffer
329336--- @return vim.lsp.Client[] clients A list of clients that will be have their settings reloaded
@@ -367,11 +374,20 @@ M.set_target_arch = function(bufnr, target)
367374 end )
368375end
369376
370- --- Restart the LSP client.
371- --- Fails silently if the buffer's filetype is not one of the filetypes specified in the config.
372- --- @return number | nil client_id The LSP client ID after restart
373- M .restart = function ()
374- return restart ()
377+ --- @param ra_settings table
378+ function M .set_config (ra_settings )
379+ local bufnr = vim .api .nvim_get_current_buf ()
380+ local clients = rust_analyzer .get_active_rustaceanvim_clients (bufnr )
381+ --- @cast clients vim.lsp.Client[]
382+ for _ , client in ipairs (clients ) do
383+ local settings = get_start_settings (vim .api .nvim_buf_get_name (bufnr ), client .config .root_dir , config .server )
384+ --- @diagnostic disable-next-line : inject-field
385+ settings [' rust-analyzer' ] = vim .tbl_deep_extend (' force' , settings [' rust-analyzer' ], ra_settings )
386+ client .settings = settings
387+ client :notify (' workspace/didChangeConfiguration' , {
388+ settings = client .settings ,
389+ })
390+ end
375391end
376392
377393--- @enum RustAnalyzerCmd
@@ -381,11 +397,12 @@ local RustAnalyzerCmd = {
381397 restart = ' restart' ,
382398 reload_settings = ' reloadSettings' ,
383399 target = ' target' ,
400+ config = ' config' ,
384401}
385402
386403local function rust_analyzer_user_cmd (opts )
387404 local fargs = opts .fargs
388- local cmd = fargs [ 1 ]
405+ local cmd = table.remove ( fargs , 1 )
389406 --- @cast cmd RustAnalyzerCmd
390407 if cmd == RustAnalyzerCmd .start then
391408 M .start ()
@@ -396,8 +413,17 @@ local function rust_analyzer_user_cmd(opts)
396413 elseif cmd == RustAnalyzerCmd .reload_settings then
397414 M .reload_settings ()
398415 elseif cmd == RustAnalyzerCmd .target then
399- local target_arch = fargs [2 ]
416+ local target_arch = fargs [1 ]
400417 M .set_target_arch (nil , target_arch )
418+ elseif cmd == RustAnalyzerCmd .config then
419+ local ra_settings_str = vim .iter (fargs ):join (' ' )
420+ local f = load (' return ' .. ra_settings_str )
421+ --- @diagnostic disable-next-line : param-type-mismatch
422+ local ok , ra_settings = pcall (f )
423+ if not ok or type (ra_settings ) ~= ' table' then
424+ return vim .notify (' RustAnalyzer config: invalid Lua table.\n ' .. ra_settings_str , vim .log .levels .ERROR )
425+ end
426+ M .set_config (ra_settings )
401427 end
402428end
403429
@@ -407,7 +433,7 @@ vim.api.nvim_create_user_command('RustAnalyzer', rust_analyzer_user_cmd, {
407433 complete = function (arg_lead , cmdline , _ )
408434 local clients = rust_analyzer .get_active_rustaceanvim_clients ()
409435 --- @type RustAnalyzerCmd[]
410- local commands = # clients == 0 and { ' start' } or { ' stop' , ' restart' , ' reloadSettings' , ' target' }
436+ local commands = # clients == 0 and { ' start' } or { ' stop' , ' restart' , ' reloadSettings' , ' target' , ' config ' }
411437 if cmdline :match (' ^RustAnalyzer%s+%w*$' ) then
412438 return vim .tbl_filter (function (command )
413439 return command :find (arg_lead ) ~= nil
0 commit comments