Skip to content

Commit 1ce8c2b

Browse files
bors[bot]matklad
andauthored
Merge #4927
4927: Better encapsulate reverse-mapping of files to cargo targets r=matklad a=matklad 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... bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents f061b2d + dc90e0b commit 1ce8c2b

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

crates/rust-analyzer/src/cargo_target_spec.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use ra_cfg::CfgExpr;
44
use ra_ide::{FileId, RunnableKind, TestId};
5-
use ra_project_model::{self, ProjectWorkspace, TargetKind};
5+
use ra_project_model::{self, TargetKind};
66

77
use crate::{global_state::GlobalStateSnapshot, Result};
88

@@ -89,27 +89,23 @@ impl CargoTargetSpec {
8989
}
9090

9191
pub(crate) fn for_file(
92-
world: &GlobalStateSnapshot,
92+
global_state_snapshot: &GlobalStateSnapshot,
9393
file_id: FileId,
9494
) -> Result<Option<CargoTargetSpec>> {
95-
let &crate_id = match world.analysis().crate_for(file_id)?.first() {
96-
Some(crate_id) => crate_id,
95+
let crate_id = match global_state_snapshot.analysis().crate_for(file_id)?.first() {
96+
Some(crate_id) => *crate_id,
9797
None => return Ok(None),
9898
};
99-
let file_id = world.analysis().crate_root(crate_id)?;
100-
let path = world.file_id_to_path(file_id);
101-
let res = world.workspaces.iter().find_map(|ws| match ws {
102-
ProjectWorkspace::Cargo { cargo, .. } => {
103-
let tgt = cargo.target_by_root(&path)?;
104-
Some(CargoTargetSpec {
105-
package: cargo.package_flag(&cargo[cargo[tgt].package]),
106-
target: cargo[tgt].name.clone(),
107-
target_kind: cargo[tgt].kind,
108-
})
109-
}
110-
ProjectWorkspace::Json { .. } => None,
111-
});
112-
Ok(res)
99+
let (cargo_ws, target) = match global_state_snapshot.cargo_target_for_crate_root(crate_id) {
100+
Some(it) => it,
101+
None => return Ok(None),
102+
};
103+
let res = CargoTargetSpec {
104+
package: cargo_ws.package_flag(&cargo_ws[cargo_ws[target].package]),
105+
target: cargo_ws[target].name.clone(),
106+
target_kind: cargo_ws[target].kind,
107+
};
108+
Ok(Some(res))
113109
}
114110

115111
pub(crate) fn push_to(self, buf: &mut Vec<String>, kind: &RunnableKind) {

crates/rust-analyzer/src/global_state.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ra_flycheck::{Flycheck, FlycheckConfig};
1515
use ra_ide::{
1616
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId,
1717
};
18-
use ra_project_model::{ProcMacroClient, ProjectWorkspace};
18+
use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target};
1919
use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsTask, Watch};
2020
use relative_path::RelativePathBuf;
2121
use stdx::format_to;
@@ -28,7 +28,7 @@ use crate::{
2828
vfs_glob::{Glob, RustPackageFilterBuilder},
2929
LspError, Result,
3030
};
31-
use ra_db::ExternSourceId;
31+
use ra_db::{CrateId, ExternSourceId};
3232
use rustc_hash::{FxHashMap, FxHashSet};
3333

3434
fn create_flycheck(workspaces: &[ProjectWorkspace], config: &FlycheckConfig) -> Option<Flycheck> {
@@ -290,10 +290,6 @@ impl GlobalStateSnapshot {
290290
file_id_to_url(&self.vfs.read(), id)
291291
}
292292

293-
pub fn file_id_to_path(&self, id: FileId) -> PathBuf {
294-
self.vfs.read().file2path(VfsFile(id.0))
295-
}
296-
297293
pub fn file_line_endings(&self, id: FileId) -> LineEndings {
298294
self.vfs.read().file_line_endings(VfsFile(id.0))
299295
}
@@ -305,6 +301,20 @@ impl GlobalStateSnapshot {
305301
url_from_abs_path(&path)
306302
}
307303

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+
308318
pub fn status(&self) -> String {
309319
let mut buf = String::new();
310320
if self.workspaces.is_empty() {

0 commit comments

Comments
 (0)