Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimize target_arch switching #548

Merged
merged 9 commits into from
Oct 28, 2024
52 changes: 24 additions & 28 deletions lua/rustaceanvim/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ local function with_rustc_target_architectures(callback)
end

---LSP restart internal implementations
---@param bufnr? number
---@param target? string|nil Optional target architecture. Clients with target won't be restarted.
---@param callback? fun(client: vim.lsp.Client) Optional callback to run for each client before restarting.
---@return number|nil client_id
local function restart(bufnr, callback)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local clients = M.stop(bufnr)
local function restart(target, callback)
local bufnr = vim.api.nvim_get_current_buf()
local clients = M.stop(bufnr, target)
local timer, _, _ = vim.uv.new_timer()
if not timer then
vim.notify('Failed to init timer for LSP client restart.', vim.log.levels.ERROR)
Expand Down Expand Up @@ -285,10 +285,11 @@ end

---Stop the LSP client.
---@param bufnr? number The buffer number, defaults to the current buffer
---@param target_arch? string Optional target architecture. Clients with target won't be stopped.
---@return vim.lsp.Client[] clients A list of clients that will be stopped
M.stop = function(bufnr)
M.stop = function(bufnr, target_arch)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local clients = rust_analyzer.get_active_rustaceanvim_clients(bufnr)
local clients = rust_analyzer.get_active_rustaceanvim_clients(bufnr, { rustc_target = target_arch })
vim.lsp.stop_client(clients)
if type(clients) == 'table' then
---@cast clients vim.lsp.Client[]
Expand All @@ -303,10 +304,9 @@ M.stop = function(bufnr)
end

---Reload settings for the LSP client.
---@param bufnr? number The buffer number, defaults to the current buffer
---@return vim.lsp.Client[] clients A list of clients that will be have their settings reloaded
M.reload_settings = function(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
M.reload_settings = function()
local bufnr = vim.api.nvim_get_current_buf()
local clients = rust_analyzer.get_active_rustaceanvim_clients(bufnr)
---@cast clients vim.lsp.Client[]
for _, client in ipairs(clients) do
Expand All @@ -320,32 +320,29 @@ M.reload_settings = function(bufnr)
return clients
end

---Updates the target architecture setting for the LSP client associated with the given buffer.
---@param bufnr? number The buffer number, defaults to the current buffer
---@param target? string The target architecture. Defaults to nil(the current buffer's target if not provided).
M.set_target_arch = function(bufnr, target)
---Updates LSP client target architecture setting.
---@param target_arch? string The target architecture, defaults to nil(the current buffer's target if not provided).
M.set_target_arch = function(target_arch)
---@param client vim.lsp.Client
restart(bufnr, function(client)
-- Get current user's rust-analyzer target
local current_target = vim.tbl_get(client, 'config', 'settings', 'rust-analyzer', 'cargo', 'target')

if not target then
restart(target_arch, function(client)
if not target_arch then
-- Get current user's rust-analyzer target
local current_target = vim.tbl_get(client, 'config', 'settings', 'rust-analyzer', 'cargo', 'target')
if not current_target then
vim.notify('Using default OS target architecture.', vim.log.levels.INFO)
else
vim.notify('Target architecture is already set to the default OS target.', vim.log.levels.INFO)
end
return
end

with_rustc_target_architectures(function(rustc_targets)
if target == nil or rustc_targets[target] then
client.settings['rust-analyzer'].cargo.target = target
if target_arch == nil or rustc_targets[target_arch] then
client.settings['rust-analyzer'].cargo.target = target_arch
client.notify('workspace/didChangeConfiguration', { settings = client.config.settings })
vim.notify('Target architecture updated successfully to: ' .. target, vim.log.levels.INFO)
vim.notify('Target architecture updated successfully to: ' .. target_arch, vim.log.levels.INFO)
return
else
vim.notify('Invalid target architecture provided: ' .. tostring(target), vim.log.levels.ERROR)
vim.notify('Invalid target architecture provided: ' .. tostring(target_arch), vim.log.levels.ERROR)
return
end
end)
Expand All @@ -354,10 +351,9 @@ end

---Restart the LSP client.
---Fails silently if the buffer's filetype is not one of the filetypes specified in the config.
---@param bufnr? number The buffer number (optional), defaults to the current buffer
---@return number|nil client_id The LSP client ID after restart
M.restart = function(bufnr)
M.restart(bufnr)
M.restart = function()
return restart()
end

---@enum RustAnalyzerCmd
Expand All @@ -372,7 +368,6 @@ local RustAnalyzerCmd = {
local function rust_analyzer_cmd(opts)
local fargs = opts.fargs
local cmd = fargs[1]
local arch = fargs[2]
---@cast cmd RustAnalyzerCmd
if cmd == RustAnalyzerCmd.start then
M.start()
Expand All @@ -383,7 +378,8 @@ local function rust_analyzer_cmd(opts)
elseif cmd == RustAnalyzerCmd.reload_settings then
M.reload_settings()
elseif cmd == RustAnalyzerCmd.target then
M.set_target_arch(nil, arch)
local target_arch = fargs[2]
M.set_target_arch(target_arch)
end
end

Expand Down
32 changes: 23 additions & 9 deletions lua/rustaceanvim/rust_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,33 @@ local os = require('rustaceanvim.os')
---@class rustaceanvim.rust-analyzer.ClientAdapter
local M = {}

---@class GetClientFilter
---@field filter? vim.lsp.get_clients.Filter
---@field rustc_target? string Optional cargo target to filter rust-analyzer clients

---@param bufnr number | nil 0 for the current buffer, `nil` for no buffer filter
---@param filter? vim.lsp.get_clients.Filter
---@param filter? GetClientFilter
---@return vim.lsp.Client[]
M.get_active_rustaceanvim_clients = function(bufnr, filter)
---@type vim.lsp.get_clients.Filter
filter = vim.tbl_deep_extend('force', filter or {}, {
local client_filter = vim.tbl_deep_extend('force', filter and filter.filter or {}, {
name = 'rust-analyzer',
})
if bufnr then
filter.bufnr = bufnr
client_filter.bufnr = bufnr
end
return vim.lsp.get_clients(filter)
local clients = vim.lsp.get_clients(client_filter)
clients = vim.tbl_filter(function(client)
local rust_settings = client.config and client.config.settings and client.config.settings['rust-analyzer']
local cargo_target = rust_settings and rust_settings.cargo and rust_settings.cargo.target
-- If no filter is provided or filter.rustc_target is nil, include the client only if cargo_target exists
if not filter or not filter.rustc_target then
return cargo_target ~= nil
end
-- Include the client if cargo_target differs from filter.rustc_target
return cargo_target ~= filter.rustc_target
end, clients)
return clients
end

---@param method string LSP method name
Expand All @@ -30,7 +45,7 @@ M.any_buf_request = function(method, params, handler)
return
end
-- No buffer found. Try any client.
for _, client in ipairs(M.get_active_rustaceanvim_clients(nil, { method = method })) do
for _, client in ipairs(M.get_active_rustaceanvim_clients(nil, { filter = { method = method } })) do
client.request(method, params, handler, 0)
end
end
Expand All @@ -46,7 +61,7 @@ M.buf_request = function(bufnr, method, params, handler)
bufnr = vim.api.nvim_get_current_buf()
end
local client_found = false
for _, client in ipairs(M.get_active_rustaceanvim_clients(bufnr, { method = method })) do
for _, client in ipairs(M.get_active_rustaceanvim_clients(bufnr, { filter = { method = method } })) do
client.request(method, params, handler, bufnr)
client_found = true
end
Expand All @@ -57,19 +72,18 @@ end
---@param method string LSP method name
---@return vim.lsp.Client|nil
M.get_client_for_file = function(file_path, method)
for _, client in ipairs(M.get_active_rustaceanvim_clients(nil, { method = method })) do
for _, client in ipairs(M.get_active_rustaceanvim_clients(nil, { filter = { method = method } })) do
local root_dir = client.config.root_dir
if root_dir and vim.startswith(os.normalize_path_on_windows(file_path), root_dir) then
return client
end
end
end

---@param method string LSP method name
---@param params table|nil Parameters to send to the server
M.notify = function(method, params)
local client_found = false
for _, client in ipairs(M.get_active_rustaceanvim_clients(0, { method = method })) do
for _, client in ipairs(M.get_active_rustaceanvim_clients(0, { filter = { method = method } })) do
client.notify(method, params)
client_found = true
end
Expand Down
Loading