Skip to content

Commit

Permalink
refactor: some minor cleanup + vimdoc generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Oct 17, 2024
1 parent 95715b2 commit 49101db
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 42 deletions.
5 changes: 3 additions & 2 deletions doc/rustaceanvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ Commands:
':RustAnalyzer restart' - Restart the LSP client.
':RustAnalyzer reloadSettings' - Reload settings for the LSP client.
':RustAnalyzer target <target_arch>' - Set the target architecture for the LSP client.

The ':RustAnalyzer target' command can take a valid rustc target, such as 'wasm32-unknown-unknown', or it can be left empty to set the LSP client to use the default target architecture for the operating system.
The ':RustAnalyzer target' command can take a valid rustc target,
such as 'wasm32-unknown-unknown', or it can be left empty to set the LSP client
to use the default target architecture for the operating system.

The ':RustLsp[!]' command is available after the LSP client has initialized.
It accepts the following subcommands:
Expand Down
78 changes: 39 additions & 39 deletions lua/rustaceanvim/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local function override_apply_text_edits()
end
end

---@param client lsp.Client
---@param client vim.lsp.Client
---@param root_dir string
---@return boolean
local function is_in_workspace(client, root_dir)
Expand Down Expand Up @@ -96,35 +96,41 @@ local function configure_file_watcher(server_cfg)
end
end

---Handles retrieving rustc target archs and running on_valid callback
---Handles retrieving rustc target architectures and running the passed in callback
---to perform certain actions using the retrieved targets.
---@param on_valid function(rustc_targets)
local function validate_rustc_target(on_valid)
local on_exit = function(result)
-- use cache if available.
if rustc_targets_cache then
return on_valid(rustc_targets_cache)
end
---@param callback fun(targets: string[])
local function with_rustc_target_architectures(callback)
vim.system(
{ 'rustc', '--print', 'target-list' },
{ text = true },
---@param result vim.SystemCompleted
function(result)
if rustc_targets_cache then
return callback(rustc_targets_cache)
end

if result.code ~= 0 then
error('Failed to retrieve rustc targets: ' .. result.stderr)
end
if result.code ~= 0 then
error('Failed to retrieve rustc targets: ' .. result.stderr)
end

rustc_targets_cache = {}
for line in result.stdout:gmatch('[^\r\n]+') do
rustc_targets_cache[line] = true
rustc_targets_cache = vim.iter(result.stdout:gmatch('[^\r\n]+')):fold(
{},
---@param acc table<string, boolean>
---@param target string
function(acc, target)
acc[target] = true
return acc
end
)

return callback(rustc_targets_cache)
end

return on_valid(rustc_targets_cache)
end

-- Call vim.system with on_exit callback to avoid blocking Neovim's event loop.
vim.system({ 'rustc', '--print', 'target-list' }, { text = true }, on_exit)
)
end

---LSP restart internal implementations
---@param bufnr? number
---@param callback? function(client) Optional callback to run for each client before restarting.
---@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()
Expand Down Expand Up @@ -282,30 +288,30 @@ end

---Stop the LSP client.
---@param bufnr? number The buffer number, defaults to the current buffer
---@return table[] clients A list of clients that will be stopped
---@return vim.lsp.Client[] clients A list of clients that will be stopped
M.stop = function(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local clients = rust_analyzer.get_active_rustaceanvim_clients(bufnr)
vim.lsp.stop_client(clients)
if type(clients) == 'table' then
---@cast clients lsp.Client[]
---@cast clients vim.lsp.Client[]
for _, client in ipairs(clients) do
server_status.reset_client_state(client.id)
end
else
---@cast clients lsp.Client
---@cast clients vim.lsp.Client
server_status.reset_client_state(clients.id)
end
return clients
end

---Reload settings for the LSP client.
---@param bufnr? number The buffer number, defaults to the current buffer
---@return table[] clients A list of clients that will be have their settings reloaded
---@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()
local clients = rust_analyzer.get_active_rustaceanvim_clients(bufnr)
---@cast clients lsp.Client[]
---@cast clients vim.lsp.Client[]
for _, client in ipairs(clients) do
local settings = get_start_settings(vim.api.nvim_buf_get_name(bufnr), client.config.root_dir, config.server)
---@diagnostic disable-next-line: inject-field
Expand All @@ -321,7 +327,8 @@ end
---@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)
local on_update_target = function(client)
---@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')

Expand All @@ -334,11 +341,8 @@ M.set_target_arch = function(bufnr, target)
return
end

---on_valid callback handles the main functionality in changing system's arch
---by checking if rustc targets contains user's target or if user's provided target is nil.
---Notifies user on unrecognized target arch request
local on_valid = function(rustc_tgs)
if target == nil or rustc_tgs[target] then
with_rustc_target_architectures(function(rustc_targets)
if target == nil or rustc_targets[target] then
client.settings['rust-analyzer'].cargo.target = target
client.notify('workspace/didChangeConfiguration', { settings = client.config.settings })
vim.notify('Target architecture updated successfully to: ' .. target, vim.log.levels.INFO)
Expand All @@ -347,12 +351,8 @@ M.set_target_arch = function(bufnr, target)
vim.notify('Invalid target architecture provided: ' .. tostring(target), vim.log.levels.ERROR)
return
end
end

return validate_rustc_target(on_valid)
end

restart(bufnr, on_update_target)
end)
end)
end

---Restart the LSP client.
Expand Down
2 changes: 1 addition & 1 deletion lua/rustaceanvim/rust_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ end

---@param file_path string Search for clients with a root_dir matching this file path
---@param method string LSP method name
---@return lsp.Client|nil
---@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
local root_dir = client.config.root_dir
Expand Down

0 comments on commit 49101db

Please sign in to comment.