Skip to content

Commit 6637583

Browse files
committed
fix(lsp): create RustAnalyzer command ie auto_attach is disabled
1 parent bb57d9a commit 6637583

File tree

2 files changed

+64
-59
lines changed

2 files changed

+64
-59
lines changed

ftplugin/rust.lua

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,68 @@ end
5454

5555
vim.g.loaded_rustaceanvim = true
5656

57+
local bufnr = vim.api.nvim_get_current_buf()
58+
59+
---@enum RustAnalyzerCmd
60+
local RustAnalyzerCmd = {
61+
start = 'start',
62+
stop = 'stop',
63+
restart = 'restart',
64+
reload_settings = 'reloadSettings',
65+
target = 'target',
66+
config = 'config',
67+
}
68+
69+
local function rust_analyzer_user_cmd(opts)
70+
local fargs = opts.fargs
71+
local cmd = table.remove(fargs, 1)
72+
local lsp = require('rustaceanvim.lsp')
73+
---@cast cmd RustAnalyzerCmd
74+
if cmd == RustAnalyzerCmd.start then
75+
lsp.start(bufnr)
76+
elseif cmd == RustAnalyzerCmd.stop then
77+
lsp.stop(bufnr)
78+
elseif cmd == RustAnalyzerCmd.restart then
79+
lsp.restart(bufnr)
80+
elseif cmd == RustAnalyzerCmd.reload_settings then
81+
lsp.reload_settings(bufnr)
82+
elseif cmd == RustAnalyzerCmd.target then
83+
local target_arch = fargs[1]
84+
lsp.set_target_arch(bufnr, target_arch)
85+
elseif cmd == RustAnalyzerCmd.config then
86+
local ra_settings_str = vim.iter(fargs):join(' ')
87+
---@diagnostic disable-next-line: param-type-mismatch
88+
local f = load('return ' .. ra_settings_str)
89+
---@diagnostic disable-next-line: param-type-mismatch
90+
local ok, ra_settings = pcall(f)
91+
if not ok or type(ra_settings) ~= 'table' then
92+
return vim.notify('RustAnalyzer config: invalid Lua table.\n' .. ra_settings_str, vim.log.levels.ERROR)
93+
end
94+
lsp.set_config(bufnr, ra_settings)
95+
end
96+
end
97+
98+
vim.api.nvim_buf_create_user_command(bufnr, 'RustAnalyzer', rust_analyzer_user_cmd, {
99+
nargs = '+',
100+
desc = 'Starts, stops the rust-analyzer LSP client or changes the target',
101+
complete = function(arg_lead, cmdline, _)
102+
local rust_analyzer = require('rustaceanvim.rust_analyzer')
103+
local clients = rust_analyzer.get_active_rustaceanvim_clients()
104+
---@type RustAnalyzerCmd[]
105+
local commands = #clients == 0 and { 'start' } or { 'stop', 'restart', 'reloadSettings', 'target', 'config' }
106+
if cmdline:match('^RustAnalyzer%s+%w*$') then
107+
return vim.tbl_filter(function(command)
108+
return command:find(arg_lead) ~= nil
109+
end, commands)
110+
end
111+
end,
112+
})
113+
57114
local auto_attach = config.server.auto_attach
58115
if type(auto_attach) == 'function' then
59-
local bufnr = vim.api.nvim_get_current_buf()
60116
auto_attach = auto_attach(bufnr)
61117
end
62118

63119
if auto_attach then
64-
require('rustaceanvim.lsp').start()
120+
require('rustaceanvim.lsp').start(bufnr)
65121
end

lua/rustaceanvim/lsp/init.lua

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,10 @@ end
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)
353354
end
354355

355356
---Reload settings for the LSP client.
@@ -395,9 +396,10 @@ M.set_target_arch = function(bufnr, target)
395396
end)
396397
end
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
412414
end
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-
467416
return M

0 commit comments

Comments
 (0)