@@ -5,7 +5,7 @@ local compat = require('rustaceanvim.compat')
55local types = require (' rustaceanvim.types.internal' )
66local rust_analyzer = require (' rustaceanvim.rust_analyzer' )
77local server_status = require (' rustaceanvim.server_status' )
8- local joinpath = compat . joinpath
8+ local cargo = require ( ' rustaceanvim.cargo ' )
99
1010local function override_apply_text_edits ()
1111 local old_func = vim .lsp .util .apply_text_edits
@@ -17,69 +17,6 @@ local function override_apply_text_edits()
1717 end
1818end
1919
20- --- Checks if there is an active client for file_name and returns its root directory if found.
21- --- @param file_name string
22- --- @return string | nil root_dir The root directory of the active client for file_name (if there is one )
23- local function get_mb_active_client_root (file_name )
24- --- @diagnostic disable-next-line : missing-parameter
25- local cargo_home = compat .uv .os_getenv (' CARGO_HOME' ) or joinpath (vim .env .HOME , ' .cargo' )
26- local registry = joinpath (cargo_home , ' registry' , ' src' )
27-
28- --- @diagnostic disable-next-line : missing-parameter
29- local rustup_home = compat .uv .os_getenv (' RUSTUP_HOME' ) or joinpath (vim .env .HOME , ' .rustup' )
30- local toolchains = joinpath (rustup_home , ' toolchains' )
31-
32- for _ , item in ipairs { toolchains , registry } do
33- if file_name :sub (1 , # item ) == item then
34- local clients = rust_analyzer .get_active_rustaceanvim_clients ()
35- return clients and # clients > 0 and clients [# clients ].config .root_dir or nil
36- end
37- end
38- end
39-
40- --- @param file_name string
41- --- @return string | nil root_dir
42- local function get_root_dir (file_name )
43- local reuse_active = get_mb_active_client_root (file_name )
44- if reuse_active then
45- return reuse_active
46- end
47- local cargo_crate_dir = vim .fs .dirname (vim .fs .find ({ ' Cargo.toml' }, {
48- upward = true ,
49- path = vim .fs .dirname (file_name ),
50- })[1 ])
51- local cargo_workspace_dir = nil
52- if vim .fn .executable (' cargo' ) == 1 then
53- local cmd = { ' cargo' , ' metadata' , ' --no-deps' , ' --format-version' , ' 1' }
54- if cargo_crate_dir ~= nil then
55- cmd [# cmd + 1 ] = ' --manifest-path'
56- cmd [# cmd + 1 ] = joinpath (cargo_crate_dir , ' Cargo.toml' )
57- end
58- local cargo_metadata = ' '
59- local cm = vim .fn .jobstart (cmd , {
60- on_stdout = function (_ , d , _ )
61- cargo_metadata = table.concat (d , ' \n ' )
62- end ,
63- stdout_buffered = true ,
64- })
65- if cm > 0 then
66- cm = vim .fn .jobwait ({ cm })[1 ]
67- else
68- cm = - 1
69- end
70- if cm == 0 then
71- cargo_workspace_dir = vim .fn .json_decode (cargo_metadata )[' workspace_root' ]
72- --- @cast cargo_workspace_dir string
73- end
74- end
75- return cargo_workspace_dir
76- or cargo_crate_dir
77- or vim .fs .dirname (vim .fs .find ({ ' rust-project.json' }, {
78- upward = true ,
79- path = vim .fs .dirname (file_name ),
80- })[1 ])
81- end
82-
8320--- @param client lsp.Client
8421--- @param root_dir string
8522--- @return boolean
@@ -131,7 +68,7 @@ M.start = function(bufnr)
13168 local client_config = config .server
13269 --- @type LspStartConfig
13370 local lsp_start_config = vim .tbl_deep_extend (' force' , {}, client_config )
134- local root_dir = get_root_dir (vim .api .nvim_buf_get_name (bufnr ))
71+ local root_dir = cargo . get_root_dir (vim .api .nvim_buf_get_name (bufnr ))
13572 root_dir = root_dir and normalize_path (root_dir )
13673 lsp_start_config .root_dir = root_dir
13774 if not root_dir then
0 commit comments