diff options
author | Aleksey Kladov <[email protected]> | 2020-06-17 16:51:46 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-06-17 16:51:46 +0100 |
commit | dc90e0b5a5d4f2255d6f0b82204f26cbaafbdd46 (patch) | |
tree | f1fec822afdef76d83d56918ab5ce12697dd8943 /crates | |
parent | 931f3173992df6ac6b728fa9fa9a94d15781027e (diff) |
Better encapsulate reverse-mapping of files to cargo targets
We need to find a better way to do it...
CrateGraph by itself is fine, CargoWorkspace as well, but the mapping
between the two seems arbitrary...
Diffstat (limited to 'crates')
-rw-r--r-- | crates/rust-analyzer/src/cargo_target_spec.rs | 32 | ||||
-rw-r--r-- | crates/rust-analyzer/src/global_state.rs | 22 |
2 files changed, 30 insertions, 24 deletions
diff --git a/crates/rust-analyzer/src/cargo_target_spec.rs b/crates/rust-analyzer/src/cargo_target_spec.rs index 44f856f6b..5c22dce0d 100644 --- a/crates/rust-analyzer/src/cargo_target_spec.rs +++ b/crates/rust-analyzer/src/cargo_target_spec.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use ra_cfg::CfgExpr; | 3 | use ra_cfg::CfgExpr; |
4 | use ra_ide::{FileId, RunnableKind, TestId}; | 4 | use ra_ide::{FileId, RunnableKind, TestId}; |
5 | use ra_project_model::{self, ProjectWorkspace, TargetKind}; | 5 | use ra_project_model::{self, TargetKind}; |
6 | 6 | ||
7 | use crate::{global_state::GlobalStateSnapshot, Result}; | 7 | use crate::{global_state::GlobalStateSnapshot, Result}; |
8 | 8 | ||
@@ -89,27 +89,23 @@ impl CargoTargetSpec { | |||
89 | } | 89 | } |
90 | 90 | ||
91 | pub(crate) fn for_file( | 91 | pub(crate) fn for_file( |
92 | world: &GlobalStateSnapshot, | 92 | global_state_snapshot: &GlobalStateSnapshot, |
93 | file_id: FileId, | 93 | file_id: FileId, |
94 | ) -> Result<Option<CargoTargetSpec>> { | 94 | ) -> Result<Option<CargoTargetSpec>> { |
95 | let &crate_id = match world.analysis().crate_for(file_id)?.first() { | 95 | let crate_id = match global_state_snapshot.analysis().crate_for(file_id)?.first() { |
96 | Some(crate_id) => crate_id, | 96 | Some(crate_id) => *crate_id, |
97 | None => return Ok(None), | 97 | None => return Ok(None), |
98 | }; | 98 | }; |
99 | let file_id = world.analysis().crate_root(crate_id)?; | 99 | let (cargo_ws, target) = match global_state_snapshot.cargo_target_for_crate_root(crate_id) { |
100 | let path = world.file_id_to_path(file_id); | 100 | Some(it) => it, |
101 | let res = world.workspaces.iter().find_map(|ws| match ws { | 101 | None => return Ok(None), |
102 | ProjectWorkspace::Cargo { cargo, .. } => { | 102 | }; |
103 | let tgt = cargo.target_by_root(&path)?; | 103 | let res = CargoTargetSpec { |
104 | Some(CargoTargetSpec { | 104 | package: cargo_ws.package_flag(&cargo_ws[cargo_ws[target].package]), |
105 | package: cargo.package_flag(&cargo[cargo[tgt].package]), | 105 | target: cargo_ws[target].name.clone(), |
106 | target: cargo[tgt].name.clone(), | 106 | target_kind: cargo_ws[target].kind, |
107 | target_kind: cargo[tgt].kind, | 107 | }; |
108 | }) | 108 | Ok(Some(res)) |
109 | } | ||
110 | ProjectWorkspace::Json { .. } => None, | ||
111 | }); | ||
112 | Ok(res) | ||
113 | } | 109 | } |
114 | 110 | ||
115 | pub(crate) fn push_to(self, buf: &mut Vec<String>, kind: &RunnableKind) { | 111 | pub(crate) fn push_to(self, buf: &mut Vec<String>, kind: &RunnableKind) { |
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 73ca2a709..ef6c7d44d 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs | |||
@@ -15,7 +15,7 @@ use ra_flycheck::{Flycheck, FlycheckConfig}; | |||
15 | use ra_ide::{ | 15 | use ra_ide::{ |
16 | Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, | 16 | Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, |
17 | }; | 17 | }; |
18 | use ra_project_model::{ProcMacroClient, ProjectWorkspace}; | 18 | use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target}; |
19 | use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsTask, Watch}; | 19 | use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsTask, Watch}; |
20 | use relative_path::RelativePathBuf; | 20 | use relative_path::RelativePathBuf; |
21 | use stdx::format_to; | 21 | use stdx::format_to; |
@@ -28,7 +28,7 @@ use crate::{ | |||
28 | vfs_glob::{Glob, RustPackageFilterBuilder}, | 28 | vfs_glob::{Glob, RustPackageFilterBuilder}, |
29 | LspError, Result, | 29 | LspError, Result, |
30 | }; | 30 | }; |
31 | use ra_db::ExternSourceId; | 31 | use ra_db::{CrateId, ExternSourceId}; |
32 | use rustc_hash::{FxHashMap, FxHashSet}; | 32 | use rustc_hash::{FxHashMap, FxHashSet}; |
33 | 33 | ||
34 | fn create_flycheck(workspaces: &[ProjectWorkspace], config: &FlycheckConfig) -> Option<Flycheck> { | 34 | fn create_flycheck(workspaces: &[ProjectWorkspace], config: &FlycheckConfig) -> Option<Flycheck> { |
@@ -290,10 +290,6 @@ impl GlobalStateSnapshot { | |||
290 | file_id_to_url(&self.vfs.read(), id) | 290 | file_id_to_url(&self.vfs.read(), id) |
291 | } | 291 | } |
292 | 292 | ||
293 | pub fn file_id_to_path(&self, id: FileId) -> PathBuf { | ||
294 | self.vfs.read().file2path(VfsFile(id.0)) | ||
295 | } | ||
296 | |||
297 | pub fn file_line_endings(&self, id: FileId) -> LineEndings { | 293 | pub fn file_line_endings(&self, id: FileId) -> LineEndings { |
298 | self.vfs.read().file_line_endings(VfsFile(id.0)) | 294 | self.vfs.read().file_line_endings(VfsFile(id.0)) |
299 | } | 295 | } |
@@ -305,6 +301,20 @@ impl GlobalStateSnapshot { | |||
305 | url_from_abs_path(&path) | 301 | url_from_abs_path(&path) |
306 | } | 302 | } |
307 | 303 | ||
304 | pub(crate) fn cargo_target_for_crate_root( | ||
305 | &self, | ||
306 | crate_id: CrateId, | ||
307 | ) -> Option<(&CargoWorkspace, Target)> { | ||
308 | let file_id = self.analysis().crate_root(crate_id).ok()?; | ||
309 | let path = self.vfs.read().file2path(VfsFile(file_id.0)); | ||
310 | self.workspaces.iter().find_map(|ws| match ws { | ||
311 | ProjectWorkspace::Cargo { cargo, .. } => { | ||
312 | cargo.target_by_root(&path).map(|it| (cargo, it)) | ||
313 | } | ||
314 | ProjectWorkspace::Json { .. } => None, | ||
315 | }) | ||
316 | } | ||
317 | |||
308 | pub fn status(&self) -> String { | 318 | pub fn status(&self) -> String { |
309 | let mut buf = String::new(); | 319 | let mut buf = String::new(); |
310 | if self.workspaces.is_empty() { | 320 | if self.workspaces.is_empty() { |