347347
348348--- Restart the LSP client.
349349--- Fails silently if the buffer's filetype is not one of the filetypes specified in the config.
350+ --- @param bufnr ? number The buffer number , defaults to the current buffer
350351--- @return number | nil client_id The LSP client ID after restart
351- M .restart = function ()
352- return restart ()
352+ M .restart = function (bufnr )
353+ return restart (bufnr )
353354end
354355
355356--- Reload settings for the LSP client.
@@ -395,9 +396,10 @@ M.set_target_arch = function(bufnr, target)
395396 end )
396397end
397398
399+ --- @param bufnr ? number The buffer number , defaults to the current buffer
398400--- @param ra_settings table
399- function M .set_config (ra_settings )
400- local bufnr = vim .api .nvim_get_current_buf ()
401+ function M .set_config (bufnr , ra_settings )
402+ bufnr = bufnr or vim .api .nvim_get_current_buf ()
401403 local clients = rust_analyzer .get_active_rustaceanvim_clients (bufnr )
402404 --- @cast clients vim.lsp.Client[]
403405 for _ , client in ipairs (clients ) do
@@ -411,57 +413,4 @@ function M.set_config(ra_settings)
411413 end
412414end
413415
414- --- @enum RustAnalyzerCmd
415- local RustAnalyzerCmd = {
416- start = ' start' ,
417- stop = ' stop' ,
418- restart = ' restart' ,
419- reload_settings = ' reloadSettings' ,
420- target = ' target' ,
421- config = ' config' ,
422- }
423-
424- local function rust_analyzer_user_cmd (opts )
425- local fargs = opts .fargs
426- local cmd = table.remove (fargs , 1 )
427- --- @cast cmd RustAnalyzerCmd
428- if cmd == RustAnalyzerCmd .start then
429- M .start ()
430- elseif cmd == RustAnalyzerCmd .stop then
431- M .stop ()
432- elseif cmd == RustAnalyzerCmd .restart then
433- M .restart ()
434- elseif cmd == RustAnalyzerCmd .reload_settings then
435- M .reload_settings ()
436- elseif cmd == RustAnalyzerCmd .target then
437- local target_arch = fargs [1 ]
438- M .set_target_arch (nil , target_arch )
439- elseif cmd == RustAnalyzerCmd .config then
440- local ra_settings_str = vim .iter (fargs ):join (' ' )
441- --- @diagnostic disable-next-line : param-type-mismatch
442- local f = load (' return ' .. ra_settings_str )
443- --- @diagnostic disable-next-line : param-type-mismatch
444- local ok , ra_settings = pcall (f )
445- if not ok or type (ra_settings ) ~= ' table' then
446- return vim .notify (' RustAnalyzer config: invalid Lua table.\n ' .. ra_settings_str , vim .log .levels .ERROR )
447- end
448- M .set_config (ra_settings )
449- end
450- end
451-
452- vim .api .nvim_create_user_command (' RustAnalyzer' , rust_analyzer_user_cmd , {
453- nargs = ' +' ,
454- desc = ' Starts, stops the rust-analyzer LSP client or changes the target' ,
455- complete = function (arg_lead , cmdline , _ )
456- local clients = rust_analyzer .get_active_rustaceanvim_clients ()
457- --- @type RustAnalyzerCmd[]
458- local commands = # clients == 0 and { ' start' } or { ' stop' , ' restart' , ' reloadSettings' , ' target' , ' config' }
459- if cmdline :match (' ^RustAnalyzer%s+%w*$' ) then
460- return vim .tbl_filter (function (command )
461- return command :find (arg_lead ) ~= nil
462- end , commands )
463- end
464- end ,
465- })
466-
467416return M
0 commit comments