From 9a6279953c82d01b58825a46ede032ab246a5983 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Fri, 2 Feb 2024 12:36:20 +0100 Subject: fix(rust-analyzer): re-analyzing cargo deps (#2995) 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. --- lua/lspconfig/server_configurations/rust_analyzer.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lua/lspconfig/server_configurations/rust_analyzer.lua') diff --git a/lua/lspconfig/server_configurations/rust_analyzer.lua b/lua/lspconfig/server_configurations/rust_analyzer.lua index 22575b47..dd70c618 100644 --- a/lua/lspconfig/server_configurations/rust_analyzer.lua +++ b/lua/lspconfig/server_configurations/rust_analyzer.lua @@ -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 -- cgit v1.2.3-70-g09d2