|
3 | 3 | //! `ide` crate.
|
4 | 4 |
|
5 | 5 | use std::{
|
| 6 | + fs, |
6 | 7 | io::Write as _,
|
7 | 8 | process::{self, Stdio},
|
8 | 9 | sync::Arc,
|
@@ -138,10 +139,10 @@ pub(crate) fn fetch_dependency_list(
|
138 | 139 | .into_iter()
|
139 | 140 | .filter_map(|it| {
|
140 | 141 | let root_file_path = state.file_id_to_file_path(it.root_file_id);
|
141 |
| - crate_path(it.name.as_ref(), root_file_path).map(|path| CrateInfoResult { |
| 142 | + crate_path(root_file_path).and_then(to_url).map(|path| CrateInfoResult { |
142 | 143 | name: it.name,
|
143 | 144 | version: it.version,
|
144 |
| - path: path.to_string(), |
| 145 | + path, |
145 | 146 | })
|
146 | 147 | })
|
147 | 148 | .collect();
|
@@ -1933,33 +1934,32 @@ fn run_rustfmt(
|
1933 | 1934 | }
|
1934 | 1935 | }
|
1935 | 1936 |
|
1936 |
| -/// Searches for the directory of a Rust crate with a given name in the directory tree |
1937 |
| -/// of the root file of that crate. |
| 1937 | +/// Searches for the directory of a Rust crate given this crate's root file path. |
1938 | 1938 | ///
|
1939 | 1939 | /// # Arguments
|
1940 | 1940 | ///
|
1941 |
| -/// * `crate_name`: The name of the crate to search for. This should be a `Some` value if |
1942 |
| -/// a crate name has been specified, or `None` if no crate name has been specified. |
1943 | 1941 | /// * `root_file_path`: The path to the root file of the crate.
|
1944 | 1942 | ///
|
1945 | 1943 | /// # Returns
|
1946 | 1944 | ///
|
1947 | 1945 | /// An `Option` value representing the path to the directory of the crate with the given
|
1948 | 1946 | /// name, if such a crate is found. If no crate with the given name is found, this function
|
1949 | 1947 | /// returns `None`.
|
1950 |
| -fn crate_path(crate_name: Option<&String>, root_file_path: VfsPath) -> Option<VfsPath> { |
1951 |
| - crate_name.and_then(|crate_name| { |
1952 |
| - let mut root_path = root_file_path; |
1953 |
| - while let Some(path) = root_path.parent() { |
1954 |
| - if let Some((name, _)) = path.name_and_extension() { |
1955 |
| - if name.starts_with(crate_name.as_str()) { |
1956 |
| - return Some(path); |
1957 |
| - } |
1958 |
| - } else { |
1959 |
| - break; |
1960 |
| - } |
1961 |
| - root_path = path; |
| 1948 | +fn crate_path(root_file_path: VfsPath) -> Option<VfsPath> { |
| 1949 | + let mut current_dir = root_file_path.parent(); |
| 1950 | + while let Some(path) = current_dir { |
| 1951 | + let cargo_toml_path = path.join("../Cargo.toml")?; |
| 1952 | + if fs::metadata(cargo_toml_path.as_path()?).is_ok() { |
| 1953 | + let crate_path = cargo_toml_path.parent()?; |
| 1954 | + return Some(crate_path); |
1962 | 1955 | }
|
1963 |
| - None |
1964 |
| - }) |
| 1956 | + current_dir = path.parent(); |
| 1957 | + } |
| 1958 | + None |
| 1959 | +} |
| 1960 | + |
| 1961 | +fn to_url(path: VfsPath) -> Option<Url> { |
| 1962 | + let path = path.as_path()?; |
| 1963 | + let str_path = path.as_os_str().to_str()?; |
| 1964 | + Url::from_file_path(str_path).ok() |
1965 | 1965 | }
|
0 commit comments