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/rust-analyzer/src/cargo_target_spec.rs | |
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/rust-analyzer/src/cargo_target_spec.rs')
-rw-r--r-- | crates/rust-analyzer/src/cargo_target_spec.rs | 32 |
1 files changed, 14 insertions, 18 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) { |