Skip to content

Commit

Permalink
rust-analyzer: Fix is_library
Browse files Browse the repository at this point in the history
I'm not that well versed in Lua, but the previous check

   if fname:sub(1, #item) == item

didn't really work as expected and I was still experiencing high CPU
usage when using the `go to definition` functionality. After adding some
print statements for debugging, I noticed that only a few strings were
removed from the base dir and thus the comparison was failing. Perhaps
it might work differently in other operating systems, but my Linux
machine it did not work correctly.

I replaced this check with `util.path.is_descendant` which now works
correctly, as well as added another path to check when git repositories
are used as cargo dependencies.
  • Loading branch information
jeremija committed Feb 2, 2024
1 parent ac530df commit e8b2087
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lua/lspconfig/server_configurations/rust_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ local function is_library(fname)
local user_home = util.path.sanitize(vim.env.HOME)
local cargo_home = os.getenv 'CARGO_HOME' or util.path.join(user_home, '.cargo')
local registry = util.path.join(cargo_home, 'registry', 'src')
local git_registry = util.path.join(cargo_home, 'git', 'checkouts')

local rustup_home = os.getenv 'RUSTUP_HOME' or util.path.join(user_home, '.rustup')
local toolchains = util.path.join(rustup_home, 'toolchains')

for _, item in ipairs { toolchains, registry } do
if fname:sub(1, #item) == item then
for _, item in ipairs { toolchains, registry, git_registry } do
if util.path.is_descendant(item, fname) then
local clients = vim.lsp.get_active_clients { name = 'rust_analyzer' }
return #clients > 0 and clients[#clients].config.root_dir or nil
end
Expand Down

0 comments on commit e8b2087

Please sign in to comment.