-
-
Notifications
You must be signed in to change notification settings - Fork 92
perf: optimize target_arch switching #548
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
Changes from 5 commits
b9601a2
dd8f57c
f63ac83
8a7b846
5d93ea2
f75ce6e
d18d5ef
1ea2cc8
350e663
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,18 +5,82 @@ local os = require('rustaceanvim.os') | |
---@class rustaceanvim.rust-analyzer.ClientAdapter | ||
local M = {} | ||
|
||
--- Default target value for rustc when no specific target is provided. | ||
--- Used as a fallback to let rustc determine the appropriate target based on the OS. | ||
DEFAULT_RUSTC_TARGET = 'OS' | ||
|
||
---Local rustc targets cache | ||
local rustc_targets_cache = nil | ||
|
||
M.load_os_rustc_target = function() | ||
mrcjkb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
vim.system({ 'rustc', '-Vv' }, { text = true }, function(result) | ||
if result.code == 0 then | ||
for line in result.stdout:gmatch('[^\r\n]+') do | ||
local host = line:match('^host:%s*(.+)$') | ||
if host then | ||
M.os_rustc_target = host | ||
break | ||
end | ||
end | ||
end | ||
end) | ||
end | ||
|
||
---Handles retrieving rustc target architectures and running the passed in callback | ||
---to perform certain actions using the retrieved targets. | ||
---@param callback fun(targets: string[]) | ||
M.with_rustc_target_architectures = function(callback) | ||
mrcjkb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if rustc_targets_cache then | ||
return callback(rustc_targets_cache) | ||
end | ||
vim.system( | ||
{ 'rustc', '--print', 'target-list' }, | ||
{ text = true }, | ||
---@param result vim.SystemCompleted | ||
function(result) | ||
if result.code ~= 0 then | ||
error('Failed to retrieve rustc targets: ' .. result.stderr) | ||
end | ||
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 | ||
) | ||
end | ||
|
||
---@class rustaceanvim.lsp.get_clients.Filter: vim.lsp.get_clients.Filter | ||
---@field exclude_rustc_target? string Cargo target triple (e.g., 'x86_64-unknown-linux-gnu') 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? rustaceanvim.lsp.get_clients.Filter | ||
---@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 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) | ||
if filter and filter.exclude_rustc_target then | ||
clients = vim.tbl_filter(function(client) | ||
local cargo_target = vim.tbl_get(client, 'config', 'settings', 'rust-analyzer', 'cargo', 'target') | ||
if filter.exclude_rustc_target == DEFAULT_RUSTC_TARGET and cargo_target == nil then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue: tip: If you have Nix installed, I recommend you enter a nix devShell ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks! done |
||
return false | ||
end | ||
return cargo_target ~= filter.exclude_rustc_target | ||
end, clients) | ||
end | ||
|
||
return clients | ||
end | ||
|
||
---@param method string LSP method name | ||
|
@@ -64,7 +128,6 @@ M.get_client_for_file = function(file_path, method) | |
end | ||
end | ||
end | ||
|
||
---@param method string LSP method name | ||
---@param params table|nil Parameters to send to the server | ||
M.notify = function(method, params) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: The
stop
function shouldn't set a rustc target by default, otherwise it will always try to filter, even when we don't need it to.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah make sense. thanks