diff options
author | ThiƩbaud Weksteen <[email protected]> | 2020-06-09 20:26:42 +0100 |
---|---|---|
committer | ThiƩbaud Weksteen <[email protected]> | 2020-06-15 12:11:53 +0100 |
commit | 44f28f65af15371cfe35e3599571e7217d6a9db1 (patch) | |
tree | 0219c79b467a53a8b2f24f996aa58733258314b4 | |
parent | 017331a53c1eeaa1253d2829165627bfa27dc124 (diff) |
Accept relative paths in rust-project.json
If a relative path is found as part of Crate.root_module or Root.path,
interpret it as relative to the location of the rust-project.json file.
-rw-r--r-- | crates/ra_project_model/src/lib.rs | 32 | ||||
-rw-r--r-- | crates/rust-analyzer/src/bin/main.rs | 8 | ||||
-rw-r--r-- | crates/rust-analyzer/src/config.rs | 6 | ||||
-rw-r--r-- | crates/rust-analyzer/src/main_loop.rs | 7 |
4 files changed, 31 insertions, 22 deletions
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index cb0e27dce..9541362f5 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs | |||
@@ -29,13 +29,7 @@ pub enum ProjectWorkspace { | |||
29 | /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`. | 29 | /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`. |
30 | Cargo { cargo: CargoWorkspace, sysroot: Sysroot }, | 30 | Cargo { cargo: CargoWorkspace, sysroot: Sysroot }, |
31 | /// Project workspace was manually specified using a `rust-project.json` file. | 31 | /// Project workspace was manually specified using a `rust-project.json` file. |
32 | Json { project: JsonProject }, | 32 | Json { project: JsonProject, project_location: PathBuf }, |
33 | } | ||
34 | |||
35 | impl From<JsonProject> for ProjectWorkspace { | ||
36 | fn from(project: JsonProject) -> ProjectWorkspace { | ||
37 | ProjectWorkspace::Json { project } | ||
38 | } | ||
39 | } | 33 | } |
40 | 34 | ||
41 | /// `PackageRoot` describes a package root folder. | 35 | /// `PackageRoot` describes a package root folder. |
@@ -164,10 +158,15 @@ impl ProjectWorkspace { | |||
164 | format!("Failed to open json file {}", project_json.display()) | 158 | format!("Failed to open json file {}", project_json.display()) |
165 | })?; | 159 | })?; |
166 | let reader = BufReader::new(file); | 160 | let reader = BufReader::new(file); |
161 | let project_location = match project_json.parent() { | ||
162 | Some(parent) => PathBuf::from(parent), | ||
163 | None => PathBuf::new(), | ||
164 | }; | ||
167 | ProjectWorkspace::Json { | 165 | ProjectWorkspace::Json { |
168 | project: from_reader(reader).with_context(|| { | 166 | project: from_reader(reader).with_context(|| { |
169 | format!("Failed to deserialize json file {}", project_json.display()) | 167 | format!("Failed to deserialize json file {}", project_json.display()) |
170 | })?, | 168 | })?, |
169 | project_location: project_location, | ||
171 | } | 170 | } |
172 | } | 171 | } |
173 | ProjectManifest::CargoToml(cargo_toml) => { | 172 | ProjectManifest::CargoToml(cargo_toml) => { |
@@ -200,9 +199,11 @@ impl ProjectWorkspace { | |||
200 | /// the root is a member of the current workspace | 199 | /// the root is a member of the current workspace |
201 | pub fn to_roots(&self) -> Vec<PackageRoot> { | 200 | pub fn to_roots(&self) -> Vec<PackageRoot> { |
202 | match self { | 201 | match self { |
203 | ProjectWorkspace::Json { project } => { | 202 | ProjectWorkspace::Json { project, project_location } => project |
204 | project.roots.iter().map(|r| PackageRoot::new_member(r.path.clone())).collect() | 203 | .roots |
205 | } | 204 | .iter() |
205 | .map(|r| PackageRoot::new_member(project_location.join(&r.path))) | ||
206 | .collect(), | ||
206 | ProjectWorkspace::Cargo { cargo, sysroot } => cargo | 207 | ProjectWorkspace::Cargo { cargo, sysroot } => cargo |
207 | .packages() | 208 | .packages() |
208 | .map(|pkg| PackageRoot { | 209 | .map(|pkg| PackageRoot { |
@@ -219,7 +220,7 @@ impl ProjectWorkspace { | |||
219 | 220 | ||
220 | pub fn proc_macro_dylib_paths(&self) -> Vec<PathBuf> { | 221 | pub fn proc_macro_dylib_paths(&self) -> Vec<PathBuf> { |
221 | match self { | 222 | match self { |
222 | ProjectWorkspace::Json { project } => project | 223 | ProjectWorkspace::Json { project, .. } => project |
223 | .crates | 224 | .crates |
224 | .iter() | 225 | .iter() |
225 | .filter_map(|krate| krate.proc_macro_dylib_path.as_ref()) | 226 | .filter_map(|krate| krate.proc_macro_dylib_path.as_ref()) |
@@ -235,7 +236,7 @@ impl ProjectWorkspace { | |||
235 | 236 | ||
236 | pub fn n_packages(&self) -> usize { | 237 | pub fn n_packages(&self) -> usize { |
237 | match self { | 238 | match self { |
238 | ProjectWorkspace::Json { project } => project.crates.len(), | 239 | ProjectWorkspace::Json { project, .. } => project.crates.len(), |
239 | ProjectWorkspace::Cargo { cargo, sysroot } => { | 240 | ProjectWorkspace::Cargo { cargo, sysroot } => { |
240 | cargo.packages().len() + sysroot.crates().len() | 241 | cargo.packages().len() + sysroot.crates().len() |
241 | } | 242 | } |
@@ -251,13 +252,14 @@ impl ProjectWorkspace { | |||
251 | ) -> CrateGraph { | 252 | ) -> CrateGraph { |
252 | let mut crate_graph = CrateGraph::default(); | 253 | let mut crate_graph = CrateGraph::default(); |
253 | match self { | 254 | match self { |
254 | ProjectWorkspace::Json { project } => { | 255 | ProjectWorkspace::Json { project, project_location } => { |
255 | let crates: FxHashMap<_, _> = project | 256 | let crates: FxHashMap<_, _> = project |
256 | .crates | 257 | .crates |
257 | .iter() | 258 | .iter() |
258 | .enumerate() | 259 | .enumerate() |
259 | .filter_map(|(seq_index, krate)| { | 260 | .filter_map(|(seq_index, krate)| { |
260 | let file_id = load(&krate.root_module)?; | 261 | let file_path = project_location.join(&krate.root_module); |
262 | let file_id = load(&file_path)?; | ||
261 | let edition = match krate.edition { | 263 | let edition = match krate.edition { |
262 | json_project::Edition::Edition2015 => Edition::Edition2015, | 264 | json_project::Edition::Edition2015 => Edition::Edition2015, |
263 | json_project::Edition::Edition2018 => Edition::Edition2018, | 265 | json_project::Edition::Edition2018 => Edition::Edition2018, |
@@ -540,7 +542,7 @@ impl ProjectWorkspace { | |||
540 | ProjectWorkspace::Cargo { cargo, .. } => { | 542 | ProjectWorkspace::Cargo { cargo, .. } => { |
541 | Some(cargo.workspace_root()).filter(|root| path.starts_with(root)) | 543 | Some(cargo.workspace_root()).filter(|root| path.starts_with(root)) |
542 | } | 544 | } |
543 | ProjectWorkspace::Json { project: JsonProject { roots, .. } } => roots | 545 | ProjectWorkspace::Json { project: JsonProject { roots, .. }, .. } => roots |
544 | .iter() | 546 | .iter() |
545 | .find(|root| path.starts_with(&root.path)) | 547 | .find(|root| path.starts_with(&root.path)) |
546 | .map(|root| root.path.as_ref()), | 548 | .map(|root| root.path.as_ref()), |
diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index 8d071ab1c..99e3f7173 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs | |||
@@ -108,11 +108,11 @@ fn run_server() -> Result<()> { | |||
108 | config.update(value); | 108 | config.update(value); |
109 | } | 109 | } |
110 | config.update_caps(&initialize_params.capabilities); | 110 | config.update_caps(&initialize_params.capabilities); |
111 | let cwd = std::env::current_dir()?; | ||
112 | config.root_path = | ||
113 | initialize_params.root_uri.and_then(|it| it.to_file_path().ok()).unwrap_or(cwd); | ||
111 | 114 | ||
112 | if config.linked_projects.is_empty() { | 115 | if config.linked_projects.is_empty() { |
113 | let cwd = std::env::current_dir()?; | ||
114 | let root = | ||
115 | initialize_params.root_uri.and_then(|it| it.to_file_path().ok()).unwrap_or(cwd); | ||
116 | let workspace_roots = initialize_params | 116 | let workspace_roots = initialize_params |
117 | .workspace_folders | 117 | .workspace_folders |
118 | .map(|workspaces| { | 118 | .map(|workspaces| { |
@@ -122,7 +122,7 @@ fn run_server() -> Result<()> { | |||
122 | .collect::<Vec<_>>() | 122 | .collect::<Vec<_>>() |
123 | }) | 123 | }) |
124 | .filter(|workspaces| !workspaces.is_empty()) | 124 | .filter(|workspaces| !workspaces.is_empty()) |
125 | .unwrap_or_else(|| vec![root]); | 125 | .unwrap_or_else(|| vec![config.root_path.clone()]); |
126 | 126 | ||
127 | config.linked_projects = ProjectManifest::discover_all(&workspace_roots) | 127 | config.linked_projects = ProjectManifest::discover_all(&workspace_roots) |
128 | .into_iter() | 128 | .into_iter() |
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 1253db836..0e7a937a0 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs | |||
@@ -38,12 +38,13 @@ pub struct Config { | |||
38 | 38 | ||
39 | pub with_sysroot: bool, | 39 | pub with_sysroot: bool, |
40 | pub linked_projects: Vec<LinkedProject>, | 40 | pub linked_projects: Vec<LinkedProject>, |
41 | pub root_path: PathBuf, | ||
41 | } | 42 | } |
42 | 43 | ||
43 | #[derive(Debug, Clone)] | 44 | #[derive(Debug, Clone)] |
44 | pub enum LinkedProject { | 45 | pub enum LinkedProject { |
45 | ProjectManifest(ProjectManifest), | 46 | ProjectManifest(ProjectManifest), |
46 | JsonProject(JsonProject), | 47 | InlineJsonProject(JsonProject), |
47 | } | 48 | } |
48 | 49 | ||
49 | impl From<ProjectManifest> for LinkedProject { | 50 | impl From<ProjectManifest> for LinkedProject { |
@@ -54,7 +55,7 @@ impl From<ProjectManifest> for LinkedProject { | |||
54 | 55 | ||
55 | impl From<JsonProject> for LinkedProject { | 56 | impl From<JsonProject> for LinkedProject { |
56 | fn from(v: JsonProject) -> Self { | 57 | fn from(v: JsonProject) -> Self { |
57 | LinkedProject::JsonProject(v) | 58 | LinkedProject::InlineJsonProject(v) |
58 | } | 59 | } |
59 | } | 60 | } |
60 | 61 | ||
@@ -167,6 +168,7 @@ impl Default for Config { | |||
167 | lens: LensConfig::default(), | 168 | lens: LensConfig::default(), |
168 | hover: HoverConfig::default(), | 169 | hover: HoverConfig::default(), |
169 | linked_projects: Vec::new(), | 170 | linked_projects: Vec::new(), |
171 | root_path: PathBuf::new(), | ||
170 | } | 172 | } |
171 | } | 173 | } |
172 | } | 174 | } |
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 8ec571b70..cc9bb1726 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs | |||
@@ -121,7 +121,12 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> { | |||
121 | }) | 121 | }) |
122 | .ok() | 122 | .ok() |
123 | } | 123 | } |
124 | LinkedProject::JsonProject(it) => Some(it.clone().into()), | 124 | LinkedProject::InlineJsonProject(it) => { |
125 | Some(ra_project_model::ProjectWorkspace::Json { | ||
126 | project: it.clone(), | ||
127 | project_location: config.root_path.clone(), | ||
128 | }) | ||
129 | } | ||
125 | }) | 130 | }) |
126 | .collect::<Vec<_>>() | 131 | .collect::<Vec<_>>() |
127 | }; | 132 | }; |